Datatables server side processing

How to implement: Datatables server side processing. Code 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"] ],
Datatables server side processing of the length selection
Datatables server side processing of the length selection

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.

Debug in Developer Tools
Debug in Developer Tools

Search bar / filter

The search bar is enabled by default in datatables.

Datatables Search Bar Filter
Datatables Search Bar Filter

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

  $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 1

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

Datatables server side processing 2


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 3

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

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

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

Datatables server side processing 4

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 5

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 6

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Scroll to Top