Disable column sorting/ordering until data is in a table.
Disable column sorting/ordering until data is in a table.
Hey everyone.
I have a datatable that is using server-side processing and defered rendering. On my page, above the table, I had some inputs for filtering first name, last name and an organization name. There is also a search button on the page. When the page is first loaded I display no data, forcing the user to click the search button. I do an ajax call whenever the search button is clicked by calling oTable.api().ajax.reload(). Datatables will call the ajax when the pagination or column headers are clicked. Both of those cases will return data (or not if there's nothing to return).
Unfortunately, I received an issue where a user clicked a column header on an empty table and that automatically made a call to the server. Because of this I was given a new requirement -- "Disable column sorting until the table has data." Is this even possible? I know column sorts are set during initialization and the only way I can imagine this is to reinit the table... but, I imagine that's going to have a slew of issues since my datatable is set up for serverside processing.
Has anyone tried this before?
Answers
This is a good question. I also have a similar requirement. I need to disable automatic ajax redraw on empty tables. Let me know if you find an answer or a workaround.
Why not disable table sorting initially, https://datatables.net/reference/option/ordering . Then after your Ajax call, using https://datatables.net/reference/event/xhr , check to see if any data returned. If so, enable table sorting via the 'settings' variable in the example. If no data, disable sorting. Alternatively, you could always disable table sorting before making Ajax call using https://datatables.net/reference/event/preXhr .