How to Disable sorting when no records are fetched either by server-side processing or search?

How to Disable sorting when no records are fetched either by server-side processing or search?

venkateshsjvenkateshsj Posts: 3Questions: 1Answers: 0

A standing ovation to Allan, the man behind Datatables! I would also like to congratulate him for creating an awesomely beautiful tool for displaying tabular data which loads of features and functionalities to tweak the tool to our requirement.

I have used Datatables for the first time and have been fairly successful in tweaking to customize as follows,
1. Info - display in format "Displaying 'x of y pages | Total x row(s) | Fetched in 'a' second(s)"
2. Tweak dom to display export buttons + info + pagination in single row
3. If not records are displayed then hide export buttons + info + pagination
4. Display "No records to display." message row in case of no records
5. Custom filtering on button click
6. Edit link for a column to edit details in popup
7. Custom Reload button to refresh the datatable
8. Block datatable while server-side processing takes place, so user cannot interfere with the processing
9. Custom export PDF/Print logic to export server-side data with logic to display time required to export
10. Serial no column without sorting as first column

Now I am stuck at 1 final point. I am using DataTables 1.10.11. I have implemented it in my MVC 5 project using server-side fetching json data. There are certain columns on which sorting is required and has been provided. I have this requirement that if no records are fetched, either from server-side or search, then disable column sorting to avoid unnecessary round-trips to the server.

I searched the forums but did not find a working solution. I had read in some post where Allan had mentioned clearly that once datatables has been init the settings cannot be changed. So can anyone help me how I will be able to implement the same. Its a fairly clear requirement and can be generated for any table with static data too.

I would be very grateful if someone could provide me with the code to achieve the same or a link where it already exists, maybe I might have missed it.

Thanks a lot in advance.

Answers

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    Hi,

    Thanks for your kind words.

    If you are using server-side processing, then you wouldn't know that there are no records until you hit the server anyway would you not? You don't need a second Ajax request to order the data, so I'm not sure I understand what the benefit is?

    Allan

  • venkateshsjvenkateshsj Posts: 3Questions: 1Answers: 0

    Hi Allan,

    Thanks for your reply. I think I have not made my requirement clear or maybe I have not understood your answer clearly.

    Either way, I will again explain what I want. You are right when you mentioned that I will not know how many records are being fetched until I hit the server. That is absolutely fine with me. My requirement is that of post Ajax. Once I come to know that 0 records are being fetched I want to disable sorting on all columns. Sorting should be enabled only when there are 2 or more records to be displayed else sorting should be disabled as there is no point in sorting the data when there is 1 or 0 records to display.

    I hope I have made my requirement more clear now. This is something I am not understanding as to how to achieve. Is there any event or function which could help me achieve this?

    Again, thanks in advance..... :)

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    Hi,

    Thanks for your reply. I think I understand now - you basically want to reduce the load on the server a little by not making requests when there is no data? Unfortunately there is no option to do that in DataTables at the moment.

    This is actually intentional since you might add data on the server-side and then a click to sort would need to have effect (which the client-side wouldn't know about).

    Allan

  • venkateshsjvenkateshsj Posts: 3Questions: 1Answers: 0

    Hi Allan,

    Thanks for your reply.

    I somehow do not agree with your answer. Even if data is added on the server-side, user will be required to initiate an action to fetch that data. Further if no records are being displayed, then user anyways will not try and click on column sorting to check if any new data is available or not. So it does not make any sense.

    I feel this is a missing functionality that should have been available. If at all you do not agree with my suggestions, then could you at least guide/hint me in adding such a functionality in my project? Your help could save me a lot many hours of trying to understand the source code. I am not so proficient in jQuery.

    Thanks in advance.

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    Even if data is added on the server-side, user will be required to initiate an action to fetch that data

    If it is server-side processing you are using, then a sort, filter or any other action that results in a table draw would be that action. That is how server-side processing is designed to work.

    The only way to have it not make the Ajax request would be to use ajax as a function. You could then have a logic condition to check if the table has data in it or not - if it doesn't then just return an empty array of data. If it does, make a $.ajax call to get the required data.

    Having this done automatically is not something I plan to build into DataTables at this time.

    Allan

  • SamirKhatriSamirKhatri Posts: 1Questions: 0Answers: 0

    Allan,

    I have same request, where there is no record returned in json data, user still has option to play with pagination, search and sorting. Is it possible to hide those features from user?

    Thanks,
    Samir

This discussion has been closed.