php - Jquery DataTable serverside rendering and pagination error in front end -
i using php
, query select * table
, result want show in jquery
datatable
in server side processing means want use jquery
datatable
's own ajax
method. have total 58 rows. default should have 6 page. 10 rows per page. data coming , shown in table. wrong pagination.
actually shows 58 rows in first page , showing 6 pagination buttons. if click on nothing working. searching , sorting not working.
i not pasting sql
code here pasting procedural code of php
here.
php
code:
require_once("logic/logsdatalogic.php"); $ldl = new logsdatalogic(); $data = $ldl->getdatabyfromdatetodate(); $arr = [ "draw" => $_post["draw"], "recordstotal" => count($data), "recordsfiltered" => count($data), "data" => $data ]; echo json_encode($arr);
jquery
code:
$(document).ready(function(){ $('#logs_table').datatable( { "processing": true, "serverside": true, ajax: { url: '../app/getdata.php', type: "post" } } ); });
html
code:
<link href="https://cdn.datatables.net/1.10.15/css/jquery.datatables.min.css" type="text/css" rel="stylesheet"> </head> <body> <table class="display" cellspacing="0" width="100%" id="logs_table"> <thead> <tr> <th>id</th> <th>user id</th> <th>login time</th> <th>ip</th> </tr> </thead> <tfoot> <tr> <th>id</th> <th>user id</th> <th>login time</th> <th>ip</th> </tr> </tfoot> </table> <!-- end here --> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://cdn.datatables.net/1.10.15/js/jquery.datatables.min.js"></script>
i have attached image of final result.table image here
sol 1:
if dont want pagination server set "serverside": false, bring record ie 58 @ 1 go pagination work.
sol 2:
if want pagination server side means fetch 10 record each time follow these steps
you have added "serverside": true, when console along other parameters some additional parameters passed. need handle in server side query limit 20,10
refer: https://datatables.net/examples/server_side/simple.html
eg:
draw:4 columns[0][data]:0 start:20 length:10 search[value]: search[regex]:false
explanation:
when set serverside false
datatable gets records server(in case 58) , paginate in ui suggested(10 rec each page) when chnage page no ajax call fired
when set serverside true
datable gets first 10 record , when change page ajax call fired set of 10 records. datatable adds parameters along request , expect handle in server side eg: first record , total no of record per set , total no of record available
so each request start , length passed no of records index
in response datable expect "draw":2,"recordstotal":57,"recordsfiltered":57 along data
draw unique key can use request recordstotal no no of record : need additiona query count of records
Comments
Post a Comment