2014-11-06 37 views
0

我有一個數據表,我隱藏一些列這樣說:隱藏列,並保持列在數據表DOM

<script> 
var tbl = document.getElementById("myTable"); 
for (var j = 0; j < tbl.rows.length; j++){ 
     tbl.rows[j].cells[1].style.display = "none";  
     tbl.rows[j].cells[2].style.display = "none"; 
     tbl.rows[j].cells[6].style.display = "none"; 
     tbl.rows[j].cells[8].style.display = "none"; 
    } 
</script> 

因爲當我試圖隱藏在數據表中定義的列,列從DOM中刪除。 我需要保留DOM中的列,因爲我使用此列的值來更改其他單元格的背景顏色。

我的數據表函數定義:

<script type="text/javascript"> 
$(document).ready(function() { 
    $('#myTable').dataTable({ 
     "bPaginate": false, 
     "bJQueryUI": true, 
     "bAutoWidth": false, 
     "iDisplayLength": -1, 
     "oLanguage": { 
     "sSearch": "Buscar", 
     "oPaginate":{ 
      "sFirst": "Primero", 
      "sLast":  "Ãimo", 
      "sNext":  "Siguiente", 
      "sPrevious": "Anterior" 
     } 
     } 
    }); 

    }); 
    $(function() { 
      $('.list-group-item').click(function() { 
       $('.panel-collapse.in').collapse('hide'); 
      }); 
    }); 
</script> 

如何隱藏列,保持列DataTable的DOM。

+0

你應該這樣做在jQuery中,就像你有你的數據表。另外,顯示:沒有不會從dom中刪除。你必須錯過那裏,你的問題會有所不同。 – 2014-11-06 21:22:43

+0

你能提供一個小提琴嗎? – Sai 2014-11-06 21:35:38

+0

@ADASein我試着隱藏jQuery中的列,但列已從數據表DOM中刪除,我需要評估其他列。出於這個原因,我決定使用display:none代替jquery。當我嘗試使用固定標題時,標題是固定的,但也顯示隱藏列中的文本。 – Joseleg 2014-11-06 22:41:59

回答

1

試試這個。 。 。

CSS:

th.hide_me, td.hide_me {display: none;}

在數據表初始化:

"aoColumnDefs": [ { "sClass": "hide_me", "aTargets": [ 0 ] } ] // first column in visible columns array gets class "hide_me"

記住你的隱藏類添加到您的THEAD細胞也:從檢索

<thead> 
    <th class="hide_me">First Column</th> 
    <th>Second Column</th> 
    <th>Third Column</th> 
</thead> 

回答:jQuery DataTables hide column without removing it from DOM