Functionality needed : Once the datatable is completely loaded using ajax, I want to show second column only if role of the logged-in user is admin, else hide it.
What we need to do is, call API “initComplete” in datatable’s callback .
Do something like below :
$(document).ready(function() {
$('#example').DataTable({
"language": {
"loadingRecords": " "
},
"ajax": {
"url": "fetchListByAjax.action",
cache: false
},
"type": "POST",
"processing": true,
"serverSide": false,
"columns": [{
"data": "idTd"
}, {
"data": "name"
}, {
"data": "source"
}, {
"data": "time"
}, {
"data": "status"
}, ],
"autoWidth": false,
"aaSorting": [
[0, 'desc']
],
language: {
searchPlaceholder: "type any keyword..."
},
"initComplete": function(settings, json) {
api.search(this.innerHTML).draw();
if (' ' == "admin") {
$('td:nth-child(2)').show();
} else {
table.columns([1]).visible(false);
}
}
});
});
