Data Table
$(document).ready(function() {
var advance = $('#advanced-table').DataTable( {
dom: 'B<"clear">lfrtip',
"processing": true,
"serverSide": true,
"ajax":{
url :"server_processing.php ", // json datasource
type: "post", // method , by default get
},
mark: true,
columnDefs: [
{
targets: 1,
className: 'noVis',
},
{
"targets": [ 6 ],
"visible": false
},
{
"targets": [ 7 ],
"visible": false
}
],
buttons: {
name: 'primary',
buttons: [ 'copy', 'csv', 'excel', 'pdf', 'print', 'colvis' ]
},
//"language": {
//"url": "http://cdn.datatables.net/plug-ins/1.10.13/i18n/Portuguese-Brasil.json"
// }
} );
$('a.toggle-vis').on( 'click', function (e) {
e.preventDefault();
// Get the column API object
var column = advance.column( $(this).attr('data-column') );
// Toggle the visibility
column.visible( ! column.visible() );
} );
// Setup - add a text input to each footer cell
$('#advanced-table tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<div class="md-input-wrapper"><input type="text" class="md-form-control" placeholder="Pesquisar '+title+'" /></div>' );
} );
// Apply the search
advance.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
} );
server_processing.php
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "entt";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0 =>'ID',
1 =>'Nome',
2 => 'E-mail',
4 => 'Cidade',
5 => 'Estado',
6 => 'Cep',
7 => 'Data',
8 => 'Hora',
9 => 'Status'
);
// getting total number records without any search
$sql = "SELECT id, email, nome, cidade, estado, cep, data, hora ";
$sql.=" FROM usuarios";
$query=mysqli_query($conn, $sql) or die("server_processing.php: get usuarios");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT id, email, nome, cidade, estado, cep, data, hora ";
$sql.=" FROM usuarios WHERE 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( id LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR nome LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR email LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR cidade LIKE '".$requestData['search']['value']."%' )";
$sql.=" OR estado LIKE '".$requestData['search']['value']."%' )";
$sql.=" OR cep LIKE '".$requestData['search']['value']."%' )";
$sql.=" OR data LIKE '".$requestData['search']['value']."%' )";
$sql.=" OR hora LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("server_processing.php: get usuarios");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("server_processing.php: get usuarios");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["id"];
$nestedData[] = $row["nome"];
$nestedData[] = $row["email"];
$nestedData[] = $row["cidade"];
$nestedData[] = $row["estado"];
$nestedData[] = $row["cep"];
$nestedData[] = $row["data"];
$nestedData[] = $row["hora"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
?>
DataTables warning: table id=advanced-table - Requested unknown parameter '8' for row 0, column 8. For more information about this error, please see http://datatables.net/tn/4