Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dataTables showing only one page when more than x records - ajax

Tags:

javascript

I came across this prolem when using datatables to display records via an ajax call.

However many results I choose per page the pagination only shows one page even when not all records are displayed.

So, with 12 possible records and 5 records per page, the table displays

"Displaying 5 of 5 records (filtered from a possible 12)". 

but there is only 1 page, not 3 in the pagination section.

iDisplayLength   is 5

in the json response

iTotalRecords   12
iTotalDisplayRecords    5

Why is it not showing the 3 pages it should?

like image 554
Tofuwarrior Avatar asked Jan 18 '14 07:01

Tofuwarrior


1 Answers

The problem was my misunderstanding of how to apply the paging server side and what value to return for iTotalDisplayRecords.

iTotalDisplayRecords is NOT the number of records being displayed on the page, it is the number of records that would be displayed, after filtering, if there was no pagination.

I was calculating iTotalDisplayRecords as the number of records being returned for each page (in this case 5) what resolved the problem was calculating this number BEFORE applying limit and offset but after any other filters (in my case date range and result type)

Hope this helps someone.

like image 150
Tofuwarrior Avatar answered Oct 19 '22 09:10

Tofuwarrior