PHP Library to handle server-side processing for Datatables, in a fast and simple way. Live Demo
- Easy to use. Generates json using only a few lines of code.
- Editable columns with a closure function.
- Supports mysql and sqlite for native php.
- Works with :
Installation via composer is supported.
If you haven't started using composer, I highly recommend you to use it.
Put a file named composer.json
at the root of your project, containing this information:
{
"require": {
"ozdemir/datatables": "1.*"
}
}
And then run: composer install
Or just run : composer require ozdemir/datatables
Add the autoloader to your project:
<?php
require_once 'vendor/autoload.php'
You're now ready to begin using the Datatables php library.
A simple ajax example:
<?php
require_once 'vendor/autoload.php';
use Ozdemir\Datatables\Datatables;
use Ozdemir\Datatables\DB\MySQL;
$config = [ 'host' => 'localhost',
'port' => '3306',
'username' => 'homestead',
'password' => 'secret',
'database' => 'sakila' ];
$dt = new Datatables( new MySQL($config) );
$dt->query("Select film_id, title, description from film");
echo $dt->generate();
This is the list of available public methods.
- query ( $query : string )
(required)
- add ($newColumn:string, Closure:object )
(optional)
- edit ($column:string, Closure:object )
(optional)
- get ($value:string )
(optional - for dev purpose)
- hide ($column:mixed )
(optional)
- generate ( )
(required)
<?php
$dt = new Datatables( new MySQL($config) );
$dt->query("Select id, name, email, address, plevel from users");
$dt->edit('id', function($data){
// return an edit link.
return "<a href='user.php?id=" . $data['id'] . "'>edit</a>";
});
$dt->edit('email', function($data){
// return [email protected] to m***@mail.com
return preg_replace('/(?<=.).(?=.*@)/u','*', $data['email']);
});
$dt->edit('address', function($data){
// check if user has authorized to see that
$current_user_plevel = 4;
if ($current_user_plevel > 2 && $current_user_plevel > $data['plevel']) {
return $data['address'];
}
return 'you are not authorized to view this column';
});
$dt->add('action', function($data){
// return an edit link in new column action
return "<a href='user.php?id=" . $data['id'] . "'>edit</a>";
});
echo $dt->generate();
DataTables > 1.10
PHP > 5.3.7
Copyright (c) 2015 Yusuf ÖZDEMİR, released under the MIT license