Sorting without new ajax request possible?

Sorting without new ajax request possible?

countesscatcountesscat Posts: 21Questions: 7Answers: 0
edited November 2018 in Free community support

Hi, guys! I have a project with DataTables and it is mainly statistical numbers which stay the same, except for today's date. The table data is handled server-side. What I notice is that when data is loaded and when sorting (asc or desc) is applied, DataTables makes another ajax call to get the current data and then sorts. Well, in my case I would like to have sorting the currently displayed values by column without making another ajax request but just to do the sorting. Any idea how to achieve this?

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @countesscat ,

    There should only be a single serverSide request when the table is intialised which would also request the correct ordering. If that's not happening, it might be down to your config.

    We're happy to take a look. As per the forum rules, if you could link to a running test case showing the issue we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    when sorting (asc or desc) is applied

    Are you referring to the default sorting when the Datatable is initialized or sorting that takes place after initialization? If the sorting takes place after initialization then Datatables will send another request. You could use order to set the initial ordering of the table to eliminate the second request.

    Kevin

  • countesscatcountesscat Posts: 21Questions: 7Answers: 0
    edited November 2018

    Hi, @kthorngren! Thanks for your reply! I refer to sorting that takes place after initialization when clicking or tapping on the column name with up/down arrows. So I want to have server-side data loading via ajax but client-side sorting of the columns. I also have daterangepicker alongside DataTables and it would be best, if DataTables only reloads the data server-side on use of daterangepicker date range change or DataTables length per page select option change.

    @colin, I can link to a working example but the app is still in beta and registration is closed. I can create a test account for that purpose. Please let me know, if that would work and where to post it.

    Actually, I think that changing the length per page should be client-side, if possible, too.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    All those functions cause Datatables to draw the table. Each draw will result in Datatables sending an ajax request with the parameters described here:
    https://datatables.net/manual/server-side

    Since only the data displayed is at the client each sort, search or change of page length will result in Datatables fetching the updated data via ajax to the server.

    Kevin

  • countesscatcountesscat Posts: 21Questions: 7Answers: 0

    So how to make it make a draw only to load the data initially? It would make no sense to keep on requesting the same data server-side, only sorted or paginated differently. I also noticed that if a new draw is forced by some client interaction, each next request does not cancel the previous draws and after the last draw is finished, the previous ones keep on processing somehow in the background. Is there a way to cancel any previous request to the ajax file? Example: I sort by column 1 and before the results are returned via ajax, I click to sort by column 2. Both actions make a new draw. Results shown are the second draw results - sorted by column, 2 but the request for data sorted by column 1 still continues in the background. Check with network tab of DevTools, for example to verify.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @countesscat ,

    It sounds like you don't need serverSide, as this will always go to the server to get the data - the client only has the data that's visible.

    Instead, use ajax, this way the client requests all data from the server, and then the client does all sorting and filtering.

    Cheers,

    Colin

This discussion has been closed.