PHP code example of murzid / codeigniter-datatables
1. Go to this page and download the library: Download murzid/codeigniter-datatables library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
murzid / codeigniter-datatables example snippets
// CodeIgniter 3 Example
// Here we will select all fields from posts table
// and make a join with categories table
// Please note: we don't need to call ->get() here
$queryBuilder = $this->db->select('p.*, c.name category')
->from('posts p')
->join('categories c', 'c.id=p.category_id');
/**
* The first parameter is the query builder instance
* and the second is the codeigniter version (3 or 4)
*/
$datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder, '3');
$datatables->generate(); // done
// General, use the second param to define the version
// The default is 4
$datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder, '3');
// CodeIgniter 3
$datatables = new Murzid\CodeIgniterDataTables\DataTablesCodeIgniter3($queryBuilder);
// CodeIgniter 4
$datatables = new Murzid\CodeIgniterDataTables\DataTablesCodeIgniter4($queryBuilder);
$datatables = new Murzid\CodeIgniterDataTables\DataTables($queryBuilder);
// Return the output as objects instead of arrays
$datatables->asObject();
// Only return title & category (accept string or array)
$datatables->only(['title', 'category']);
// Return all except the id
// You may use one of only or except
$datatables->except(['id']);
// Format the output
$datatables->format('title', function($value, $row) {
return '<b>'.$value.'</b>';
});
// Add extra column
$datatables->addColumn('action', function($row) {
return '<a href="url/to/delete/post/'.$row->id.'">Delete</a>';
});
// Add column alias
// It is very useful when we use SELECT JOIN to prevent column ambiguous
$datatables->addColumnAlias('p.id', 'id');
// Add column aliases
// Same as the addColumnAlias, but for multiple alias at once
$datatables->addColumnAliases([
'p.id' => 'id',
'c.name' => 'category'
]);
// Add squence number
// The default key is `sequenceNumber`
// You can change it with give the param
$datatables->addSequenceNumber();
$datatables->addSequenceNumber('rowNumber'); // It will be rowNumber
// Don't forget ot call generate to get the results (send to output buffer)
$datatables->generate();
// Output Content
// TRUE = Send to output buffer (default)
// FALSE = Return generated data as array without send to output buffer
$output_buffer = FALSE;
// Additional Custom Data (array)
$extra_data = [
// Set Custom Data
'customField' => 'customValue',
// Enable Query Debugging (Last Query)
'debug' => TRUE,
];
// Call generate to get the custom results
$result = $datatables->generate($output_buffer, $extra_data);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.