Datatables ColVis JS HTML Columns
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/jszip-2.5.0/dt-1.10.20/af-2.3.4/b-1.6.1/b-colvis-1.6.1/b-html5-1.6.1/cr-1.5.2/kt-2.5.1/r-2.2.3/sc-2.0.1/sl-1.3.1/datatables.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/v/bs4-4.1.1/jq-3.3.1/jszip-2.5.0/dt-1.10.20/af-2.3.4/b-1.6.1/b-colvis-1.6.1/b-html5-1.6.1/cr-1.5.2/kt-2.5.1/r-2.2.3/sc-2.0.1/sl-1.3.1/datatables.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th class="show">Name</th>
<th class="show">Position</th>
<th class="hide">Office</th>
<th class="hide">Age</th>
<th class="show">Start date</th>
<th class="show">Salary</th>
</tr>
</thead>
</table>
JavaScript
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
colReorder: true,
buttons: {
dom: {
button: {
tag: 'button',
className: '',
}
},
buttons: [
{
extend: 'colvis',
columns: [2, 3, 4, 5],
titleAttr: 'Columns can be hidden or shown by toggling them here.',
text: `<span>Columns</span>`,
columnText: function(dt, idx, title) {
return `<div style="display:flex">${title}</div>`;
},
}
]
},
} );
} );