我想创建一个具有以下结构的表:
Row1 datacolumn1 datacolumn2 datacolumn2
SubRow1 datacolumn1 datacolumn2 datacolumn2 SubRow2 datacolumn1
datacolumn2 datacolumn2 SubRow3
datacolumn1 datacolumn2 datacolumn2
我想让子行在点击时展开/折叠。我似乎无法让它与表格一起使用。我知道列表会是一个更好的选择,但表格更容易维护。
<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;
}
这很糟糕。我知道。:)