问题
Is it possible to set datatables to paginate, but load only the n amount of entries that are displayed from the server? (via Ajax request).
In other words, each time you re-sort or click 'Next n entries', a small request is made to the server to load the new entries to display. Thus minimizing the initial load time.
Will I be able to do this via the initialization, so that it applies to all datatables in my site?
回答1:
Since you're not providing any code I won't be able to do a full answer. However, you should create a jquery plugin :
(psedo code)
$.fn.tablePagination = function(option) {
var self = this;
$.ajax({
url: option.url
success: function(data){
self.html(createTable(data));
})
});
}
Then use it like :
$(".tabable").tablePagination({url: "your/server/tableId"});
回答2:
Of course, this is the cool thing with jquery-dataTables.
Look at this example (targeted at PHP and MySQL).Server sided Data with sorting and pagination
This works out of the box. A you have to do is to enter your MySQL fields into this PHP line:
$aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
If you are using Node/mongoDB or any other server sided script you will have to write your own code. But basically:
- You set an url to your serversided code
- You fetch data from any datasource (db, json file or else) clamped by the values dataTables sets in the request(actual_Pagenumber, Items_per_page, sorting for columns, filtering). Just use the firebug console to look at a typical request.
- You return json-encoded Data to your client (In PHP/MySql by
echo json_encode( $output );
. In node/mongo just the result of your query.
来源:https://stackoverflow.com/questions/22194144/datatables-pagination-with-ajax