raliable / ci4-datatables
A DataTables library for CodeIgniter 4
Installs: 58
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/raliable/ci4-datatables
Requires
- php: ^8.0
- codeigniter4/framework: ^4.5
README
This library provides a simple and efficient way to implement server-side processing for DataTables in CodeIgniter 4 applications.
Installation
You can install this library via Composer. Run the following command in your project directory:
composer require raliable/ci4-datatables
Usage
1. Load the Library
In your controller, load the DataTables library and configure it as needed:
<?php namespace App\Controllers; use \Raliable\DataTables\DataTables; use CodeIgniter\API\ResponseTrait; class DataTableController extends BaseController { use ResponseTrait; public function index() { return view('datatable_view'); } public function ajax_list() { $config = [ 'table' => 'your_table_name', ]; $datatables = new DataTables($config); $datatables->select('id, name, address, email') ->join('another_table', 'another_table.foreign_key = your_table_name.id', 'left') ->where('status', 1) ->orderBy('id', 'asc'); return $datatables->generate(); } }
2. Configure Your View
Create a view file (e.g., datatable_view.php) to display the DataTable:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DataTables Example</title> <link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script> <script> $(document).ready(function() { $('#example').DataTable({ "processing": true, "serverSide": true, "ajax": { "url": "<?= base_url('datatablecontroller/ajax_list') ?>", "type": "POST", "data": function (d) { d.<?= csrf_token() ?> = "<?= csrf_hash() ?>"; } }, "columns": [ { "data": "id", "bSortable": false, "searchable": false }, { "data": "name", "bSortable": true, "searchable": true }, { "data": "address", "bSortable": false, "searchable": true }, { "data": "email", "bSortable": true, "searchable": true } ] }); }); </script> </head> <body> <table id="example" class="display" style="width:100%"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Address</th> <th>Email</th> </tr> </thead> </table> </body> </html>
Methods
select(string $columns): Select the columns to be returned.join(string $table, string $fk, string $type = null): Join another table.where(string $keyCondition, $val = null): Add awhereclause.orWhere(string $keyCondition, $val = null): Add anorWhereclause.whereIn(string $keyCondition, array $val = []): Add awhereInclause.orderBy($column, string $order = 'ASC'): Add anorderByclause.groupBy(string $groupBy): Add agroupByclause.generate(bool $raw = false): Generate the DataTables response. Ifrawistrue, returns an array, otherwise returns a JSON response.