gpt4 book ai didi

javascript - 无法在我的 HTML 表格中显示第三列(向下钻取功能 jQuery/JavaScript )

转载 作者:行者123 更新时间:2023-12-02 15:17:19 25 4
gpt4 key购买 nike

我正在处理这个具有向下钻取功能的 HTML 表,它适用于一列,但我无法显示第三列,我想单击子类别列并显示另一列,但我陷入了这部分。

这是我的 JS 代码

         function toggleVisibilitySubcategory() {

$('table.drillDown th:nth-child(2)').toggle();
$('table.drillDown tbody td:nth-child(2)').toggle();
}

function toggleVisibilityItem() {

$('table.drillDown th:nth-child(3)').toggle();
$('table.drillDown tbody td:nth-child(3)').toggle();
}

$(function () {
toggleVisibilitySubcategory();
toggleVisibilityItem();
$('table.drillDown').each(function() {

var $table = $(this);
$table.find('.parent').click(function() {
console.log( "click on parent" );
$(this).nextUntil('.parent').toggle();

if ($(this).find("td:hidden").length > 0) {
toggleVisibilitySubcategory();
}

});

var $childRows = $table.find('tbody tr').not('.parent').hide();
$table.find('button.hide').click(function() {
$childRows.hide();
});
$table.find('button.show').click(function() {
$childRows.filter('.child').show();
});
$table.find('tr.child').click(function(){
$(this).nextUntil('.child').show()
});


});
});

这是 HTML 表格。

 <table class="drillDown" border="1" align="center">

<thead>
<th>Category</th>
<th>Subcategory</th>
<th>Item</th>
<th>LW</th>
</thead>

<tbody>
<!--PARENT ROW-->
<tr class="parent">
<td>Category 1</td>
<td></td>
<td></td>
<td></td>
</tr>

<tr class="child" >
<td>&nbsp;</td>
<td>Subcategory_1 A
<td>a</td>
<td></td>
</tr>

<tr class="child">
<td>&nbsp;</td>
<td>Subcategory_1 B</td>
<td>a</td>
<td></td>
</tr>
</tbody>
</table>

最佳答案

我的解决方案是创建一个新的切换函数来隐藏/显示最后一个表列,并像下面的代码片段一样使用它:

function toggleVisibilitySubcategory() {
$('table.drillDown th:nth-child(2)').toggle();
$('table.drillDown tbody td:nth-child(2)').toggle();
}
$(function () {
toggleVisibilitySubcategory();
$('table.drillDown').each(function() {

var $table = $(this);
$table.find('.parent').click(function() {
console.log( "*****Click on Parent" );
$(this).nextUntil('.parent').toggle();
if ($(this).find("td:hidden").length == 1) {
toggleVisibilitySubcategory();
}
});

var $childRows = $table.find('tbody tr').not('.parent').hide();
$table.find('button.hide').click(function() {
$childRows.hide();
});
$table.find('button.show').click(function() {
$childRows.filter('.child').show();
});
$table.find('tr.child').click(function(){
$(this).nextUntil('.child').show()
});

});
});
<script src="http://code.jquery.com/jquery-1.11.3.js"></script>

<table class="drillDown" border="1" align="center">
<colgroup>
<thead>
<tr>
<th>Category</th>
<th>Subcategory</th>
</tr>
</thead>
</colgroup>
<tbody>
<!--PARENT ROW-->
<tr class="parent">
<td>Category 1</td>
<td colspan="2">$12,345.45</td>
</tr>
<tr class="child" >
<td>&nbsp;</td>
<td colspan="2">Subcategory_1 A</td>
</tr>
<tr class="child">
<td>&nbsp;</td>
<td colspan="2">Subcategory_1 B</td>
</tr>
<!--PARENT ROW-->
<tr class="parent">
<td>Category 2</td>
<td colspan="2">$135,754.99</td>
</tr>
<tr class="child" >
<td>&nbsp;</td>
<td colspan="2">Subcategory_2 A</td>
</tr>
<tr class="child">
<td>&nbsp;</td>
<td colspan="2">Subcategory_2 B</td>
</tr>
<tr class="child">
<td>&nbsp;</td>
<td colspan="2">Subcategory_2 C</td>
</tr>

<tr class="parent">
<td>Catgegory 3</td>
<td colspan="2">$232,563.46</td>
</tr>
<tr class="child" >
<td>&nbsp;</td>
<td colspan="2">Subcategory_3 A</td>
</tr>
<tr class="child">
<td>&nbsp;</td>
<td colspan="2">Subcategory_3 B</td>
</tr>
<tr class="child">
<td>&nbsp;</td>
<td colspan="2">Subcategory_3 C</td>
</tr>
</tbody>
</table>

关于javascript - 无法在我的 HTML 表格中显示第三列(向下钻取功能 jQuery/JavaScript ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com