add custom datatables column button

Asked

Viewed 1,084 times

3

I’m trying to add a button with the id from user to serverside datatables I have made several unsuccessful attempts, if anyone can help me I appreciate it. This is my datatables code:

    $('#jsontable').dataTable({
"oLanguage": {
  "sUrl": "busca/pt-br.txt"
},
        "responsive": true,
        "processing": true,
        "serverSide": true,
        "ajax": "busca/busca_usuario.php"
});

This is the array of the search:

$columns = array(
    array( 'db' => '`a`.`nome`',         'dt' => 0, 'field' => 'nome' ),
    array( 'db' => '`a`.`cpf`',          'dt' => 1, 'field' => 'cpf' ),
    array( 'db' => '`a`.`situacao`',     'dt' => 4, 'field' => 'situacao'),
    array( 'db' => '`b`.`descricao`',    'dt' => 2, 'field' => 'descricao' ),
    array( 'db' => '`c`.`descricao`',    'dt' => 3, 'field' => 'nomegh', 'as' => 'nomegh' ),

);

That’s how the data comes in:

{"draw":1,"recordsTotal":28,"recordsFiltered":28,"data":[{"0":"ADRIANO TESTE","1":"0000000000","4"
:"ativo","2":"Aluno","3":"Aluno 1ª série"}]}

I was able to add the link to the column just to get the user id:

I leave my attempt to help others with the same doubt:

    $('#jsontable').dataTable({
"oLanguage": {
  "sUrl": "busca/pt-br.txt"
},
        "responsive": true,
        "processing": true,
        "serverSide": true,
        "ajax": "busca/busca_usuario.php",
         "aoColumnDefs" : [
            {"data": null, "sDefaultContent": "<a href=teste.php?id=' + data[0] + '>teste</a>","aTargets": [5]}
        ]
});

1 answer

3


Good I want to put here the solution so that another user with the same problem gets a solution.

first in search I added the id of the user in id_usuario as you can see below:

$columns = array(
    array( 'db' => '`a`.`id_usuario`',   'dt' => 0, 'field' => 'id_usuario' ),
    array( 'db' => '`a`.`nome`',         'dt' => 1, 'field' => 'nome' ),
    array( 'db' => '`a`.`cpf`',          'dt' => 2, 'field' => 'cpf' ),
    array( 'db' => '`a`.`situacao`',     'dt' => 5, 'field' => 'situacao'),
    array( 'db' => '`b`.`descricao`',    'dt' => 3, 'field' => 'descricao' ),
    array( 'db' => '`c`.`descricao`',    'dt' => 4, 'field' => 'nomegh', 'as' => 'nomegh' )

);

Then in the call of datatables I added the column with the buttons and hid the column with the Ids pq I don’t care if it appears see below:

    $('#jsontable').dataTable({
"oLanguage": {
  "sUrl": "busca/pt-br.txt"
},
        "responsive": true,
        "processing": true,
        "serverSide": true,
        "ajax": "busca/busca_usuario.php",
  "aoColumnDefs": [    
        {
        "bSearchable": false,
        "bVisible": false,
        "aTargets": [0] // aqui é a coluna do id como é a primeira é 0
        },  
     {
       "aTargets": [ 6 ], // o numero 6 é o nº da coluna
       "mRender": function ( data, type, full ) { //aqui é uma funçãozinha para pegar os ids
         return '<a class="editar fancybox.iframe" href="cadastro_editar.php?id=' + full[0] + '"><img src="imagens/editar.png" width="24px" height="24px" border=0  title="Editar usuário"/></a><a class="ficha fancybox.iframe" href="fichapessoal_editar.php?id=' + full[0] + '"><img src="imagens/atualiza.png" width="24px" height="24px" border=0  title="Editar ficha pessoal"/></a>';
       }
     }
   ]
});
  • Thank you so much opeta a while ago I was with this problem and your solution saved me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.