Searching, sorting and pagination not working in server side datatable
Searching, sorting and pagination not working in server side datatable
Hi,
I have implemented server side datatable, with django and mysql. It is editable and everything is working fine, but searching sorting and pagination(unable to limit number of rows per page) seems not working now. While checking in console on searching following error is coming
this._iRecordsDisplay NaN (In browser console).
This is the example which i have used :: "https://github.com/twtrubiks/DRF-dataTable-Example-server-side"
I am using this with django rest framework, shall i need to do something in server side or what could be the reason for this
Following is html code
id | Port Name | Discharge Capacity | Days | Discharge Cost | Action |
---|---|---|---|---|---|
Following is js code
let table = $('#datatables').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "http://localhost:8000/master-data/dis-port-capacity",
"type": "GET"
},
"columns": [
{"data": "id"},
{"data": "port_name"},
{"data": "discharge_capacity"},
{"data": "days"},
{"data": "discharge_cost"},
{
"data": null,
"defaultContent": '<button type="button" class="btn btn-info">Edit</button>' + '  '
}
]
});
Kindly help anyone it is really urgent !!
This question has an accepted answers - jump to answer
Answers
With server side processing enabled the server script (your django code in this case) is expected to perform the search, sort and paging functions and just return the proper rows for the page being displayed. The server script is expected to handle this protocol:
https://datatables.net/manual/server-side
It would be easier for you to use client side processing if your data set size allows. See this FAQ:
https://datatables.net/faqs/index#speed
There appear to be some third party Django/Datatables libraries you can try:
https://pypi.org/project/django-datatables-view/
https://github.com/izimobil/django-rest-framework-datatables
I haven't used them but it might be easier than building your own server side code.
Kevin
Sorry i am not able to get your answer ... exactly what I am missing here do i need to make changes in django side. I am not able to do following things ::
In browser console this error is coming this._iRecordsDisplay NaN
Yes. When you enable server side processing the server script is responsible for handling those functions.
Kevin
Okay so for every one of them I have to do changes at django side. ?
Yes. Take a look at the
serverSide
docs. It states:The question is do you need server side processing? To help answer that please see the FAQ I posted above.
If you are able to use client side data then Datatables will take care of the sorting, search and paging.
Kevin
what is client side data i didn't get that actually little elaborate
can u provide me any resource to do such ....
Here is an example of client side processing:
https://datatables.net/examples/data_sources/ajax.html
Click the ajax tab to see the data that is returned, 57 rows. All the data is in the client and Datatables will perform the search, sort and paging functions. There are no requests sent to the server when searching, sorting or paging.
Compare that to this server side processing example:
https://datatables.net/examples/data_sources/server_side.html
Click on the ajax tab and you see just 10 rows returned. In this case Datatables expects the server script to perform the search, sort and paging. Perform a search or sort and you will see the ajax tab updated with the new data for the page.
Server side processing is used for very large data sets to help with performance issues.
Kevin
Try commenting out the
"serverSide": true,
line and see how it behaves. Do you have performance issues?Kevin