javascript - Collapsing table rows with multiple tbody elements -
i have table looks this:
<table> <thead> <tr><th>customer</th><th>order</th><th>month</th></tr> </thead> <tbody> <tr><td>customer 1</td><td>#1</td><td>january</td></tr> <tr><td>customer 1</td><td>#2</td><td>april</td></tr> <tr><td>customer 1</td><td>#3</td><td>march</td></tr> </tbody> <tbody> <tr><td>customer 2</td><td>#1</td><td>january</td></tr> <tr><td>customer 2</td><td>#2</td><td>april</td></tr> <tr><td>customer 2</td><td>#3</td><td>march</td></tr> </tbody> <tbody> <tr><td>customer 3</td><td>#1</td><td>january</td></tr> <tr><td>customer 3</td><td>#2</td><td>april</td></tr> <tr><td>customer 3</td><td>#3</td><td>march</td></tr> </tbody> .... .... 10s of records </table>
i want make each tbody element clickable (collapsible) in collapsed state, summary of inside (say, customer 1 | 3 entries
) , in expanded state, see actual rows.
can done table structured shown above?
jsfiddle here: http://jsfiddle.net/ju4xh/
it's little messy , animations don't work (i'm guessing it's because it's on <tr>
s, here's came with:
$(document).ready(function () { $("table").on("click", "tbody", function () { var $this = $(this); var mytrs = $this.children("tr"); if ($this.hasclass("collapsed")) { $this.removeclass("collapsed"); mytrs.first().remove(); mytrs.show(); } else { $this.addclass("collapsed"); var newinfo = mytrs.first().children("td").first().text() + " | " + mytrs.length + " entries"; mytrs.hide(); $this.prepend($("<tr><td colspan='3'>" + newinfo + "</td></tr>").hide()).find("tr").first().slidedown(); } }); });
demo: http://jsfiddle.net/zhqaf/1/
when click non-collapsed <tbody>
, hide rows , prepend new 1 details wanted. when click collapsed <tbody>
, removes new "details" row, , shows original rows.
Comments
Post a Comment