How to make CRUD buttons in datatables?

2

I am doing server-side activation and want to know how to make some buttons from a basic CRUD, in the last row, type like this:

Mycode

<scripttype="text/javascript" language="javascript" class="init">

    $(document).ready(function() {
        $('#example').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": "scripts/server_processing.php",

            "order": [[ 0, "asc" ]]
        } );
    } );

    </script>

And server_processing.php :

<?php
$table = 'tbl_medic';
$primaryKey = 'id_Medic';

$columns = array(
    array( 'db' => 'nome', 'dt' => 0 ),
    array( 'db' => 'cidade',  'dt' => 1 ),
    array( 'db' => 'cep',   'dt' => 2 ),
    array( 'db' => 'crm',     'dt' => 3 ),
    array( 'db' => 'email', 'dt' => 4 ),
    array( 'db' => 'pastaDocumentos',     'dt' => 5 )
);

$sql_details = array(
    'user' => '***', //Usuário do banco de dados
    'pass' => '***', //Senha do banco de dados
    'db'   => '***', //Banco de dados
    'host' => 'localhost'
);

require_once( 'ssp.class.php' );
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
    
asked by anonymous 29.06.2017 / 20:56

1 answer

2

I can not guarantee it's the best way, but you can use columnDefs to accomplish this task:

$('#').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "scripts/server_processing.php",
    columns: [...],
    "columnDefs": [
        {
            "targets": 3, //"Número referente a coluna, startando no 0"
            "render": function (data, type, row) {
                //Aqui tem um callback onde pode retornar o botão
                //row - aqui você possui todos os atributos da sua linha
                //Basta criar seu botão e como string e retornar;

                var deleteBtn = '<a type="button" href="/action/metodo/' + row.id + '" ></a>'     
                return deleteBtn;
            }
        }
    ]
});

To get more organized the generation of the buttons, I advise you to create a factory function to do the service:)

    
29.06.2017 / 21:10