檢查這個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