[英]How to hide or show column of table created using datatables in javascript?
Do you know how to hide or show column when datatable's source is javascript? 当数据表的源代码是javascript时,您知道如何隐藏或显示列吗?
Methods for showing or hiding columns 显示或隐藏列的方法
table = $('#example').DataTable();
var col = table.column("0").visible(false);
work when data source is directly into html 当数据源直接进入html时工作
<table id="example" class="row-border hover">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
...
But it does not work and launch an error when DataTable has a javascript source 但当DataTable具有javascript源时,它不起作用并启动错误
var table = $('#example').dataTable({
"data": source,
"columns": columns,
"columnDefs": defs
});
var col = table.column("0").visible(false);//ERROR!
Do you know how to hide a column of Datatables with a javascript source please? 您知道如何用JavaScript源隐藏Datatables列吗?
Try something like this, 试试这个
var table = $('#example').dataTable({
"data": source,
"columns": columns,
"columnDefs": [
{
"targets": [ 0 ],
"visible": false,
},
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull )
{
$('td:eq(0)', nRow).hide();
}
});
Updated. 更新。 Try to add fnRowCallback.
尝试添加fnRowCallback。 thanks!
谢谢!
I finally found another answere : it doesn't depends of html or json source but there is a difference between DataTable() which is the new version and dataTable() the old version 我终于找到了另一个答案:它不依赖于html或json源,但是新版本的DataTable()和旧版本的dataTable()之间有区别
column(n).visible(bool)
works for DataTable() 适用于DataTable()
fnRowCallback
works for dataTable() 适用于dataTable()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.