Show/Hide search based on received number of rows from server side
Show/Hide search based on received number of rows from server side
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
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.
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 secondjson
parameter passed into theinitComplete
function. Or you could userows()
with chainedcount()
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 thedom
options.This example shows the
rows().count()
and hiding the info element.https://live.datatables.net/cigunuji/1/edit
Kevin