Server Side Datatables Library for CodeIgniter 4 Framework NOTE: This lib is under early development.
Library to make server side Datatables on CodeIgniter 4 to be more easy
- Codeigniter 4.*
- JQuery 3.*
- JQuery Datatables
Installation is best done via Composer, you may use the following command:
composer require irsyadulibad/codeigniter4-datatables
This will add the latest release of codeigniter4-datatables as a module to your project.
Should you choose not to use Composer to install, you can download this repo, extract and rename this folder to codeigniter4-datatables. Then enable it by editing app/Config/Autoload.php and adding the Irsyadulibad\DataTables namespace to the $psr4 array. For example, if you copied it into app/Libraries:
$psr4 = [
'Config' => APPPATH . 'Config',
APP_NAMESPACE => APPPATH,
'App' => APPPATH,
'Irsyadulibad\DataTables' => APPPATH .'Libraries/codeigniter4-datatables/src',
];
This is an example code for using this library:
- PHP:
<?php namespace App\Controllers;
use Irsyadulibad\DataTables\DataTables;
class Home extends BaseController
{
public function json()
{
return DataTables::use('users')
->where(['role' => 'admin'])
->hideColumns(['password'])
->rawColumns(['bio'])
->make(true);
}
}
- Javascript
$('#table').DataTable({
processing: true,
serverSide: true,
ajax:{
url: 'http://localhost:8080/json'
},
columns: [
{data: 'username', name: 'username'},
{data: 'email', name: 'email'},
{data: 'fullname', name: 'fullname'}
{data: 'bio', name: 'bio'}
]
});
Now you can use this without instantiate class
DataTables::use('table');
We did not use the POST method due to a problem with the CSRF
$routes->get('datatables/json', 'Controller::method', ['as' => 'dt-json']);
- Select Table
Select the table that you want to use
DataTables::use('table')
- Set Output
The default parameter is true, which is automatically return the JSON data. You can return the data's dump by passing the false param
DataTables::use('table')
->make(false);
- Select Fields
Select the sepicifics column in the table
->select('username, password')
- Where Clause
->where(['role' => 'user', 'active' => 1])
- orWhere Clause
->orWhere(['role' => 'user', 'active' => 0])
- Join Clause
// <table>, <condition>, <type>
->join('address', 'users.id = address.uid', 'INNER JOIN')
- Add Column
Add custom column which is not in the table
// <name>, <callback>
->addColumn('action', function($data) {
return '<a href="/edit/'.$data->id.'">edit</a>';
})
- Edit Column\
// <name>, <callback>
->editColumn('created_at', function($data) {
return format($data);
})
- Raw Columns
By default, all of the data is escaped to prevent XSS. But if you want to unescape them, you can use this method
->rawColumns(['bio'])
- Hide Columns
Hide columns from JSON output
->hideColumns(['password'])
- For now, we don't use the POST method due to a problem with the CSRF
Github: [https://github.com/irsyadulibad]
Website: [http://irsyadulibad.my.id]
Facebook: [https://facebook.com/irsyadulibad.dev]