Show/Hide search based on received number of rows from server side

Show/Hide search based on received number of rows from server side

mvdgeijnmvdgeijn Posts: 2Questions: 1Answers: 0
edited September 2023 in Free community support

Hi,

I think I have a simple question, but somehow I can't find the answer. Most of the times the datatables table is used in my project, it only contains a few records (received from server side). I would like to hide the search, pagination and number of records panes when the number of records is below 10. Above 10 the search, pagination and number of records must be shown.

How can this be done? I have created a table.on('xhr', function{...}) event handler, but I don't know what I need to do in there. I've found the dom setting, but how do I change it dynamically and redraw the table when the total number of records is high?

I was also wondering if there is an event fired when the table receives data for the first time from the server side. This hiding/showing should only be done the first time the data is received. I can fix that by using a global variable off course.

Thanks!

Answers

  • mvdgeijnmvdgeijn Posts: 2Questions: 1Answers: 0

    For the time being I'm counting the number of rows at server side when the page is requested. Setting the sDom setting accordingly. It's a little overhead, but it works for now. If there is an easier way, please let me know.

  • kthorngrenkthorngren Posts: 21,449Questions: 26Answers: 4,975

    One option is to use initComplete which runs after Datatables initialization. You could return the row count from the server in the JSON response. You can access the JSON response with the second json parameter passed into the initComplete function. Or you could use rows() with chained count() to get the row count.

    You can use jQuery or Javascript to hide the div containing the elements to hide. Inspect the page to find the selectors to use. There is not an option to dynamically change the dom options.

    This example shows the rows().count() and hiding the info element.
    https://live.datatables.net/cigunuji/1/edit

    Kevin

Sign In or Register to comment.