2015-01-10 93 views
0

我需要你的帮助:隐藏父THEAD如果tbody的所有tr所隐藏显示:无

问题:

我有我的表中选择滤波器。 如果值不相同,则过滤器将隐藏tbody的tr行。表头仍然显示。

问题:

如果选择滤波器隐藏(显示:无;)ALL TR的TBODY中,THEAD也应该隐藏。

验证码:

$(document).ready(function(){ 
    $("select[name='kurs']").click(function() { 
     $('tbody').each(function(){ 
      if ($(this).find("tr:hidden")) { 
       $(this).closest(thead).hide(); 
      } 
     }); 
    }); 
}); 
+0

如果没有'tr'可见,隐藏整个表不是更容易吗? –

+0

@RoryMcCrossan表中可能有多个'tbody'。 – Teemu

+1

@Teemu:即使如此,如果它们全都隐藏起来,隐藏整个桌子和整个桌子之间没有什么区别,除非有某种原因(不太可能因为某种原因不应该隐藏)。 – BoltClock

回答

0

如何

$('tbody').each(function(){ 
    if ($(this).has("> tr:visible").length === 0){ 
     $(this).closest('table').hide(); 
    } 
}); 

它在tbody检查可见tr S,如果没有任何table是隐藏的。

+0

好主意。但没有工作:( –

+0

我没有意识到'.has()'没有返回布尔结果 – Musa

0

这也将这样做:

$(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>

+0

这段代码杀死了所有的数据,我在一页上得到了4个不同的表格,并且只有一个隐藏的数据集tr放松thead。 –

0

检查这个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(); 
     } 
    }); 
}); 

希望它有帮助。