Jquery | Perform dynamic operation on load of Datatable

Author - Neha Mishra
1.03.2017
|
0 Comments
|

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);
            }
        }
    });
});
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *