phputil/datatables

PHP representation of Datatables' request and response.

2.0.1 2017-01-09 18:37 UTC

This package is auto-updated.

Last update: 2024-10-23 04:23:58 UTC


README

PHP representation of Datatables' request and response.

Main files:

This project uses semantic versioning. See our releases.

Installation

composer require phputil/datatables

Example on version 2.x

<?php
require_once 'vendor/autoload.php';

use phputil\datatables\DataTablesRequest;
use phputil\datatables\DataTablesResponse;

//
// REQUEST
//
$req = new DataTablesRequest( $_POST );

// PAGINATION
$offset = $req->start;
$limit = $req->length;

// SEARCH
$searchValue = $req->searchValue(); // Example: 'Alice'

// FILTERING
$search = $req->columnSearch(); // Example: array( 'name' => 'Bob', 'age' => 21 )

// SORTING
$order = $req->columnOrder(); // Example: array( 'name' => 'ASC', 'age' => 'DESC' )

...

//
// RESPONSE
//
$totalCount = /* total number of records to return */
$filteredCount = /* filtered number of records to return */
$data = /* items to return */
$draw = $req->draw; // From the request

$res = new DataTablesResponse(
	$totalCount, $filteredCount, $data, $draw );
	
echo json_encode( $res );
?>

Example on version 1.x

<?php
require_once 'vendor/autoload.php';

use phputil\DataTablesRequest;
use phputil\DataTablesResponse;

//
// REQUEST
//
$req = new DataTablesRequest( $_POST );

// PAGINATION
$limit = $req->limit();
$offset = $req->offset();

// SEARCH
$search = $req->search(); // null in case of not having search

// FILTERING
$filters = $req->filters(); // Example: array( 'name' => 'Bob', 'age' => 21 )

// SORTING
//	Originally, Datatables returns the sort order
//	by column index, but here you can get it using
//	your own column names.
$orders = $req->orders( array( 'name', 'age' ) ); // Example: array( 'name' => 'asc', 'age' => 'desc' )

...

//
// RESPONSE
//
$totalCount = /* total number of records to return */
$filteredCount = /* filtered number of records to return */
$data = /* items to return */
$draw = $req->draw(); // From the request

$res = new DataTablesResponse(
	$totalCount, $filteredCount, $data, $draw );
	
echo json_encode( $res );
?>