Datatables pagination with AJAX

久未见 提交于 2020-03-02 10:16:53

问题


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:

  1. You set an url to your serversided code
  2. 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.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!