datatables hide show columns code example
Example 1: datatables dynamically hide columns
const table = $('#example').DataTable();
// Get the column API object
const column = table.column( 2 ); // gets 2nd column (0-indexed)
// Toggle the visibility
column.visible( ! column.visible() ); // true or false
Example 2: hide datatable columns using datatable predifine function
$(document).ready(function() {
$('#example').DataTable( {
"columnDefs": [
{
"targets": [ 2 ],
"visible": false,
"searchable": false
},
{
"targets": [ 3 ],
"visible": false
}
]
} );
} );
Example 3: datatable hide no data available in table
var someTableDT = $("#some-table").on("draw.dt", function () {
$(this).find(".dataTables_empty").parents('tbody').empty();
}).DataTable(/*init object*/);