简体   繁体   中英

Datatables how to search and paginate server side

I have a Java rest api which I can get paginated results like this:

/allusers?page=1&text=searchkeyword&pageSize=50

I am trying to implement the frontend using datatables(also open to other library suggstions) But cant seem to figure out how do do I send the page and search keywords using Datatables server side processing:

This is the example code from their website, how do I pass the parameters here? So when user clicks next page or search for a keyword it makes the relavent backend call:

$(document).ready(function() {
  $('#example').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "../server_side/scripts/server_processing"
  });
});

There are a few things you can/must do to set up your pagination.

  1. All your columns must contain at least attribute "data":

     var aoColumns = [ { "mData": "c1-data-attr", "sName": "c1-name-attr", "sClass": "c1-class-attr"}] 
  2. Defining a dataSrcFunction, which will automatically receive as a parameter the JSON with your paginated table from your API response. The return (json.data) shall contain the values for each column defined in aoColumns by their mData atributte value.

     var dataSrcFunct = function (json) { console.log(json); //manipulate your JSON here return json.data; }; 
  3. Defining an errorFunction that will execute when the response is not OK.

These parameters will go in your configuration like this:

$('#example').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": { 
            "url": "/allusers?page=1&text=searchkeyword&pageSize=50",
            "dataSrc": dataSrcFunction,
            "error": errorFunction
        },
        "aoColumns": aoColumns
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM