2017-05-04 86 views
0

我是jquery和datatable的新手,但很快学习。隐藏数据表中的列if if

我想根据我将测试的变量的值隐藏特定的列if。但我不知道在哪里说如果和代码来隐藏列。

HTML:

<table id="table_id" class="table table-striped table-bordered table hover" width="100%" cellspacing="0" > 
<thead> 
<tr> 
    <th>Have</th> 
    <th>A</th> 
    <th>Good</th> 
    <th>Day</th> 
</tr> 
</thead> 

的Jquery:

$(document).ready(function() { 
$('#table_id').DataTable({ 
    "processing": true, 
    "order": [[ 3, "desc" ]], 
    "ajax": { 
     "url": "somewhere.php", 
     "type": "POST" 
    }, 
    "columns": [ 
     { "data": "Have" }, 
     { "data": "A" }, 
     { "data": "Good" }, 
     { "data": "Day" } 
    ] 
}); 
}); 

伪的,如果

if($_POST('something') =="hey"){ 
    hide column 1 and 2;} 
+0

这应该被标记'[jquery-datatables]',但由于某种原因,它不让我通过编辑来改变它。 –

+0

https://datatables.net/reference/api/column().visible() –

+0

我试图添加这个标签,但不能。是的,我看到了API,但它看起来像代码需要在某个特定的地方,因为当我尝试它没有工作。 – Romain

回答

1

使用initComplete选项把你的条件,一旦表已代码初始化。使用columns().visible() API方法来隐藏选定的列。

例如:

var table = $('#example').DataTable({ 
    ajax: 'https://api.myjson.com/bins/qgcu', 
    initComplete: function(settings, json){ 
     var api = new $.fn.dataTable.Api(settings); 

     api.columns([4, 5]).visible(false); 
    } 
}); 

在上述例子中,json变量保存从中可以用于定义条件显示/隐藏栏服务器响应。

查看this example的代码和演示。