Server-side paging in DataTables

Server-side paging in DataTables allows for large amounts of data to be loaded and displayed on a page without having to load the entire dataset. This is done by sending an Ajax request to the server for each new page of data, instead of loading all the data at once.

Here are the basic steps to implement server-side paging in DataTables:

1. Configure the server-side script to return the appropriate subset of data based on the page number and page size.

2. Initialize DataTables with the server-side processing option set to true, and configure the Ajax settings to point to the server-side script and send the necessary parameters.

3. Set the paging options in DataTables to display the desired number of rows per page, and configure any additional options as needed.

Here is an example of how to implement server-side paging in DataTables using PHP and MySQL:

PHP script (server-side script to retrieve data):

<?php
// MySQL database connection details
$host = 'localhost';
$user = 'username';
$password = 'password';
$database = 'database';

// Get the page number and page size from the DataTables parameters
$start = $_GET['start'];
$length = $_GET['length'];

// Build the MySQL query to retrieve the data for the requested page
$query = "SELECT * FROM my_table LIMIT $start, $length";

// Execute the query and retrieve the data
$conn = new mysqli($host, $user, $password, $database);
$result = $conn->query($query);
$data = array();
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Format the data as JSON and return it to DataTables
echo json_encode(array(
    "draw" => intval($_GET['draw']),
    "recordsTotal" => $conn->query('SELECT COUNT(*) FROM my_table')->fetch_row()[0],
    "recordsFiltered" => $conn->query('SELECT COUNT(*) FROM my_table')->fetch_row()[0],
    "data" => $data
));
?>

JavaScript code (client-side script to initialize DataTables):

$(document).ready(function() {
    $('#myTable').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "server-side-script.php",
            "data": function(d) {
                d.start = d.start || d.page * d.length;
                d.length = d.length || 10;
            }
        },
        "columns": [
            {"data": "id"},
            {"data": "name"},
            {"data": "email"}
        ],
        "pageLength": 10,
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
    });
});

In this example, the PHP script retrieves the requested page of data from the database using the LIMIT clause in the MySQL query. The script also calculates the total number of records in the table, which is needed by DataTables for paging calculations.

The JavaScript code initializes DataTables with the server-side processing option set to true, and configures the Ajax settings to send the necessary parameters to the server-side script. The column definitions specify which columns should be displayed in the table, and the paging options set the number of rows per page and the available page sizes.

With these steps in place, DataTables will handle the pagination and display of data on the page, and will automatically send requests to the server for new pages of data as the user navigates through the table.

Komentar

Postingan populer dari blog ini

WhatsApp Web login QR code in an HTML page using whatsapp-web.js

Node.js Telegram Bot API send an image with text

Add these security headers to your website