Reputation: 11855
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"
});
});
Upvotes: 0
Views: 540
Reputation: 31
There are a few things you can/must do to set up your pagination.
All your columns must contain at least attribute "data":
var aoColumns = [ { "mData": "c1-data-attr", "sName": "c1-name-attr", "sClass": "c1-class-attr"}]
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;
};
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
);
Upvotes: -1