Collapsing Table Rows via Jquery -
im trying build table. table consists of following. each line row.
main-parent + parent - children - children + parent - children - children
the children collapse parent. , each of parents collapse main parent. ive got partially working not fully. can see behavior in http://jsfiddle.net/0lh5ozyb/88/
html
<table class="table"> <tr class="main-parent" id=""> <td><div>main parent</div></td> <td><div>main parent</div></td> <td><div>main parent</div></td> </tr> <tr class="parent sub" id="row1"> <td><div>parent</div></td> <td><div>parent</div></td> <td><div>parent</div></td> </tr> <tr class="child-row1-1 sub"> <td><div>child</div></td> <td><div>jackson</div></td> <td><div>94</div></td> </tr> <tr class="child-row1-2 sub"> <td><div>child</div></td> <td><div>doe</div></td> <td><div>80</div></td> </tr> <tr class="parent sub" id="row2"> <td><div>parent</div></td> <td><div>parent</div></td> <td><div>parent</div></td> </tr> <tr class="child-row2-1 sub"> <td><div>child</div></td> <td><div>jackson</div></td> <td><div>94</div></td> </tr> <tr class="child-row2-2 sub"> <td><div>child</div></td> <td><div>doe</div></td> <td><div>80</div></td> </tr> </table>
jquery
// main heading $('tr.main-parent') .css("cursor", "pointer") .click(function () { var $children = $(this).nextuntil($('tr').not('.sub')); $children.find('td > div, td').slidetoggle(); }); $('tr[class^=child-]').find('td > div, td').hide(); // parent heading $('tr.parent') .css("cursor", "pointer") .click(function () { var $children = $(this).nextuntil($('tr').not('[class^=child-]')); $children.find('td > div, td').slidetoggle(); }); $('tr.sub').find('td > div, td').hide();
slidetoggle can't used main heading because child headings may in mixed states of visibility. inverting subheadings visible.
if @ least 1 visible need them close.
// main heading $('tr.main-parent') .css("cursor", "pointer") .click(function () { var $children = $(this).nextuntil($('tr').not('.sub')); if ($children.find(':visible').length) { $children.find('td > div, td').slideup(); } else { $children.find('td > div, td').slidedown(); } });
see: http://jsfiddle.net/0lh5ozyb/120/
edit
if show subheadings when opening main heading, filter them first:
$children.filter('.parent').find('td > div, td').slidedown();
updated example: http://jsfiddle.net/0lh5ozyb/133/
Comments
Post a Comment