2015-01-10 92 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(); 
     } 
    }); 
}); 

希望它有幫助。