隐藏父头,如果 tbody 中的所有 tr 都隐藏显示:无
Hide parent thead if ALL tr in tbody are hidden display:none
我需要你的帮助:
问题:
我的表有一个选择过滤器。如果值不同,筛选器将隐藏正文的 tr 行。表头仍显示。
问题:
如果选择过滤器隐藏(显示:无;?)身体的所有 tr,头部也应该隐藏。
守则:
$(document).ready(function(){
$("select[name='kurs']").click(function() {
$('tbody').each(function(){
if ($(this).find("tr:hidden")) {
$(this).closest(thead).hide();
}
});
});
});
这也将做:
$(this).parent().find("thead").hide();
示例代码:
function hide() {
$('tbody').each(function(){
if($(this).not("tr:hidden").length=1)
{
$(this).parent().find("thead").hide();
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Table
<table border='1'>
<thead>
<tr><td>H1</td><td>H2</td></tr>
</thead>
<tbody>
<tr style='display:none'><td>a</td><td>b</td></tr>
<tr style='display:none'><td>a</td><td>b</td></tr>
<tr style='display:none'><td>a</td><td>b</td></tr>
<tr style='display:none'><td>a</td><td>b</td></tr>
</tbody>
</table>
<button onClick='hide()'>Hide</button>
怎么样
$('tbody').each(function(){
if ($(this).has("> tr:visible").length === 0){
$(this).closest('table').hide();
}
});
它会检查tbody
中可见tr
,如果没有隐藏table
。
检查这个小提琴,看看这是否是你想要的。
当选择更改时,它会筛选行。我做了一些事情,可能是也可能不是您可能拥有的过滤方法。重要的是在没有行时关闭头的部分。
$("#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();
}
});
});
希望对您有所帮助。
相关文章:
- 使用Javascript,如何显示<tr>在另一<tr>悬停时,隐藏第二个<tr>
- jQuery只隐藏<tr>在一张桌子上
- 获取TR之外最接近的隐藏值
- 防止隐藏的 tr 破坏 td 宽度
- 根据TD类别隐藏TR
- Javascript没有't隐藏tr元素
- 隐藏<tr>包含id中的变量's
- 将表的隐藏状态更改为可见状态>点击TR
- 隐藏具有th和colspan的tr
- 如果链接没有 href,则隐藏父 TR
- 隐藏父头,如果 tbody 中的所有 tr 都隐藏显示:无
- 在特定时间后从表中隐藏 tr
- 隐藏/显示基于所选单选按钮的表 TR - jquery
- 使用 javascript 隐藏具有相同类的所有 tr
- 选择任何 TR 和/或 TD 并隐藏元素
- 隐藏tr元素,以防在其中单击
- 隐藏tr.中的整行
- 仅当td在特定html标记后不包含任何内容时,才隐藏tr
- 使用jQuery validate可以显示和隐藏TR ID元素吗
- 基于多个CSS类和来自多个复选框的输入显示/隐藏tr元素