The recommended useage of Datatables for this particular usecase

The recommended useage of Datatables for this particular usecase

phppStevephppSteve Posts: 3Questions: 1Answers: 0
edited March 2022 in DataTables 1.10

Hi, The way I'm using Datatable is probably common, yet even after the multiple code example still not sure how to configure the DataTable call
.
The user will run a search on any of our webpages, that could return anywhere between 9 or 10 results , up to results that go beyond 200,000 records.

Would like to:

  • Load maximum 1000 total records into the table at one time ( they will choose 10,20, etc.. per page of course )

  • The "Total Records" on the paginator, will say: "201,456 records total" as an example..

    • And the prev or next button, will call - via ajax through our backend endpoint - the SQL database to get the next 10 records or previous 10 records, if they move beyond the original total 1000 records that are in the table.

Do I need
"serverside: true" for this,
and
"ajax: /backend/endpoint/getData.php"

I'm guessing I need the columns parameter to the DataTable() as well..
For some examples, I've seen:
columns: [
{ 'db': 'first_name' }
{ 'db': 'last_name' }
]
Or I've seen 'data' as the key instead of 'db'.

Has anyone seen a good example for this basic use case ?
Thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    When using serverSide processing the server script needs to support sending one page at a time using the server side processing protocol. The server script is expect to run only the rows for the page. Each sorting, searching or paging action will fetch the page data from the server. You can see the behavior with the server side processing examples.

    The user will run a search on any of our webpages,

    You can to this with the ajax.data option, like this example.

    I'm guessing I need the columns parameter to the DataTable() as well..

    If the row data is object based then you will use columns.data. See the Data docs for more details. Also see the ajax docs.

    Please post any further questions.

    Kevin

  • phppStevephppSteve Posts: 3Questions: 1Answers: 0

    Thanks for the reply. I'll check out the links

    When I mentioned: "The user will run a search on any of our webpages," what I meant was just that, on all our pages, there's a standard html form at the top of page, so they fill in the search parameters, cilck submit, and our database returns the results in the datatable at bottom.
    I might have made it sound like I want to do further searching/filtering through Datatable.
    This does get me progress. So far I can't get any examples to work, even a simple one I found at: https://www.geeksforgeeks.org/how-to-demonstrate-the-use-of-ajax-loading-data-in-datatables/

    But I'm going to recreate the example outside of the php framework I"m working inside.

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794
    Answer ✓

    When I mentioned: "The user will run a search on any of our webpages," what I meant was just that, on all our pages

    Understood. That is where sending the extra parameters via ajax.data is used. You can send the search terms to the server to be used as part of the query filter. When the user enters a search term and submits use ajax.reload() for Datatables to send a new ajax request to the server with the search term parameters. If you are using server side processing the an table draw, sorting, searching or paging will send the aajx request along with the search term parameters.

    Just for reference here is the server side PHP script used for the SSP examples.

    If you don't want to use server side processing you could create your own custom paging with 1000 rows of data at the client. You can use the same -option ajax.datatechnique without server side processing. You can create your own custom previous and next buttons and use-api page()to change pages and-api page.info()to keep track of where you are at and if at the last page use-api ajax.reload()with an additional pageNumber parameter. The ajax response can contain additional parameters that indicate total rows, ie 201,456, that you can then use to update the information element with-option infoCallbackor create your own. Use the-api ajax.json()` API to get the current JSON response.

    There aren't any examples of this but I believe there are other threads discussing this type of solution.

    Lots of options. We are here to help.

    Kevin

  • phppStevephppSteve Posts: 3Questions: 1Answers: 0

    That's nice. I may want to just do that,.. hook up the nextprev buttons, but not doing serverside. i'm going to check those threads out. Thanks again. I wish that basic example didn't have the ssp library stuff, though. Would be nice to have just the most basic, not use libs. But it's all good.

Sign In or Register to comment.