PHPackages                             rougin/datatables - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rougin/datatables

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rougin/datatables
=================

Handles server-side of DataTables in PHP.

44565PHPCI failing

Since Sep 29Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/rougin/datatables)[ Packagist](https://packagist.org/packages/rougin/datatables)[ RSS](/packages/rougin-datatables/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Datatables
==========

[](#datatables)

[![Latest Version on Packagist](https://camo.githubusercontent.com/28223539762f79917ae30c9cd561c7d77b327620b7709aeec8a8be2a432c5171/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f7567696e2f646174617461626c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/datatables)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rougin/datatables/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/54f2a7a3a4e64f796a5afde55beeb8983509e35db6ec8f8e4fda44ad5760d63c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f7567696e2f646174617461626c65732f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/rougin/datatables/actions)[![Coverage Status](https://camo.githubusercontent.com/fe5f72ee85d9696f61edbf80d34699a5edb990eb392e5b7a55e3230ec71ef7ce/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f7567696e2f646174617461626c65733f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/rougin/datatables)[![Total Downloads](https://camo.githubusercontent.com/ba3712feea96b52a79d6698944420270d5d7ed226886cd7a60579d681153b36f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f7567696e2f646174617461626c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/datatables)

`Datatables` is a simple PHP package that handles the [server-side of DataTables](https://datatables.net/examples/data_sources/server_side.html). Its server-side response can be used by `DataTables` from the HTML which requires little to no configuration.

Installation
------------

[](#installation)

Install the `Datatables` package via [Composer](https://getcomposer.org/):

```
$ composer require rougin/datatables
```

Basic usage
-----------

[](#basic-usage)

Prior in configuring `Datatables`, kindly ensure that the `serverSide` property is set to `true` in the Javascript part:

```
// index.js

let options = { processing: true }

options.ajax = 'http://localhost:8000'

options.serverSide = true

new DataTable('#example', options)
```

```

```

Note

For more information in the above example, kindly see the [official guide](https://datatables.net/examples/data_sources/server_side.html) on how to implement server-side rendering of data to `DataTables`.

From the PHP part, use the `Table` class to define the specified table:

```
// index.php

use Rougin\Datatables\Request;
use Rougin\Datatables\Table;

// ...

// The $_GET variable should be returned ---
// and parsed as array ------
$request = new Request($_GET);
// -----------------------------------------

// Parse columns based on the Request ---------
$table = Table::fromRequest($request, 'users');
// --------------------------------------------
```

By default, getting columns from the payload of the Javascript part of `DataTables` does not provide its name (e.g., `forename`, `surname`, etc.). As the column name is required for getting its data from a source, there is a need to map its column to the database table:

```
// index.php

// ...

// The first column will be named as "forename" ---
$table->mapColumn(0, 'forename');
// ------------------------------------------------

$table->mapColumn(1, 'surname');
$table->mapColumn(2, 'position');
$table->mapColumn(3, 'office');
$table->mapColumn(4, 'date_start');
$table->mapColumn(5, 'salary');

// ...
```

Once the table has been properly configured, initialize a source (e.g., `PdoSource`) that will be used for getting the data of the specified table:

```
// index.php

use Rougin\Datatables\Source\PdoSource;

// ...

// Create a PDO instance... --------------
$dsn = 'mysql:host=localhost;dbname=demo';

$pdo = new PDO($dsn, 'root', /** ... */);
// ---------------------------------------

// ...then pass it to the PdoSource ---
$source = new PdoSource($pdo);
// ------------------------------------

// ...
```

Then use the `Query` class to generate the requested data:

```
// index.php

use Rougin\Datatables\Query;

// ...

/** @var \Rougin\Datatables\Source\SourceInterface */
$source = /** ... */;

$query = new Query($request, $source);

/** @var \Rougin\Datatables\Result */
$result = $query->getResult($table);
```

The `getResult` from the `Query` class returns a `Result` class in which returns the response as an array or as JSON format:

```
// index.php

// ...

/** @var \Rougin\Datatables\Result */
$result = $query->getResult($table);

echo $result->toJson();
```

```
$ php index.php
```

```
{
  "draw": 1,
  "recordsFiltered": 57,
  "recordsTotal": 57,
  "data":
  [
    [
      "Airi",
      "Satou",
      "Accountant",
      "Tokyo",
      "2008-11-28",
      "162700.0"
    ],
    [
      "Angelica",
      "Ramos",
      "Chief Executive Officer (CEO)",
      "London",
      "2009-10-09",
      "1200000.0"
    ],

    // ...
  ]
}
```

Creating custom sources
-----------------------

[](#creating-custom-sources)

To create a custom source, kindly use the `SourceInterface` for its implementation:

```
namespace Rougin\Datatables\Source;

use Rougin\Datatables\Request;
use Rougin\Datatables\Table;

interface SourceInterface
{
    /**
     * Returns the total items after filter. If no filters
     * are defined, the value should be same with getTotal.
     *
     * @return integer
     */
    public function getFiltered();

    /**
     * Returns the items from the source.
     *
     * @return string[][]
     */
    public function getItems();

    /**
     * Returns the total items from the source.
     *
     * @return integer
     */
    public function getTotal();

    /**
     * Sets the payload to be used in the source.
     *
     * @param \Rougin\Datatables\Request $request
     *
     * @return self
     */
    public function setRequest(Request $request);

    /**
     * Sets the table to be used in the source.
     *
     * @param \Rougin\Datatables\Table $table
     *
     * @return self
     */
    public function setTable(Table $table);
}
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](https://github.com/rougin/datatables/blob/master/CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

If there is a need to check the source code of `Datatables` for development purposes (e.g., creating fixes, new features, etc.), kindly clone this repository first to a local machine:

```
$ https://github.com/rougin/authsum.git "Sample"
```

After cloning, use `Composer` to install its required packages:

```
$ cd Sample
$ composer update
```

Once the packages were installed, kindly check the following below on how to maintain the code quality and styling guide when interacting the source code of `Datatables`:

### Unit tests

[](#unit-tests)

`Datatables` also contains unit tests that were written in [PHPUnit](https://phpunit.de/index.html):

```
$ composer test
```

When creating fixes or implementing new features, it is recommended to run the above command to always check if the updated code introduces errors during development.

### Code quality

[](#code-quality)

To retain the code quality of `Datatables`, a static code analysis code tool named [PHPStan](https://phpstan.org/) is being used during development. To start, kindly install the specified package in global environment of `Composer`:

```
$ composer global require phpstan/phpstan
```

Once installed, `PHPStan` can now be run using the `phpstan` command:

```
$ cd Sample
$ phpstan
```

### Coding style

[](#coding-style)

Asides from code quality, `Datatables` also uses a tool named [PHP Coding Standards Fixer](https://cs.symfony.com/) for maintaining an opinionated style guide. The said tool needs also to be installed in the global environment of `Composer`:

```
$ composer global require friendsofphp/php-cs-fixer
```

After being installed, use the `php-cs-fixer` command in the same `Datatables` directory:

```
$ cd Sample
$ php-cs-fixer fix --config=phpstyle.php
```

The specified `phpstyle.php` currently follows the [PSR-12](https://www.php-fig.org/psr/psr-12/) as the baseline of the coding style and uses [Allman](https://en.wikipedia.org/wiki/Indentation_style#Allman_style) as its indentation style.

Note

Installing `PHPStan` and `PHP Coding Standards Fixer` requires a version of PHP that is `7.4` and above.

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](https://github.com/rougin/datatables/blob/master/LICENSE.md) for more information.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance44

Moderate activity, may be stable

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity13

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c7721589a958a1fbcef1b527c794d820d75b82988f14b2ed86a1c6904d76a59e?d=identicon)[rougin](/maintainers/rougin)

---

Top Contributors

[![rougin](https://avatars.githubusercontent.com/u/6078637?v=4)](https://github.com/rougin "rougin (74 commits)")

### Embed Badge

![Health badge](/badges/rougin-datatables/health.svg)

```
[![Health](https://phpackages.com/badges/rougin-datatables/health.svg)](https://phpackages.com/packages/rougin-datatables)
```

###  Alternatives

[d4l/kirby-static-site-generator

Static site generator plugin for Kirby 3+

14313.1k](/packages/d4l-kirby-static-site-generator)[aeon-php/rate-limiter

Aeon rate limiter (throttling) library

10142.9k1](/packages/aeon-php-rate-limiter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
