Searching, sorting and pagination not working in server side datatable

Searching, sorting and pagination not working in server side datatable

aditya14paditya14p Posts: 8Questions: 1Answers: 0
edited May 2019 in Free community support

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>' + ' &nbsp'
}
]
});

Kindly help anyone it is really urgent !!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954

    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

  • aditya14paditya14p Posts: 8Questions: 1Answers: 0

    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 ::

    1. Limit record per page
    2. Unable to sort
    3. Unable to search
    4. Pagination not working.

    In browser console this error is coming this._iRecordsDisplay NaN

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954
    Answer ✓

    Sorry i am not able to get your answer ... exactly what I am missing here do i need to make changes in django side.

    Yes. When you enable server side processing the server script is responsible for handling those functions.

    Kevin

  • aditya14paditya14p Posts: 8Questions: 1Answers: 0

    Okay so for every one of them I have to do changes at django side. ?

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954
    edited May 2019

    Yes. Take a look at the serverSide docs. It states:

    Client-side processing - where filtering, paging and sorting calculations are all performed in the web-browser.
    Server-side processing - where filtering, paging and sorting calculations are all performed by a server.

    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

  • aditya14paditya14p Posts: 8Questions: 1Answers: 0

    what is client side data i didn't get that actually little elaborate

  • aditya14paditya14p Posts: 8Questions: 1Answers: 0

    can u provide me any resource to do such ....

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954

    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

  • kthorngrenkthorngren Posts: 21,345Questions: 26Answers: 4,954

    Try commenting out the "serverSide": true, line and see how it behaves. Do you have performance issues?

    Kevin

This discussion has been closed.