PHPackages                             lampjunkie/php-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. lampjunkie/php-datatables

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

lampjunkie/php-datatables
=========================

PHP Library for (http://www.datatables.net)

1.0.1(13y ago)5817.4k29[1 issues](https://github.com/lampjunkie/php-datatables/issues)[1 PRs](https://github.com/lampjunkie/php-datatables/pulls)MITPHPPHP &gt;=5.2.4

Since Sep 19Pushed 13y ago14 watchersCompare

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

READMEChangelogDependenciesVersions (2)Used By (0)

php-datatables
==============

[](#php-datatables)

This is a PHP library which wraps around the DataTables () plugin for jQuery.

This library provides a object-oriented interface to configure and build javascript data tables. The goal of this library is to provide an easily extendable framework that allows DataTables to be easily incorporated into any existing application or web framework. The library provides the means to build DataTables that can render data from any source including databases, web services, csv files, etc...

Currently, php-datatables implements a majority of the features and options within DataTables. The remaining ones will be added over time.

Overview
--------

[](#overview)

The basic steps to using php-datatables are:

1. Set configuration options on a config object.
2. Implement a method to load an array of entity objects for your table to render.
3. Implement methods to format the data for your columns.

### Demo

[](#demo)

Look at the files within the demo/ directory to see a basic example of php-datatables in action.

- ajax.php

    A file showing how a json response is rendered for a DataTables ajax request
- Browser.php

    An entity object that is used by DemoDataTable to render the data
- browsers.csv

    A csv file containing the sample data for DemoDataTable
- DemoDataTable

    An example DataTable\_DataTable implementation which loads data from a csv file
- index.php

    A file showing how the initial html/js for a DataTable is rendered

Class Overview
--------------

[](#class-overview)

### DataTable\_DataTable

[](#datatable_datatable)

This class is the heart of the library. You simply extend this class for each DataTable that you will need in your application. The purpose of this class is to obtain the data for the table and to format the results that will be output.

Here are the basic requirements.

1. Pass in a DataTable\_Config object (see below).

    ```
    Your DataTable will need a config object in order define the columns and various options. You can either
    pass this object into the constructor or define it within your constructor and pass it on to the parent
    class.

    ```
2. Implement the loadData() method

    ```
    This method is where you pull the data you want displayed and return it within a DataTable_DataResult object.
    You are provided with a DataTable_Request object which allows you to pull the pagination information that
    is passed in through AJAX requests.

    ```
3. Implement the getTableId() method

    ```
    Simply return a unique id for this table instance. This id is used as the html id attribute in the rendered
    html table.

    ```
4. Implement getter methods to format colummn values

    ```
    In each DataTable_Column object (see below) you must define a getter method name so that the column knows
    where to obtain the value for each entity object. By default, DataTable_DataTable will call that getter
    method on the entity object. However, you can implement the getter method within your DataTable class
    in order to format the column values however you want.

    The only requirement is that your method should expect to receive an entity object as it's only parameter.

    Example configuration of getter method for a column:

            // configure column
            $column = new DataTable_Column();
            $column->setName('fullName')
                   ->setTitle('Full Name')
                   ->setGetMethod('getFullName');

    Example implementation of getter method defined above:

     	   /**
     		* Format a column value that combines multiple entity properties
     		*/
     		protected function getFullName(User $user)
     		{
     		  return $user->getFirstName() . ' ' . $user->getLastName();
     		}

    ```
5. Implement javascript callback functions

    ```
    If your datatable needs to implement the various DataTable callback methods (http://datatables.net/usage/callbacks)
    simply implement the various methods found within DataTable_DataTable.

     	   protected function getRowCallbackFunction()
     	   {
     	     return "
     			function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
     				/* Bold the grade for all 'A' grade browsers */
     				if ( aData[4] == 'A' )
     				{
     					$('td:eq(4)', nRow).html( 'A' );
     				}
     				return nRow;
     			}
     		 ";
     	   }

    ```

### DataTable\_Config

[](#datatable_config)

This class defines all the configuration options for a DataTable. A DataTable\_DataTable object expects to receive a DataTable\_Config object. This object set's all the various options that get passed to the javascript table. This object also holds a collection of all the DataTable\_Column definitions.

Most of the time you will probably want to instantiate the config object in your \_\_construct and pass it on to the parent class. However, you can create the config from outside and pass it in to your DataTable\_DataTable class. This is useful if you want to load the configuration from somewhere else such as a database, etc.

### DataTable\_Column

[](#datatable_column)

This class defines all the options for an individual column within the DataTable. Each column must at the very least have a name, title, and getter method name. You can configure whether columns are sortable, searchable, etc.

1. setTitle()

    This method sets the title that will be rendered in the column's tag
2. setName()

    This method sets a unique name that the column can be referenced by.
3. setGetMethod()

    This method lets DataTable\_DataTable know where it should obtain the value for the current column. First, it will check to see if this method exists within your DataTable\_DataTable implementing class. Otherwise, it will call the getter method on the entity object.
4. setSortKey()

    A key that loadData() can reference to know what to sort against.

Example:

```
    $column = new DataTable_Column();
    $column->setName("browser")
           ->setTitle("Browser")
           ->setGetMethod("getBrowser")
           ->setSortKey("b.browser")
           ->setIsSortable(true);

```

### DataTable\_Request

[](#datatable_request)

An object of this class needs to be passed into the DataTable\_DataTable-&gt;renderJson() method. This eventually gets passed into your loadData() implementation. This object simply stores the parameters that are passed from DataTables within AJAX requests and allow you to access them for pagination, sorting, and searching.

By default, this class provides a hydration method to fill the object with the parameters from a $\_GET, $\_POST, or $\_REQUEST array.

```
	$request = new DataTable_Request();
	$request->fromPhpRequest($_REQUEST);

```

You can extend this class to hydrate the parameters from some other framework specific request object if you need to.

### DataTable\_DataResult

[](#datatable_dataresult)

This is the type of object that is expected to be returned from your DataTable\_DataTable-&gt;loadData() implementation. You just need to pass in the array of your entities and a count of the total number of results for pagination (total records for all pages).

Using your DataTable
--------------------

[](#using-your-datatable)

Display table (index.php):

```
	// render the initial html/js
	$table = new MyDataTable();
	$table->setAjaxDataUrl('ajax.php');
	echo $table->render();

```

Render AJAX response (ajax.php):

```
	// instatiate new DataTable
	$table = new MyDataTable();

	// convert DataTable AJAX parameters in request to a DataTable_Request
	$request = new DataTable_Request();
	$request->fromPhpRequest($_REQUEST);

	// render the JSON data string
	echo $table->renderJson($request);

```

Non-AJAX DataTable
------------------

[](#non-ajax-datatable)

If you have a smaller data set that you want to render in a DataTable and thus don't need AJAX for your pagination, sorting you can easily switch to non-ajax mode.

You just need to make sure that serverSideEnabled is set to false on your DataTable\_Config object.

This will result in your loadData() method getting called when DataTable\_Datatable-&gt;render() is called. The loadData() method will receive a DataTable\_Request object which has the sorting set for whatever the default sort column is within your config. You may also want to set the staticMaxLength on the config object to let your loadData method know how to limit your results.

Example:

```
    // disable ajax
    $config->setIsServerSideEnabled(false);

    // let your loadData() method know to limit to 200 results
    $config->setStaticMaxLength(200);

```

Multi-Column Sorting
--------------------

[](#multi-column-sorting)

If you need to sort against multiple columns, you can easily get the sorting information of all of the columns from the DataTable\_Request object.

Example:

```
    public function loadData(DataTable_Request $request)
    {
      foreach($request->getSortColumns() as $sortColIndex => $sortDir){
        $sortKey = $this->getColumns()->get($sortColIndex)->getSortKey();

        // do something with $sortKey and $sortDir
      }
    }

```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4989d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/776015?v=4)[Marc Roulias](/maintainers/lampjunkie)[@lampjunkie](https://github.com/lampjunkie)

---

Tags

datatablesajaxtable

### Embed Badge

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

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

###  Alternatives

[wenzhixin/bootstrap-table

An extended table to integration with some of the most widely used CSS frameworks. (Supports Bootstrap, Semantic UI, Bulma, Material Design, Foundation)

11.8k283.4k1](/packages/wenzhixin-bootstrap-table)[datatables.net/datatables.net

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table. This is jQuery DataTables

56156.5k25](/packages/datatablesnet-datatablesnet)[fedemotta/yii2-widget-datatables

DataTables widget for Yii2

34179.4k1](/packages/fedemotta-yii2-widget-datatables)[datatables.net/datatables.net-bs5

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table. This is DataTables with styling for \[Bootstrap5\](https://getbootstrap.com/)

2185.7k16](/packages/datatablesnet-datatablesnet-bs5)[datatables.net/datatables.net-bs4

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table. This is DataTables with styling for \[Bootstrap4\](https://getbootstrap.com/docs/4.6/getting-started/introduction/)

2924.0k15](/packages/datatablesnet-datatablesnet-bs4)[datatables.net/datatables.net-dt

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table. This is DataTables with styling for \[DataTables\](https://datatables.net/)

1835.1k15](/packages/datatablesnet-datatablesnet-dt)

PHPackages © 2026

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