|
| 1 | +# CodeIgniter DataTables |
| 2 | + |
| 3 | +DataTables server-side for CodeIgniter, supported both for CodeIgniter 3 and CodeIgniter 4. |
| 4 | + |
| 5 | +**Note:** This library only handle the server-side part, you still needs to configure a client side like jQuery, DataTables library and including the styles. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +The requirements is base on what version of CodeIgniter you use. |
| 10 | +- [CodeIgniter 3 Requirements](https://codeigniter.com/userguide3/general/requirements.html) |
| 11 | +- [CodeIgniter 4 Requirements](https://codeigniter.com/user_guide/intro/requirements.html) |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +You just need to use composer and everything is done. |
| 16 | + |
| 17 | +```sh |
| 18 | +composer require ngekoding/codeigniter-datatables |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +Here is the basic example to use this library, you are freely make any changes for the client side, like defining searchable column, orderable column, etc... |
| 24 | + |
| 25 | +### CodeIgniter 3 Example |
| 26 | + |
| 27 | +```php |
| 28 | +// CodeIgniter 3 Example |
| 29 | + |
| 30 | +// Here we will select all fields from posts table |
| 31 | +// and make a join with categories table |
| 32 | +// Please note: we don't need to call ->get() here |
| 33 | +$queryBuilder = $this->db->select('p.*, c.name category') |
| 34 | + ->from('posts p') |
| 35 | + ->join('categories c', 'c.id=p.category_id'); |
| 36 | + |
| 37 | +/** |
| 38 | + * The first parameter is the query builder instance |
| 39 | + * and the second is the codeigniter version (3 or 4) |
| 40 | + */ |
| 41 | +$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, '3'); |
| 42 | +$datatables->generate(); // done |
| 43 | +``` |
| 44 | + |
| 45 | +### CodeIgniter 4 Example |
| 46 | + |
| 47 | +```php |
| 48 | +// CodeIgniter 4 Example |
| 49 | + |
| 50 | +$db = db_connect(); |
| 51 | +$queryBuilder = $db->from('posts p') |
| 52 | + ->select('p.*, c.name category') |
| 53 | + ->join('categories c', 'c.id=p.category_id'); |
| 54 | + |
| 55 | +$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, '4'); |
| 56 | +$datatables->generate(); // done |
| 57 | +``` |
| 58 | + |
| 59 | +**The above examples will give you for [ajax data source (arrays)](https://datatables.net/examples/ajax/simple.html), so you need to make sure the table header you makes for the client side is match with the ajax response. We will talk about the objects data source below.** |
| 60 | + |
| 61 | +### Client Side Examples |
| 62 | + |
| 63 | +You must include the jQuery and DataTables library. |
| 64 | + |
| 65 | +```php |
| 66 | +<link src="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css"> |
| 67 | + |
| 68 | +<table id="table-post"> |
| 69 | + <thead> |
| 70 | + <th>ID</th> |
| 71 | + <th>Title</th> |
| 72 | + <th>Category</th> |
| 73 | + <th>Description</th> |
| 74 | + </thead> |
| 75 | +</table> |
| 76 | + |
| 77 | +<script src="https://code.jquery.com/jquery-3.5.1.js"></script> |
| 78 | +<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script> |
| 79 | +<script> |
| 80 | +$('#table-post').DataTable({ |
| 81 | + processing: true, |
| 82 | + serverSide: true, |
| 83 | + ajax: { |
| 84 | + url: 'http://localhost/project/index.php/post/ajax_datatables', // Change with your own |
| 85 | + method: 'GET', // You are freely to use POST or GET |
| 86 | + } |
| 87 | +}) |
| 88 | +</script> |
| 89 | +``` |
| 90 | + |
| 91 | +## Objects Data Source |
| 92 | + |
| 93 | +As was mentioned above, the default data source we get is an arrays. It is easy also to get the objects data source. |
| 94 | + |
| 95 | +To get objects response, you just need to call `asObject()` method. |
| 96 | + |
| 97 | +```php |
| 98 | +$datatables->asObject() |
| 99 | + ->generate(); |
| 100 | +``` |
| 101 | + |
| 102 | +And then you can configure the client side with columns option to fit your data. |
| 103 | + |
| 104 | +```php |
| 105 | +$('#table-post').DataTable({ |
| 106 | + processing: true, |
| 107 | + serverSide: true, |
| 108 | + ajax: { |
| 109 | + url: 'http://localhost/project/index.php/post/ajax_datatables', |
| 110 | + method: 'GET', |
| 111 | + }, |
| 112 | + columns: [ |
| 113 | + { data: 'id' }, |
| 114 | + { data: 'title' }, |
| 115 | + { data: 'category' }, |
| 116 | + { data: 'description' } |
| 117 | + ] |
| 118 | +}) |
| 119 | +</script> |
| 120 | +``` |
| 121 | + |
| 122 | +## Some Others Settings |
| 123 | + |
| 124 | +Some basic functionalities already available, here is the full settings you can doing to this library. |
| 125 | + |
| 126 | +### Use class for spesify the CodeIgniter version |
| 127 | +```php |
| 128 | +// General, use the second param to define the version |
| 129 | +// The default is 4 |
| 130 | +$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, '3'); |
| 131 | + |
| 132 | +// CodeIgniter 3 |
| 133 | +$datatables = new Ngekoding\CodeIgniterDataTables\DataTablesCodeIgniter3($queryBuilder); |
| 134 | + |
| 135 | +// CodeIgniter 4 |
| 136 | +$datatables = new Ngekoding\CodeIgniterDataTables\DataTablesCodeIgniter4($queryBuilder); |
| 137 | + |
| 138 | +``` |
| 139 | + |
| 140 | +### Available Options |
| 141 | + |
| 142 | +```php |
| 143 | +$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder); |
| 144 | + |
| 145 | +// Return the output as objects instead of arrays |
| 146 | +$datatables->asObject(); |
| 147 | + |
| 148 | +// Only return title & category (accept string or array) |
| 149 | +$datatables->only(['title', 'category']); |
| 150 | + |
| 151 | +// Return all except the id |
| 152 | +// You may use one of only or except |
| 153 | +$datatables->except(['id']); |
| 154 | + |
| 155 | +// Format the output |
| 156 | +$datatables->format('title', function($value, $row) { |
| 157 | + return '<b>'.$value.'</b>'; |
| 158 | +}); |
| 159 | + |
| 160 | +// Add extra column |
| 161 | +$datatables->addColumn('action', function($row) { |
| 162 | + return '<a href="url/to/delete/post/'.$row->id.'">Delete</a>'; |
| 163 | +}); |
| 164 | + |
| 165 | +// Add squence number |
| 166 | +// The default key is `sequenceNumber` |
| 167 | +// You can change it with give the param |
| 168 | +$datatables->addSequenceNumber(); |
| 169 | +$datatables->addSequenceNumber('rowNumber'); // It will be rowNumber |
| 170 | + |
| 171 | +// Don't forget ot call generate to get the results |
| 172 | +$datatables->generate(); |
| 173 | +``` |
0 commit comments