Client-side processing
Example
Highlights
JavaScript
-
Include “Load more” plug-in file
You need to include plug-in’s JavaScript file after loading jQuery and jQuery DataTables files as shown below:
<script type="text/javascript" src="//gyrocode.github.io/jquery-datatables-pageLoadMore/1.0.0/js/dataTables.pageLoadMore.min.js"></script>
-
Control elements
With
dom: 'frt'
I hide pagination, page length and information controls. These controls are not needed with “Load more” button.If you’re using Bootstrap, Foundation or other frameworks, see dom option for default string for your framework and remove characters
p
,l
andi
to hide these controls.
HTML
-
“Load more” button
Button is initially hidden with inline CSS style
display:none
. This helps to avoid flickering while resource files are being loaded.Later visibility of the “Load more” button is controlled based on availability of the additional data in a callback defined by drawCallback option.
You can use your own markup for the “Load more” button and style it as needed. Just remember to use button’s ID (
btn-example-load-more
in my example) in the JavaScript code.
Reloading Data
When you reload your data using ajax.reload() API method, number of records loaded on screen at once is preserved. If you want to reset table page length to default, use page.resetMore()
API method as shown below.
Server-side processing
Example
Highlights
Code for server-side processing mode is very similar to code used for client-side processing mode. Therefore everything that has been mentioned above also applies here as well.
There is only one notable difference. You need to use predefined function $.fn.dataTable.pageLoadMore
as a value for ajax option and specify URL to your server-side script using url
option.
For example:
ajax: $.fn.dataTable.pageLoadMore({
url: '/test/0'
}),
Optionally, you can specify request method using method
option and data to be sent to the server using data
option. For example:
ajax: $.fn.dataTable.pageLoadMore({
url: '/test/0',
method: 'POST',
data: {
extraParam: 'extraValue'
}
}),
Known Limitations
-
Plug-in expects data returned by the server-side script to be in
data
property. For example:{ "draw": 1, "recordsTotal": 57, "recordsFiltered": 57, "data": [ [ "Angelica", "Ramos", "System Architect", "London", "9th Oct 09", "$2,875" ], [ "Ashton", "Cox", "Technical Author", "San Francisco", "12th Jan 09", "$4,800" ], ... ] }
Also please note that I am using Mockjax plug-in to emulate server-side processing in my example. This plug-in (jquery.mockjax.min.js
) and corresponding code ($.mockjax({ ... })
) is not needed in production.
Thanks for the example, Michael!
After including plug-in’s JavaScript file to my WordPress site, I get the following error:
“Uncaught TypeError: Cannot set property ‘pageLoadMore’ of undefined at dataTables.pageLoadMore.min.js:5”
What could be the reason? How to fix it?
One of the possible reasons is that you need to include the script after jQuery and jQuery DataTables JavaScript files. If that doesn’t work, please provide an example demonstrating the problem. You can try loading full version of the script to determine the exact location of the error.
I added the script to functions.php and it should load after jQuery and jQuery DataTables files:
Here is the website: http://fridayinvestor.com/
You are loading
dataTables.pageLoadMore.min.js
before jQuery DataTables, that is why there is$.fn.dataTable is undefined
console error. Third argument forwp_enqueue_script()
can be used to specify the script pageLoadMore depends on.Perfect! Got it. Thank you, Michael!
Hello. How can i use search and filtering with load more and server-side processing ?
Artem, search with server-side processing seems to be broken, let me investigate why.
Just updated the repository on GitHub correcting issue with handling requests in server-side processing mode.
On click on Load More, I want to add some classes on the newly added data row and and its tds. Can you please help me out here
This is very good one. I am using ajax server side to load more data. However, I got issues with sorting column where it does not seem to work as expected. It would be great if the sorting works on client side. For server side, it refresh new data from server side during sorting, and it reset load more page to new data, not appended data.
I would like to use client side but loading data from ajax. Each time of clicking Load more button it should get new data from server and append to dataTable. However, all sorting, searching should be on client side, so they should not reload new data. Please help me on this.
Hi Michael,
Great tutorial and exactly what I needed. One quick question though. If I wish to add a fading in effect to the new table rows loading on button click how could I appraoch that?
Thanks you very much!
Best,
Paulo