Datatables processes only one request

3

So, I'm making a custom selects system with the datatable, but the request only processes once.

Example of my php, js and html

<div class="input-group has-feedback" id="divCidade">
  <div class="input-group-addon">
         <i class="fa fa-envelope"></i>
   </div>
   <input type="text" class="form-control input-group txtCidade" name="txtCidade" placeholder="Ex.: Belo Horizonte" id="txtCidade" autocomplete="off" maxlength="50" data-columns="10">
</div>

JS

$("#btnFiltro").on('click',function() {
    if($(".chkCartaCancelada").is(':checked')) {
       var i =$(this).attr('data-columns');//pega o valor da coluna definida para ele
       var v =$('.chkCartaCancelada').val();//pega o valor do mesmo
       dataTable.columns(i).search(v).draw();//faz a pesquisa dentro do datatable
       console.log(v);
    }
});
$("#btnFiltro").on('click',function() {
    if($(".txtCidade").val() != "") {
       var i =$('.txtCidade').attr('data-columns');//pega o valor da coluna definida para ele
      var v =$('.txtCidade').val();//pega o valor do mesmo
      dataTable.columns(i).search(v).draw();//faz a pesquisa dentro do datatable
      console.log(v);
    }

});

PHP

if(!empty($requestData['columns'][6]['search']['value'])){
     $sql.=" AND status_atual LIKE '".$requestData['columns'][5]['search']['value']."%' ";
}
if(!empty($requestData['columns'][10]['search']['value'])){
     $sql.=" AND cidade LIKE '".$requestData['columns'][10]['search']['value']."%' ";
}
    
asked by anonymous 31.03.2017 / 20:26

1 answer

1

DataTables expects a Json in response to requests.

And DataTable itself has a component that helps you manipulate the data through a request.

$table = 'datatables_demo';


// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The 'db' parameter represents the column name in the database, while the 'dt'
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'first_name', 'dt' => 0 ),
    array( 'db' => 'last_name',  'dt' => 1 ),
    array( 'db' => 'position',   'dt' => 2 ),
    array( 'db' => 'office',     'dt' => 3 ),
    array(
        'db'        => 'start_date',
        'dt'        => 4,
        'formatter' => function( $d, $row ) {
            return date( 'jS M y', strtotime($d));
        }
    ),
    array(
        'db'        => 'salary',
        'dt'        => 5,
        'formatter' => function( $d, $row ) {
            return '$'.number_format($d);
        }
    )
);

// SQL server connection information
$sql_details = array(
    'user' => '',
    'pass' => '',
    'db'   => '',
    'host' => ''
);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
)

Reference link

    
23.12.2018 / 12:58