Change pageLength through custom search
Change pageLength through custom search
I have a datatable setup with a custom search which is fully customizable and works exactly as intended. The only issue that I am running into is that the display number at the bottom of the page doesn't match the results ("Showing 1 to 100 of 50,000 entries").
By default, the limit is 100, so I have the pageLength set to 100. This limit can be changed through the search. Instead of returning 100 items, maybe the user wants to return 250. However, when the user changes the limit, the data is shown correctly (there are 250 entries shown), but the count at the bottom stays 100.
How can I change this through serverside processing?
Replies
How are you doing this? Are you using the
page.len()
API to set the number of rows displayed on the page or the length change input element?Sounds like you are doing something different that Datatables doesn't know about. At a minimum post the relevant Javascript code so we can see what you are doing. Better is a link to your page or a test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
No, I am not using that, but that might be what I need.
The custom search is a section where the user can do a detailed search very similar to Jira. The limit is a dropdown (much like normal DT) and then the value is simply added to the database query to limit the number of results. But I don't know how to change the value of the DT table so that it matches the returned entries.
This is what I am using to send data to the server side:
myDataTable.search(JSON.stringify(search_values)).draw();
Where search values is a combination of checkboxes, dropdowns and a search field. In the search field the user can do things like
source=my_source and var1=my_1st_value and var2=my_2nd_value
The search is broken up and creates a dynamic database call in order to retrieve the desired data.
One of the dropdowns for the search is a limit - "Limit by x amount".
I need to change the pageLength to that of "x" from the dropdown. From the looks of it, the
page.len()
might just be the ticket.Bingo. That was exactly what I needed.
Thank you!
Server side processing is a client/server solution that provides paging. When the Datatables page length is set to 100 the server side processing script is expected to return only 100 rows. You can use the browser's network inspector to see the JSON response and how many rows are returned.
Based on your description it sounds like the server side processing script you have follows the SSP protocol. A proper SSP script will set the limit based on the Datatables page length, ie 100, not the 250 from the custom search.
I'm not sure how the client side Datatables handles the display if 250 rows are returned when it expects 100 rows. Sounds like 250 rows might be displayed but I'm not sure. Likely you won't want to change your server side script to use the
length
parameter, as documented in the SSP protocol, to set the query limit. To keep Datatables in sync use thepage.len()
API. Something like this:Where
CustomSearchPageLength
is the page length value from the custom search.Kevin