Datatable Server Side with JOIN [closed]

3

Good evening!

I started using Datatable recently, I'm using it with the server-side feature. I need to relate 2 tables with INNER JOIN, I saw that there is a "difficulty" in doing this type of procedure in the case of server-side. I even found a custom ssp.class.php, but I did not really understand how to use it ...

Would anyone have a tutorial to share about it? Or an example?

    
asked by anonymous 11.07.2018 / 02:12

1 answer

0

When using the server-side option, you will have to adapt your server-side code to receive and return the values in a structure defined by the DataTables requests.

DataTables sends the data with this structure more or less to the server:

  • start: If you are using pagination, you will have to return only the table rows that will be displayed, then start is the lower limit of the search in the
  • length : from start, how many forward lines will I return, that is, the number of items that will be displayed on a page
  • draw : a control number that serves for the DataTables to know on which page it is, you will just return it back
  • order : an array with columns and direction (ascending, descending) that you will use in the query to sort the table
  • search : An array with values that you will use to do searches in the table, that is, the WHERE of the query
  • columns : the columns of the table

In addition, DataTables also expects a response with this structure:

  • draw: Page control value for DataTables
  • recordsTotal: total number of rows in your table in the unfiltered database (a count (*) without WHERE)
  • recordsFiltered: total number of query rows with filters. If no filter has been made (search by specific values), then it will be the same value as recordsTotal
  • data: an array of keys and values with the return of your query.

You will return all of this in a JSON. JSON may have other data as needed, but this data is required for the table to function correctly with pagination and everything else.

Knowing this structure, you now have to set up your queries using start, length, search, sort, etc. The fact that you make a JOIN in your search does not matter in this case, it works with any query. I suggest you see the code for this page , it shows an example of how to do this with MySQL, using the structure I spoke of, in a simpler way than the ssp.class.php you mentioned. This video on YouTube also explains very clearly. Good luck!

    
11.07.2018 / 15:25