jQuery Expanding and Collapsing Table Rows
The expanding and collapsing behavior added earlier also conflicts with our filters. If a section is collapsed and a new filter is chosen, then the matching items are displayed, even if in the collapsed section. Conversely, if the table is filtered and a section is expanded, then all items in the expanded section are displayed regardless of whether they match the filter.
Since we have added the filtered class to all rows when they are removed by a filter button, we can check for this class inside our collapser's click handler:
var toggleSrc = $(this).attr('src'); if ( toggleSrc == toggleMinus ) { $(this).attr('src', togglePlus) .parents('tr').siblings().addClass('collapsed').fadeOut('fast'); } else{ $(this).attr('src', toggleMinus) .parents('tr').siblings().removeClass('collapsed') .not('.filtered').fadeIn('fast'); };
var%20toggleSrc%20%3D%20%24%28this%29.attr%28%27src%27%29%3B%20%0A%0D%0Aif%20%28%20toggleSrc%20%3D%3D%20toggleMinus%20%29%20%7B%20%0A%0D%0A%24%28this%29.attr%28%27src%27%2C%20togglePlus%29%20%0A%0D%0A.parents%28%27tr%27%29.siblings%28%29.addClass%28%27collapsed%27%29.fadeOut%28%27fast%27%29%3B%20%0A%0D%0A%7D%20else%7B%20%0A%0D%0A%24%28this%29.attr%28%27src%27%2C%20toggleMinus%29%20%0A%0D%0A.parents%28%27tr%27%29.siblings%28%29.removeClass%28%27collapsed%27%29%20%0A%0D%0A.not%28%27.filtered%27%29.fadeIn%28%27fast%27%29%3B%20%0A%0D%0A%7D%3B
While we are collapsing or expanding rows, we add or remove another new class on the rows. We need this class to solve the other half of the problem. The filtering code can use the class to ensure that a row should be shown when the filter changes:
$table.find ('tbody tr'). each(function() { if ($('td', this).filter(':nth-child(' + (column + 1) + ')').text() == e.data['keyword']) { $(this).removeClass('filtered').not('.collapsed').show(); } else if ($('th',this).length == 0) { $(this).addClass('filtered').hide(); } });
%24table.find%28%27tbody%20tr%27%29.each%28function%28%29%20%7B%20%0A%0D%0Aif%20%28%24%28%27td%27%2C%20this%29.filter%28%27%3Anth-child%28%27%20%2B%20%28column%20%2B%201%29%20%2B%20%27%29%27%29.text%28%29%20%0A%0D%0A%3D%3D%20e.data%5B%27keyword%27%5D%29%20%7B%20%0A%0D%0A%24%28this%29.removeClass%28%27filtered%27%29.not%28%27.collapsed%27%29.show%28%29%3B%20%0A%0D%0A%7D%20%0A%0D%0Aelse%20if%20%28%24%28%27th%27%2Cthis%29.length%20%3D%3D%200%29%20%7B%20%0A%0D%0A%24%28this%29.addClass%28%27filtered%27%29.hide%28%29%3B%20%0A%0D%0A%7D%20%0A%0D%0A%7D%29%3B%20
Now our features play nicely, each able to hide and show the rows independently.
Trackback(0)
|