jquery - How to implement YADCF into a server side DataTable -
i trying add column filters server side datatables.js request (the database using huge). when add lines yadcf, have no functionality of text boxes. proper way add column filters php file? have: index.php
<head> <link rel="stylesheet" type="text/css" href="css/jquery.datatables.css"> <script type="text/javascript" language="javascript" src="js/jquery.js"></script> <script type="text/javascript" language="javascript" src="js/jquery.datatables.yadcf.js"></script> <script type="text/javascript" language="javascript" src="js/jquery.datatables.js"></script> <script type="text/javascript" language="javascript" > $(document).ready(function() { 'use strict'; var datatable = $('#employee-grid').datatable( { "processing": false, "serverside": true, "ajax":{ url :"employee-grid-data.php", // json datasource type: "post", // method , default error: function(){ // error handling $(".employee-grid-error").html(""); $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">no data found in server</th></tr></tbody>'); $("#employee-grid_processing").css("display","none"); } } } ); } ); </script> <style> div.container { margin: 0 auto; max-width:760px; } div.header { margin: 100px auto; line-height:30px; max-width:760px; } body { background: #f7f7f7; color: #333; font: 90%/1.45em "helvetica neue",helveticaneue,verdana,arial,helvetica,sans-serif; } </style> </head> <body> <div class="header"></h1></div> <div class="container"> <table id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%"> <thead> <tr> <th>employee name</th> <th>salary</th> <th>age</th> </tr> </thead> </table> </div></body></html>
and sql request file: employee-grid-data.php
<?php /* database connection start */ $servername = "localhost"; $username = "root"; $password = ""; $dbname = ""; $conn = mysqli_connect($servername, $username, $password, $dbname) or die("connection failed: " . mysqli_connect_error()); /* database connection end */ // storing request (ie, get/post) global array variable $requestdata= $_request; $columns = array( // datatable column index => database column name 0 =>'employee_name', 1 => 'employee_salary', 2=> 'employee_age' ); // getting total number records without search $sql = "select employee_name, employee_salary, employee_age "; $sql.=" employee"; $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: employees"); $totaldata = mysqli_num_rows($query); $totalfiltered = $totaldata; // when there no search parameter total number rows = total number filtered rows. $sql = "select employee_name, employee_salary, employee_age "; $sql.=" employee 1=1"; if( !empty($requestdata['search']['value']) ) { // if there search parameter, $requestdata['search']['value'] contains search parameter $sql.=" , ( employee_name '".$requestdata['search']['value']."%' "; $sql.=" or employee_salary '".$requestdata['search']['value']."%' "; $sql.=" or employee_age '".$requestdata['search']['value']."%' )"; } $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: employees"); $totalfiltered = mysqli_num_rows($query); // when there search parameter have modify total number filtered rows per search result. $sql.=" order ". $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 asc/desc */ $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: employees"); $data = array(); while( $row=mysqli_fetch_array($query) ) { // preparing array $nesteddata=array(); $nesteddata[] = $row["employee_name"]; $nesteddata[] = $row["employee_salary"]; $nesteddata[] = $row["employee_age"]; $data[] = $nesteddata; } $json_data = array( "draw" => intval( $requestdata['draw'] ), // every request/draw clientside , send number parameter, when recieve response/data first check draw number, sending same number in draw. "recordstotal" => intval( $totaldata ), // total number of records "recordsfiltered" => intval( $totalfiltered ), // total number of records after searching, if there no searching totalfiltered = totaldata "data" => $data // total data array ); echo json_encode($json_data); // send data json format ?>
i tried combine datatable function yadcf function found at; http://yadcf-showcase.appspot.com/dom_ajax_multiple_1.10.html . however, not column filters work. proper syntax yadcf server side request? or there changes jquery.datatables.js or jquery.datatables.yadcf.js must make?
i tried create variable otable still not yadcf.init(otable) make text boxes functional. missing?
thank help.
please see yadcf - server side source example. each column filter sends entered value columns[x][search][value]
, x
zero-based column index.
so in example 3 columns in server-side script have column filters in following variables:
$requestdata['columns'][0]['search']['value']
$requestdata['columns'][1]['search']['value']
$requestdata['columns'][2]['search']['value']
you need adjust php take account these filters if content not empty string.
Comments
Post a Comment