Get ajax response inside dataTable ()

This is my existing pagination code here. I am getting an error because it is roleList

not defined.
I don't know how to use the ajax response internally dataTable()

. What have I done wrong? I have been amazed at this part for the last 3 days, however I have been looking for a solution for a long time but could not tell the difference.

function empRoles() {debugger
    table = $('#mydata').DataTable({
        "ajax": {
            "type": 'POST',
            "dataType": 'json',
            "url": '/Admin/getRolesList',
            "dataSrc": function (response) {
                var roleList = response;
                console.log(roleList)
            },
            "data": {
                "json": JSON.stringify(roleList)
            }

        },
        "columns": [{
            "data": "sNo"
        }, { 
            "data": "roleName",
            "className": "roleName"
        }, {
            "data": "roleName",
            "render": (data, type, row, meta) => `
                <button class="btn edit btn-info" id="edit${row.sNo}">
                    <i class="fa fa-pencil"></i>
                    Edit
                </button>
                <button class="btn update btn-success" id="update${row.sNo}">
                    <i class="fa fa-floppy-o"></i>
                    Update
                </button>
                <button class="btn dlt btn-danger" data-toggle="modal" data-target="#confirm" id="delete${row.sNo}">
                    <i class="fa fa-trash-o"></i>
                    Delete
                </button>
            `
            ,
            "sortable": false
        }],
        "createdRow": (row, data, dataIndex) => {
            $(row).attr("id", "row" + data.sNo);
            $('td:eq(1)', row).attr("id", "name" + data.sNo);
        }
    });
}

      

Any advice please.

+3


source to share


2 answers


If I understand the problem correctly, I believe the simplest solution would be to simply initialize the variable roleList

outside of the parameters.



function empRoles() {
    var roleList = []; // or whatever the initial data value should be
    var table = $('#mydata').DataTable({ 
    // ...
       "dataSrc": function (response) {
            roleList = response;
            console.log(roleList)
        },
        "data": {
            "json": JSON.stringify(roleList)
        }
    // ...

      

0


source


Piece of code

"data": {
            "json": JSON.stringify(roleList)
        }

      

performed before

"dataSrc": function (response) {
            roleList = response;
            console.log(roleList)
        }

      



Therefore the roleList variable is not available for ajax.data.

You can check this by putting breakpoints in your browser.

Here you are basically trying to access the input variable and pass it back to the server. The same can be done with your own custom ajax section using the functions mentioned in this link -

https://datatables.net/reference/option/ajax

0


source







All Articles