Datatables processes only one request

Asked

Viewed 186 times

3

So I’m making a system of custom selects with the datatable, only 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']."%' ";
}
  • Post code not images of them

  • The code is getting crazy, but I’ll see here

  • Okay, you got the code

  • If anyone can help

  • How are you getting started with datatables? According to the question, I imagine you are making use of server processing, so it is important to know how you initialized the datatables.

1 answer

1

Datatables expects a Json in response to requests.

And Datatable itself has a component that helps to manipulate the data through 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 https://datatables.net/examples/data_sources/server_side

Browser other questions tagged

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