Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
Get ajax response inside dataTable()

This is my existing code for pagination here I am getting error as roleList is not defined.
I don't know how to use ajax response inside of dataTable().What I did wrong? I got struck with this part for last 3 days,however I searched for a solution for long time,but couldn't fine one.

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..

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If I'm correctly understanding the problem, I believe the simplest solution would be to simply initialize the roleList variable outside of the options.

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)
        }
    // ...
about 4 years ago · Santiago Trujillo Relatório

0

The part of code

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

is executed before

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

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

You can validate this by putting break points on your browser.

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

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda