A library for implementing server-side processing with DataTables in CodeIgniter.
Datatables Server Side is a library designed to handle server-side processing with DataTables in CodeIgniter. It provides a simple and efficient way to retrieve data from a database and generate the necessary JSON response required by DataTables.
- Download the
Datatables_server_side.php
file from this repository. - Place the
Datatables_server_side.php
file in your CodeIgniter application'sapplication/libraries
directory.
To use the Datatables Server Side library, follow these steps:
- Load the library in your controller:
$this->load->library('datatables_server_side', $params);
Replace $params with an array containing the necessary configuration parameters (see Configuration section for details).
Call the process() method to generate the JSON response:
$this->datatables_server_side->process();
Configure your DataTables instance to make an AJAX request to the URL corresponding to your controller method.
The library accepts an array of configuration parameters during initialization. The available parameters are:
table: The database table to fetch data from. primary_key: The primary key of the table. columns: An array of column names to fetch. where: A WHERE clause to filter the data (optional). joins: An array of JOIN statements (optional). group_by: An array of column names to group by (optional).
public function method($param = null)
{
// If you have optional parameters for where clause.
$where = [];
if ($param != null) {
$where = ['table.column' => $param];
}
// Load the 'datatables_server_side' library
$this->load->library('datatables_server_side', [
'table' => 'table1', // The name of the main table
'primary_key' => 'id', // The primary key column of the main table
'columns' => ['table1.column1', 'table2.column2'], // The columns to fetch from the main table and table2
'where' => array_merge($where, [
'table1.column1' => 'value1', // WHERE condition for table1.column1
'table2.column2' => 'value2' // WHERE condition for table2.column2
]),
'joins' => [
['table2', 'table1.column = table2.column', 'left'], // LEFT JOIN with table2 on column equality
['table3', 'table1.column = table3.column', 'inner'] // INNER JOIN with table3 on column equality
],
'group_by' => ['table1.column1'] // GROUP BY clause for table1.column1
]);
$this->datatables_server_side->process();
}
<tbody id="server_side_dt"></tbody>
<script>
$(window).on('load', function() {
$('#server_side_dt').DataTable({
processing: true,
serverSide: true,
ajax: "<?= site_url("Controller/method"); ?>", // 'Controller/method/param'
columns:[
{data: 0},
{
data: 0, render: function (id, type, row) {
return `<a href="<?= site_url("Controller/method/"); ?>${id}" target="_blank">
${id}
</a>`;
}
},
{data: 1}
],
"order": [[0, "DESC"]],
});
});
</script>
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on GitHub.
This library is licensed under the MIT License.