Skip to content

Commit df9efea

Browse files
committed
Initial commit of codeigniter-datatables by ngekoding
0 parents  commit df9efea

File tree

8 files changed

+583
-0
lines changed

8 files changed

+583
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
tests
3+
composer.lock

README.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
```

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ngekoding/codeigniter-datatables",
3+
"description": "DataTables server-side for CodeIgniter",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Nur Muhammad",
9+
"email": "blog.nurmuhammad@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.6",
14+
"symfony/http-foundation": "^3.4",
15+
"greenlion/php-sql-parser": "^4.5"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Ngekoding\\CodeIgniterDataTables\\": "src"
20+
}
21+
}
22+
}

src/Config.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Ngekoding\CodeIgniterDataTables;
4+
5+
class Config
6+
{
7+
protected $ciVersion;
8+
9+
/**
10+
* Map the method to call base on CodeIgniter version
11+
* We use version 4 as the references name
12+
*/
13+
protected $methodsMapping = [
14+
'3' => [
15+
'countAllResults' => 'count_all_results',
16+
'orderBy' => 'order_by',
17+
'where' => 'where',
18+
'limit' => 'limit',
19+
'get' => 'get',
20+
'QBSelect' => 'qb_select',
21+
'getFieldNames' => 'list_fields',
22+
'getResult' => 'result',
23+
'getResultArray' => 'result_array',
24+
]
25+
];
26+
27+
public function __construct($ciVersion = '4')
28+
{
29+
$this->ciVersion = $ciVersion;
30+
}
31+
32+
public function get($name)
33+
{
34+
if (isset($this->methodsMapping[$this->ciVersion])) {
35+
return $this->methodsMapping[$this->ciVersion][$name];
36+
}
37+
return $name;
38+
}
39+
}

0 commit comments

Comments
 (0)