drawCallback( settings )function Unable to remove / hide server side paginated DataTable columns
drawCallback( settings )function Unable to remove / hide server side paginated DataTable columns
Hi Allan
i got a server side paginated page which is working fine. This data table can do paginating, sorting and individual column searching. now i need to hide one or more columns depending on DOM conditions. when the dataTable load first time it hides the required columns but when we do individual search then the hide columns are showing. so i use "drawCallback": function (settings) {}. unfortunately it is not removing my columns. i am giving you my code below:
``` $(document).ready(function () {
$('#promoters thead tr#filterrow th').each(function () {
var title = $('#agents thead th').eq($(this).index()).text();
$(this).html('<input type="text" onclick="stopPropagation(event);" placeholder="Search ' + title + '" />');
});
var EnrolTreeIns= $('#promoters').DataTable({
"bServerSide": true,
"sAjaxSource": "@Url.Action("----", "-----")",
"bProcessing": true,
"type": "POST",
"pagingType": "full_numbers",
"sServerMethod": "POST",
"searchable": "true",
"sScrollX": "100%",
"bAutoWidth": false,
"autoWidth": false,
"orderCellsTop": true,
"overflow": "hidden",
"aoColumns": [
{ "sName": "PromoterId", "sClass": "hide_column" },
{ "sName": "EnrollDate" },
{ "sName": "Level" },
{ "sName": "First Name" },
{ "sName": "Last Name" },
{ "sName": "Website"
],
"drawCallback": function (settings) {
if ($("#promotercode").val() == "8") {
$('#promoters').on('init.dt', function () {
$('td:nth-child(4)').hide();
});
};
if ($("#promotercode").val() == "7") {
$('#promoters').on('init.dt', function () {
$("td:nth-child(4)").remove();
$('td:nth-child(3)').remove();
});
};
},
});
// this section is respinsible for individual column search
$(".dataTable thead th input").on('keyup change', function () {
EnrolTreeIns.column($(this).parent().index() + ':visible')
.search(this.value)
.draw();
});
function stopPropagation(evt) {
if (evt.stopPropagation !== undefined) {
evt.stopPropagation();
} else {
evt.cancelBubble = true;
}
}
if ($("#promotercode").val() == "8") {
$('#promoters').on('init.dt', function () {
$('td:nth-child(26)').remove();
});
};
if ($("#promotercode").val() == "7") {
$('#promoters').on('init.dt', function () {
$("td:nth-child(25)").remove();
$('td:nth-child(26)').remove();
});
};
}); ```