2012-07-11 75 views
0
<html> 
<head> 
    <script src="jquery-1.4.4.js"></script> 
    <script> 


     $('table').each(function(a, tbl) { 
      var currentTableRows = $(tbl).attr('rows').length - 1; 
      $(tbl).find('th').each(function(i) { 
       var remove = 0; 
       var currentTable = $(this).parents('table'); 

       var tds = currentTable.find('tr td:nth-child(' + (i + 1) + ')'); 
       tds.each(function(j) { if (this.innerHTML == '') remove++; }); 

       if (remove == currentTableRows) { 
        $(this).hide(); 
        tds.hide(); 
       } 
      }); 
     }); 

    </script> 
</head> 
<body> 
    <table border="1" > 
     <tr><td colspan="4" > alaa </td></tr> 
     <tr><th>Column1</th><th>Column2</th><th>Column3</th><th>Column4</th></tr> 
     <tr ><td>1st</td><td>1.1</td><td></td><td></td></tr> 
     <tr class="data"><td>2nd</td><td>2.1</td><td></td><td></td></tr> 
     <tr class="data"><td>3rd</td><td>3.1</td><td></td><td>1</td></tr> 
     <tr class="data"><td>4th</td><td></td><td></td><td></td></tr> 
     <tr ><td></td><td></td><td></td><td></td></tr> 
     <tr class="data"><td></td><td></td><td></td><td></td></tr> 
    </table> 

</body> 

隱藏空列..不起作用:

這裏是我的代碼...我想,從庫中的問題,所以我嘗試了許多庫如jQuery 1.4.4,1.5.2和其他

下面是測試和正常工作有http://jsfiddle.net/nlovatt/JsLn8/

但在我的文件..它不工作..

問候,

+0

你得到一個錯誤?你能發佈一個鏈接到你的文件嗎? – Thomas 2012-07-11 10:47:39

+0

沒有..我沒有得到任何錯誤.. 這裏是鏈接http://summer.remal.com/provise_alaa/JQ.html – 2012-07-11 10:49:15

回答

2

有兩個原因你的代碼不能正常工作。

1)在加載HEAD時立即執行腳本,在此階段,表不存在,所以它什麼也不做。要解決此問題,請確保在頁面加載時執行它。 2)當您將列中的空白單元格數與表中的總行數進行比較時,您錯過了大多數列的數量不同行作爲表格(你的第一行只有一列寬)。您需要與實際列中的行數進行比較,或者更好,只需做相反的事情並檢查非空列。然後

完整的代碼變成:

$(document).ready(function() { 
    $('table').each(function(a, tbl) { 
     $(tbl).find('th').each(function(i) { 
      var remove = true; 
      var currentTable = $(this).parents('table'); 
      var tds = currentTable.find('tr td:nth-child(' + (i + 1) + ')'); 
      tds.each(function(j) { if (this.innerHTML != '') remove = false; }); 
      if (remove) { 
       $(this).hide(); 
       tds.hide(); 
      } 
     }); 
    }); 
}); 
+0

thnx ..它工作正常..:D – 2012-07-11 14:08:07

+1

這工作得很好。我會注意到currentTableRows變量是未使用和不必要的。另外,如果你有大量的表(或嵌套表),你不希望這個表應用,你應該使用一個不同的選擇器,可能包含一個類,並且由於使用.parents,你需要把該選擇器在兩個使用'table'的地方。 – 2013-01-31 21:54:19

+0

你說得對,很抱歉在那裏留下'currentTableRows'。至於'.parents'的使用,那隻能得到我們正在循環的'th'的父表,實際上,這很可能與'tbl'參考相同,但我對此並不積極。如果你想具體說明你將哪個表應用到所有行中的$('table')。each(...)',那麼你可以將它改爲'$( 'table.hiddenColumns')。each(...)'where'hiddenColumns是一個CSS類。 – Thor84no 2013-01-31 23:48:22

0

嘗試這樣

$('#mytable tr th').each(function(i) { 
    //select all td in this column 
    var tds = $(this).parents('table') 
      .find('tr td:nth-child(' + (i + 1) + ')'); 
    //check if all the cells in this column are empty 
    if(tds.length == tds.filter(':empty').length) { 
     //hide header 
     $(this).hide(); 
     //hide cells 
     tds.hide(); 
    } 
    }); 

在表中隱藏列,如果在列中的所有單元格爲空

+0

我試了一下..仍然沒有工作.. – 2012-07-11 10:53:00

+0

代碼已經測試在http://jsfiddle.net/nlovatt/JsLn8/ ,但在我的文件..它不..爲什麼:$ – 2012-07-11 10:53:33

+0

@TheGhost可能是因爲我的答案中提到的第一個原因 - 你正在執行你的代碼在表存在之前。 – Thor84no 2012-07-11 11:05:11