gpt4 book ai didi

javascript - 展开/折叠带有嵌套行的表行

转载 作者:行者123 更新时间:2023-11-28 04:39:06 29 4
gpt4 key购买 nike

我想创建一个具有以下结构的表:
Row1 datacolumn1 datacolumn2 datacolumn2
SubRow1 datacolumn1 datacolumn2 datacolumn2
SubRow2 数据列 1 数据列 2 数据列 2
SubRow3 数据列 1 数据列 2 数据列 2

我希望子行在单击时展开/折叠。我似乎无法让它与表格一起工作。我知道列表是更好的选择,但表格更容易维护。

<tbody>
<tr class="row1 head1">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr class="row1 head2 closed">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr class="row1 head3">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr class="row1 head4">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr class="row1 head5">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr class="row2 head1">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>

<tr class="row2 head4">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr class="row2 head5">
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr>
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>
<tr>
<td>Cash and Equivalents</td>
<td>000,000</td>
<td>000,000</td>

</tr>

</tbody>

点击行,我调用以下函数:

holdingsTree: function(that){
var stack = '',
classes = ba.splitClasses(that.attr('class')),
nextRow = ba.getClassNumber(classes[1], "head");

if (that.next().hasClass('head'+nextRow)){
if (that.next().hasClass(classes[0]) && that.next().hasClass('open')){
that.nextUntil('.head1').hide().addClass('closed').removeClass('open');
} else if (that.next().hasClass(classes[0])){
stack = that.nextUntil('.head1');
$.each(stack, function(i, item){ if ($(item).hasClass('head' + (nextRow))){ $(this).show().addClass('open').removeClass('closed'); } });
}
} else if (that.next().hasClass('head'+ (nextRow+1))){
if (that.next().hasClass(classes[0]) && that.next().hasClass('open')){
that.nextUntil('.head1').hide().addClass('closed').removeClass('open');
} else if (that.next().hasClass(classes[0])){
stack = that.nextUntil('.head1');
$.each(stack, function(i, item){ if ($(item).hasClass('head' + (nextRow+1))){ $(this).show().addClass('open').removeClass('closed'); } });
}
} else if (that.next().hasClass('head'+ (nextRow+2))){
if (that.next().hasClass(classes[0]) && that.next().hasClass('open')){
that.nextUntil('.head1').hide().addClass('closed').removeClass('open');
return;
} else if (that.next().hasClass(classes[0])){
stack = that.nextUntil('.head'+ (nextRow+3));
$.each(stack, function(i, item){ if ($(item).hasClass('head' + (nextRow+2))){ $(this).show().addClass('open').removeClass('closed'); } });
//stack.find('.head'+(nextRow+2)).show().addClass('open').removeClass('closed');
}
} else if (that.next().hasClass('head'+ (nextRow+3))){
if (that.next().hasClass(classes[0]) && that.next().hasClass('open')){
that.nextUntil('.head1').hide().addClass('closed').removeClass('open');
} else if (that.next().hasClass(classes[0])){
stack = that.nextUntil('.head1');
$.each(stack, function(i, item){ if ($(item).hasClass('head' + (nextRow+3))){ $(this).show().addClass('open').removeClass('closed'); } });
}
} else if (that.next().hasClass('head'+ (nextRow+4))){
if (that.next().hasClass(classes[0]) && that.next().hasClass('open')){
that.nextUntil('.head1').hide().addClass('closed').removeClass('open');
} else if (that.next().hasClass(classes[0])){
that.nextUntil('.head1').show().addClass('open').removeClass('closed');
}
}

},

splitClasses: function(names){
var names = names.split(' ');
return names;
},

getClassNumber: function(name, pretext){
var result = name.split(pretext);
console.log(parseInt(result[1]) + 1);
return parseInt(result[1]) + 1;
}

它非常糟糕。我知道。 :)

最佳答案

您可以为称为父行的行设置一个类,为称为子行的行设置一个类,并切换它们的显示。

方法如下:

$(document).ready(function() {

function getChildren($row) {
var children = [];
while($row.next().hasClass('child')) {
children.push($row.next());
$row = $row.next();
}
return children;
}

$('.parent').on('click', function() {

var children = getChildren($(this));
$.each(children, function() {
$(this).toggle();
})
});

})

查看此 fiddle 以获得完整的运行代码 http://jsfiddle.net/T8t2r/3/如果这解决了您的问题,请接受答案!

祝你好运!

编辑:在更多级别上工作的一种方法是拥有级别属性。所以这里是更新版本,可以在任意数量的级别上工作。 http://jsfiddle.net/T8t2r/9/

$(document).ready(function() {

function getChildren($row) {
var children = [], level = $row.attr('data-level');
while($row.next().attr('data-level') > level) {
children.push($row.next());
$row = $row.next();
}
return children;
}

$('.parent').on('click', function() {

var children = getChildren($(this));
$.each(children, function() {
$(this).toggle();
})
});

})

关于javascript - 展开/折叠带有嵌套行的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11574586/

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