add custom button column of datatables

3

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

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

This is the array of user-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' ),

);

The data arrives like this:

{"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 only to be able 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]}
        ]
});
    
asked by anonymous 16.12.2015 / 01:52

1 answer

3

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

first in user_quest I added the id of the user in id_user 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 datatables call I added the column with the buttons and hid the column with the IDs pq I do not 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>';
       }
     }
   ]
});
    
16.12.2015 / 04:06