2011-03-02 23 views
10

如何隱藏該列中的所有空單元格,包括該列中的標題<th>,同時保留其他列及其標題原樣。以下jquery隱藏整個<th>,這是不是我想要的。 Here是一個示例,我只想隱藏整個「Column3」,其中包括<th>。提前謝謝了。刪除/隱藏表格的空列,包括<th>

$('table#mytable tr').each(function() { 
    if ($(this).children('td:empty').length === $(this).children('td').length) { 
     $(this).hide(); 
    } 
}); 
+2

該代碼隱藏錶行而不是列,因爲'$(this)'將獲得每個'tr'的值。 – Dan 2011-03-02 19:39:49

+1

您是否想隱藏所有空單元格的列或一個或多個空單元格? – 2011-03-02 19:40:06

+0

我想隱藏包含列中標題的所有空單元格的列,而保留其他列標題原樣。謝謝。 – DGT 2011-03-02 19:50:43

回答

13

花了一段時間拼湊起來。感謝nxt的一些代碼。

$('#mytable th').each(function(i) { 
    var remove = 0; 

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

    if (remove == ($('#mytable tr').length - 1)) { 
     $(this).hide(); 
     tds.hide(); 
    } 
}); 
+0

如果標題包含內容(標題)但不包含任何內容,則不會刪除標題。我仍然在努力。 – 2011-03-02 19:43:21

+0

確切地說,它不隱藏標題,這是我想要隱藏的,而保留其他標題。謝謝。 – DGT 2011-03-02 19:46:59

+0

好的,更新和測試。 http://jsfiddle.net/DRFBG – 2011-03-02 20:31:24

5

如果你想隱藏,如果所有細胞(忽略頭)是空的,你可以不喜歡列:

$('#mytable tr th').each(function(i) { 
    //select all tds 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(); 
     } 
}); 

樣品:http://jsfiddle.net/DeQHs/

樣品2(適合jQuery> 1.7):http://jsfiddle.net/mkginfo/mhgtmc05/

+0

非常感謝。這很好。 – DGT 2011-03-02 20:09:35

+0

這不起作用:http:// jsfiddle。網/ DeQHs/1/ – 2011-03-02 20:14:12

+0

二。如果只有一個字段爲空,而不是全部,則它會清除列。 – 2011-03-02 20:15:41

1

您需要下一個編碼:

HTML

<table id="mytable" border="1"> 
    <thead> 
     <tr><th>Column1</th><th>Column2</th><th>Column3</th><th>Column4</th></tr> 
    </thead> 
    <tbody> 
     <tr class="data"><td>1st</td><td>1.1</td><td></td><td>1</td></tr> 
     <tr class="data"><td>2nd</td><td>2.01</td><td></td><td>2</td></tr> 
     <tr class="data"><td>3rd</td><td>3.001</td><td></td><td>3</td></tr> 
     <tr class="data"><td>4th</td><td>4.01</td><td></td><td>4</td></tr> 
    </tbody> 
</table> 

的JavaScript

var $table = $('#mytable'); 
var thead = $table[0].tHead, tbody = $table[0].tBodies[0]; 
var colsLen = tbody.rows[0].cells.length, rowsLen = tbody.rows.length; 
var hideNode = function(node) { if (node) node.style.display = "none"; }; 
for (var j = 0; j < colsLen; ++j) { 
    var counter = 0; 
    for (var i = 0; i < rowsLen; ++i) { 
     if (tbody.rows[i].cells[j].childNodes.length == 0) ++counter; 
    } 
    if (counter == rowsLen) { 
     for (var i = 0; i < rowsLen; ++i) { 
      hideNode(tbody.rows[i].cells[j]); 
     } 
     hideNode(thead.rows[0].cells[j]); 
    } 
} 
2

無解的,我在這裏工作多表的例子。這是我用來隱藏空列帶或不帶標頭中的文本:

$('table').each(function(a, tbl) { 
     var currentTableRows = $(tbl).find('tbody tr').length; 
     $(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).text().trim() === '') remove++; }); 

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

如果表中的數據是從MySQL查詢可以驗證,如果一列是通過使用字段計數空( count = 0意味着沒有值)。

當你有很多字段時,它是相當挑剔的,並且相應的頁眉和頁腳單元也需要IF條件。但它的作品...

if ($sum_field>'0') echo "<th>field</th>"; 
if ($sum_field>'0') echo "<td>" . $row['field'] . "</td>"; 

@nmat解決方案工作正常,但不處理頁腳。