通过jQuery协助对嵌套的HTML表格进行分组

Khemikal

基本上我有一个带有3层扩展/折叠功能的html表。我要实现的目标是使层实际上在父项下扩展/折叠,而不是在右侧,基本上,一旦扩展,它们将变成向下倾斜的状态。这是我的JSFiddle:

https://jsfiddle.net/htfLmekL/1/

//expand collapse based on parent class which is column 1
$(document).ready(function() {
  $('.parent').prepend('-');

  $('.parent').on('click', function() {
    if ($(this).text().indexOf('-') != -1) {
      var str0 = $(this).text().replace(/-/g, '+');
      $(this).text(str0);
    } else {
      var str = $(this).text().replace(/\+/g, '-');
      $(this).text(str);
    }
    var $row = $(this).parent();
    var rowspan = +$(this).attr('rowspan') || 4;
    $.merge($row, $row.nextAll()).slice(0, rowspan).find('.alliance').toggle();
    $.merge($row, $row.nextAll()).slice(0, rowspan).find('.race').toggle();
    $.merge($row, $row.nextAll()).slice(0, rowspan).find('.role').toggle();
    $.merge($row, $row.nextAll()).slice(0, rowspan).find('.resource').toggle();
    $.merge($row, $row.nextAll()).slice(0, rowspan).find('.weapon').toggle();
    $.merge($row, $row.nextAll()).slice(0, rowspan).find('.price').toggle();

  });
});
沙阿

创建了类似这样的内容...尽管您将不得不对CSS进行大量调整才能使其完美。希望这可以帮助...

        <style>
  .tftable {
  font-size: 12px;
  color: #333333;
  width: 100%;
  border-width: 1px;
  border-color: #729ea5;
  border-collapse: collapse;
}

.tftable th {
  font-size: 12px;
  background-color: #acc8cc;
  border-width: 1px;
  padding: 8px;
  border-style: solid;
  border-color: #729ea5;
  text-align: left;
}

.tftable tr {
  background-color: #d4e3e5;
}

.tftable td {
  font-size: 12px;
  border-width: 1px;
  padding: 8px;
  border-style: solid;
  border-color: #729ea5;

}

.tftable tr:hover {
  background-color: #ffffff;
}

.race {
    width: 50px;
}


</style>

    </head>
    <body>








        <table class="tftable" border="1">
            <tr>
                <th>Group</th>
            </tr>
            <tr>
                <th><a onclick="javascript:toggleDiv(this,'grp1child1');"> - </a>Group 1

                    <table id="grp1child1" class="tftable" border="1"> 
                        <tr>
                            <th>Alliance : <a onclick="javascript:toggleDiv(this,'grp1child2');"> - </a>Good</th>
                        </tr>
                        <tr>
                            <td >
                                <table id="grp1child2" class="tftable" border="1">
                                    <tr>
                                        <th class="race">Race: </th>
                                        <th><a onclick="javascript:toggleDiv(this,'grp1child3');" > - </a>Humans</th>
                                        <th><a onclick="javascript:toggleDiv(this,'grp1child4');"> - </a>Elves</th>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>
                                            <table id="grp1child3" class="tftable" border="1">
                                                <tr>
                                                    <th colspan="3">&nbsp;</th>
                                                    <th>Price</th>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>

                                            </table>

                                        </td>
                                        <td>
                                            <table id="grp1child4" class="tftable" border="1">
                                                <tr>
                                                    <th colspan="3">&nbsp;</th>
                                                    <th>Price</th>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>

                                            </table>                                
                                        </td>
                                    </tr>                                    

                                </table>                                
                            </td>
                        </tr>



                    </table>

                </th>
        </tr>
        <tr>
                <th><a onclick="javascript:toggleDiv(this,'grp2child1');"> - </a>Group 2

                    <table id="grp2child1" class="tftable" border="1"> 
                        <tr>
                            <th >Alliance : <a onclick="javascript:toggleDiv(this,'grp2child2');"> - </a>Evil</th>
                        </tr>
                        <tr>
                            <td>
                                <table id="grp2child2" style="width : 100%">
                                    <tr>
                                        <th class="race">Race: </th>
                                        <th><a onclick="javascript:toggleDiv(this,'grp2child3');" > - </a>Trolls</th>
                                        <th><a onclick="javascript:toggleDiv(this,'grp2child4');"> - </a>Ogres</th>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>
                                            <table id="grp2child3" class="tftable" border="1">
                                                <tr>
                                                    <th colspan="3">&nbsp;</th>
                                                    <th>Price</th>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>

                                            </table>

                                        </td>
                                        <td>
                                            <table id="grp2child4" class="tftable" border="1">
                                                <tr>
                                                    <th colspan="3">&nbsp;</th>
                                                    <th>Price</th>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                    <td></td>
                                                </tr>

                                            </table>                                
                                        </td>
                                    </tr>                                    

                                </table>                                
                            </td>
                        </tr>

                    </table>

                </th>
            </tr>




        </table>



<script>

function toggleDiv(linkobj,divId) {
    linkobj.innerHTML = (linkobj.innerHTML === ' - ') ? ' + ' : ' - ' ;
    $("#"+divId).toggle();
}


</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

通过jQuery协助对嵌套的HTML表格进行分组

来自分类Dev

如何通过使用jQuery包装来对HTML元素进行分组

来自分类Dev

通过流对深层嵌套列表进行分组和求和

来自分类Dev

在循环中对 2 个 HTML 表格行进行分组

来自分类Dev

排序HTML表格,该表格通过JQuery追加

来自分类Dev

通过HTML表在JSON数组中进行分组

来自分类Dev

HTML中的嵌套表格

来自分类Dev

使用HTML嵌套表格

来自分类Dev

HTML中的嵌套表格

来自分类Dev

以表格形式输入信息,然后通过jquery进行调用

来自分类Dev

如何对嵌套列表进行分组?

来自分类Dev

PHP表格协助

来自分类Dev

下划线分组通过单面与嵌套对象属性进行比较

来自分类Dev

通过嵌套字典关键字对pandas数据框进行分组

来自分类Dev

如何通过数组中嵌套的子对象对象对对象进行分组

来自分类Dev

通过对MongoDB中嵌套的json数组文档的集合进行计数来分组

来自分类Dev

分组表格行组 HTML

来自分类Dev

嵌套查询协助

来自分类Dev

嵌套词典协助

来自分类Dev

协助嵌套if语句

来自分类Dev

如何通过jQuery向HTML表格添加单选按钮

来自分类Dev

通过忽略NA进行分组

来自分类Dev

通过案例陈述进行分组?

来自分类Dev

通过LINQ进行分组吗?

来自分类Dev

通过在Hive中分组来写出表格

来自分类Dev

嵌套表格行HTML CSS

来自分类Dev

HTML中的嵌套表格标签

来自分类Dev

如何通过按电子邮件地址对行进行分组来创建和发送表格-GoogleSheets

来自分类Dev

对嵌套文档中的记录进行分组