Datatables – server side processing

How to process datatables on the server side? Implement Length / limit changes, the search bar filter and sorting mechanisms.

Length / Limit

You can select the amount of data entries, which you want to display. That has to be handled on the server side.

"lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "Alle"] ],
Bildschirmfoto-2020-03-08-um-23.08.55

With the following code you can add the limit command with the user specified length to the sql query:

if($_POST["length"] != -1){
  $sqlQuery .= ' LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
 }

With chrome developer tools you can debug issues. Here you can see that the action.php api is getting called with the length 50, as the user has clicked on.

Datatables - server side processing

Search bar / filter

The search bar is enabled by default in datatables.

Bildschirmfoto-2020-03-08-um-23.16.09

With the following code you can add it to your existing sql command:

if(!empty($_POST["search"]["value"])){
  $sqlQuery .= ' AND stockname LIKE "%'.$_POST["search"]["value"].'%" ';
}

In the search console you can see your own user input character for character appearing as single action.php api calls. The search[value] field contains your search keyword.

Datatables - server side processing

The frontend shows in real time the result of the updated sql command.

Datatables - server side processing

Sorting

You can sort the table by clicking on the table header column names. Every column you can order ascending or descending. This has to be in your server side api program code.

Datatables - server side processing

If you are calculating some columns, you can do this calculation within the sql command.

$sqlQuery = "SELECT stockname, price, buy, sell, userlimits.id AS userlimitID, stocks.url AS stockURL, userid, (price / buy) AS buyPercent, (price / sell) AS sellPercent FROM userlimits LEFT JOIN users ON userlimits.userid = users.id LEFT JOIN stocks on userlimits.stockid = stocks.id WHERE userid = '".$_SESSION["userid"]."'";

The processing of the user selected columns and order mechanism can be coded that way:

Datatables - server side processing

At the search console you will see the order[0][column] variable, which contains the number of the column, which you want to order. order[0][dir] is the variable which contains ascending or descending.

Datatables - server side processing

How to disable server side sorting?

You can’t disable just server side sorting. You have to make a decision and stay with it. You can’t pick for the search bar / number limit of the data entries or the sorting mechanism, if you want to use the client or server version.

Datatables - server side processing
https://stackoverflow.com/questions/45261832/how-to-disable-server-side-sorting-with-datatables

Bewerte diesen Artikel

0 Bewertung(en), Durchschnitt: 0 von 5

Dieser Artikel wurde noch nicht bewertet.

Es tut uns leid, dass dir der Beitrag nicht gefallen hat.

Lass uns genau diesen Artikel überarbeiten.

Erzähle uns, was wir besser machen können.

Beitrag teilen

Hinterlasse einen Kommentar

  Abonnieren  
Benachrichtige mich bei
Scroll to Top