[英]jQuery datatable immediately hide column
I got the jQuery datatable hide column feature to work properly. 我得到了jQuery数据表隐藏列功能才能正常工作。 The following code will hide the 2nd column of the table: 以下代码将隐藏表的第二列:
HTML 的HTML
<a href="#" class="btn toggle-vis" data-column="1" id="hideColumn">Show/Hide</a>
JS JS
$(document).ready(function()
{
var table = $('#example1').DataTable();
$('a.toggle-vis').on('click', function(e)
{
e.preventDefault();
var column = table.column($(this).attr('data-column'));
column.visible( ! column.visible());
});
}
What I would like to do is initially hide the column when the user first enters the page. 我想做的是最初在用户首次进入页面时隐藏该列。 The column will only show when clicked. 该列仅在单击时显示。
How do I go about adjusting the code to achieve this effect? 如何调整代码以达到此效果?
You need to use columnDefs 您需要使用columnDefs
Try: 尝试:
var table = $('#example1').DataTable(
{
"columnDefs": [
{
"targets": [ 2 ],
"visible": false
}
]
} );
EDIT 编辑
I'm not sure why that doesn't work. 我不确定为什么这行不通。 Adding the code in here since in the comment did not display well. 由于在注释中添加了代码,因此显示效果不佳。 Try this instead: 尝试以下方法:
var table = $('#example1').DataTable();
table.column(1).visible(false);
Try this 尝试这个
$(function () {
// To hide the table header during page load
$("#example1 tr th:nth-child(2)").hide();
// To hide the 2nd column during page load
$("#example1 tr td:nth-child(2)").each(function () {
$(this).hide();
});
// Hide and show while clicking the link
$(".toggle-vis").click(function () {
var col = $(this).data("column");
// Hide/Show the header
$("#example1 tr th:nth-child(" + col + ")").is(":visible") ? $("#example1 tr th:nth-child(" + col + ")").hide() : $("#example1 tr th:nth-child(" + col + ")").show();
// Hide/Show the details
$("#example1 tr td:nth-child(" + col + ")").each(function () {
$(this).is(":visible") ? $(this).hide() : $(this).show();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.