检查这个fiddle,看看是否是你想要的。
当选择更改时,它会过滤行。我做了一些可能或不可能的过滤方法。重要的是当没有行时关闭thead的部分。
$("#filter").change(function() {// Select changes and filters rows of the different tables.
var class_to_filter = "." + $(this).val();// I'm accessing rows by class in order to close them, you may access them using some other method.
$.each($(class_to_filter), function (i, item) {// For each one of them, close and check if you have to close thead as well.
var $tr = $(item).closest('tr'),
$tbody = $tr.closest('tbody'),
$thead = $tbody.siblings('thead'),
numOfVisibleRows;
$tr.hide();// Hide row.
numOfVisibleRows = $('tr:visible', $tbody).length;// Number of sibling rows visible.
if (!numOfVisibleRows) {// if 0, hide thead.
$thead.hide();
}
});
});
希望它有帮助。
如果没有'tr'可见,隐藏整个表不是更容易吗? –
@RoryMcCrossan表中可能有多个'tbody'。 – Teemu
@Teemu:即使如此,如果它们全都隐藏起来,隐藏整个桌子和整个桌子之间没有什么区别,除非有某种原因(不太可能因为某种原因不应该隐藏)。 – BoltClock