{"id":16423572,"url":"https://github.com/rougin/datatables","last_synced_at":"2025-10-26T22:31:45.656Z","repository":{"id":62537430,"uuid":"66901801","full_name":"rougin/datatables","owner":"rougin","description":"Handles server-side of DataTables in PHP.","archived":false,"fork":false,"pushed_at":"2024-11-17T15:30:33.000Z","size":317,"stargazers_count":4,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-01T00:51:05.909Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://roug.in/datatables/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rougin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-30T03:08:51.000Z","updated_at":"2024-11-17T15:30:37.000Z","dependencies_parsed_at":"2022-11-02T16:15:33.825Z","dependency_job_id":null,"html_url":"https://github.com/rougin/datatables","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fdatatables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fdatatables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fdatatables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougin%2Fdatatables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rougin","download_url":"https://codeload.github.com/rougin/datatables/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238408486,"owners_count":19467099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datatables","doctrine","eloquent","php-library","wrapper"],"created_at":"2024-10-11T07:40:19.252Z","updated_at":"2025-10-26T22:31:40.372Z","avatar_url":"https://github.com/rougin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Datatables\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]][link-license]\n[![Build Status][ico-build]][link-build]\n[![Coverage Status][ico-coverage]][link-coverage]\n[![Total Downloads][ico-downloads]][link-downloads]\n\n`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.\n\n## Installation\n\nInstall the `Datatables` package via [Composer](https://getcomposer.org/):\n\n``` bash\n$ composer require rougin/datatables\n```\n\n## Basic usage\n\nPrior in configuring `Datatables`, kindly ensure that the `serverSide` property is set to `true` in the Javascript part:\n\n``` js\n// index.js\n\nlet options = { processing: true }\n\noptions.ajax = 'http://localhost:8000'\n\noptions.serverSide = true\n\nnew DataTable('#example', options)\n```\n\n``` html\n\u003c!-- index.html --\u003e\n\n\u003c!-- ... --\u003e\n\n\u003ctable id=\"example\"\u003e\u003c/table\u003e\n```\n\n\u003e [!NOTE]\n\u003e 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`.\n\nFrom the PHP part, use the `Table` class to define the specified table:\n\n``` php\n// index.php\n\nuse Rougin\\Datatables\\Request;\nuse Rougin\\Datatables\\Table;\n\n// ...\n\n// The $_GET variable should be returned ---\n// and parsed as array\u003cstring, mixed\u003e ------\n$request = new Request($_GET);\n// -----------------------------------------\n\n// Parse columns based on the Request ---------\n$table = Table::fromRequest($request, 'users');\n// --------------------------------------------\n```\n\nBy 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:\n\n``` php\n// index.php\n\n// ...\n\n// The first column will be named as \"forename\" ---\n$table-\u003emapColumn(0, 'forename');\n// ------------------------------------------------\n\n$table-\u003emapColumn(1, 'surname');\n$table-\u003emapColumn(2, 'position');\n$table-\u003emapColumn(3, 'office');\n$table-\u003emapColumn(4, 'date_start');\n$table-\u003emapColumn(5, 'salary');\n\n// ...\n\n```\n\nOnce the table has been properly configured, initialize a source (e.g., `PdoSource`) that will be used for getting the data of the specified table:\n\n``` php\n// index.php\n\nuse Rougin\\Datatables\\Source\\PdoSource;\n\n// ...\n\n// Create a PDO instance... --------------\n$dsn = 'mysql:host=localhost;dbname=demo';\n\n$pdo = new PDO($dsn, 'root', /** ... */);\n// ---------------------------------------\n\n// ...then pass it to the PdoSource ---\n$source = new PdoSource($pdo);\n// ------------------------------------\n\n// ...\n```\n\nThen use the `Query` class to generate the requested data: \n\n``` php\n// index.php\n\nuse Rougin\\Datatables\\Query;\n\n// ...\n\n/** @var \\Rougin\\Datatables\\Source\\SourceInterface */\n$source = /** ... */;\n\n$query = new Query($request, $source);\n\n/** @var \\Rougin\\Datatables\\Result */\n$result = $query-\u003egetResult($table);\n```\n\nThe `getResult` from the `Query` class returns a `Result` class in which returns the response as an array or as JSON format:\n\n``` php\n// index.php\n\n// ...\n\n/** @var \\Rougin\\Datatables\\Result */\n$result = $query-\u003egetResult($table);\n\necho $result-\u003etoJson();\n```\n\n``` bash\n$ php index.php\n```\n\n``` json\n{\n  \"draw\": 1,\n  \"recordsFiltered\": 57,\n  \"recordsTotal\": 57,\n  \"data\":\n  [\n    [\n      \"Airi\",\n      \"Satou\",\n      \"Accountant\",\n      \"Tokyo\",\n      \"2008-11-28\",\n      \"162700.0\"\n    ],\n    [\n      \"Angelica\",\n      \"Ramos\",\n      \"Chief Executive Officer (CEO)\",\n      \"London\",\n      \"2009-10-09\",\n      \"1200000.0\"\n    ],\n\n    // ...\n  ]\n}\n```\n\n## Creating custom sources\n\nTo create a custom source, kindly use the `SourceInterface` for its implementation:\n\n``` php\nnamespace Rougin\\Datatables\\Source;\n\nuse Rougin\\Datatables\\Request;\nuse Rougin\\Datatables\\Table;\n\ninterface SourceInterface\n{\n    /**\n     * Returns the total items after filter. If no filters\n     * are defined, the value should be same with getTotal.\n     *\n     * @return integer\n     */\n    public function getFiltered();\n\n    /**\n     * Returns the items from the source.\n     *\n     * @return string[][]\n     */\n    public function getItems();\n\n    /**\n     * Returns the total items from the source.\n     *\n     * @return integer\n     */\n    public function getTotal();\n\n    /**\n     * Sets the payload to be used in the source.\n     *\n     * @param \\Rougin\\Datatables\\Request $request\n     *\n     * @return self\n     */\n    public function setRequest(Request $request);\n\n    /**\n     * Sets the table to be used in the source.\n     *\n     * @param \\Rougin\\Datatables\\Table $table\n     *\n     * @return self\n     */\n    public function setTable(Table $table);\n}\n```\n\n## Changelog\n\nPlease see [CHANGELOG][link-changelog] for more information what has changed recently.\n\n## Testing\n\nIf 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:\n\n``` bash\n$ https://github.com/rougin/authsum.git \"Sample\"\n```\n\nAfter cloning, use `Composer` to install its required packages:\n\n``` bash\n$ cd Sample\n$ composer update\n```\n\nOnce 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`:\n\n### Unit tests\n\n`Datatables` also contains unit tests that were written in [PHPUnit](https://phpunit.de/index.html):\n\n``` bash\n$ composer test\n```\n\nWhen 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.\n\n### Code quality\n\nTo 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`:\n\n``` bash\n$ composer global require phpstan/phpstan\n```\n\nOnce installed, `PHPStan` can now be run using the `phpstan` command:\n\n``` bash\n$ cd Sample\n$ phpstan\n```\n\n### Coding style\n\nAsides 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`:\n\n``` bash\n$ composer global require friendsofphp/php-cs-fixer\n```\n\nAfter being installed, use the `php-cs-fixer` command in the same `Datatables` directory:\n\n``` bash\n$ cd Sample\n$ php-cs-fixer fix --config=phpstyle.php\n```\n\nThe 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.\n\n\u003e [!NOTE]\n\u003e Installing `PHPStan` and `PHP Coding Standards Fixer` requires a version of PHP that is `7.4` and above.\n\n## License\n\nThe MIT License (MIT). Please see [LICENSE][link-license] for more information.\n\n[ico-build]: https://img.shields.io/github/actions/workflow/status/rougin/datatables/build.yml?style=flat-square\n[ico-coverage]: https://img.shields.io/codecov/c/github/rougin/datatables?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/rougin/datatables.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-version]: https://img.shields.io/packagist/v/rougin/datatables.svg?style=flat-square\n\n[link-build]: https://github.com/rougin/datatables/actions\n[link-changelog]: https://github.com/rougin/datatables/blob/master/CHANGELOG.md\n[link-contributors]: https://github.com/rougin/datatables/contributors\n[link-coverage]: https://app.codecov.io/gh/rougin/datatables\n[link-downloads]: https://packagist.org/packages/rougin/datatables\n[link-license]: https://github.com/rougin/datatables/blob/master/LICENSE.md\n[link-packagist]: https://packagist.org/packages/rougin/datatables","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frougin%2Fdatatables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frougin%2Fdatatables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frougin%2Fdatatables/lists"}