CRUD Datatables Server Side Using Ignited Datatables
CRUD Datatables Server Side Using Ignited Datatables
If you want to make load millions record faster. You need to learn
really awesome.
I'm pretty sure you're familiar with datatables, that's why you're
12,000 records, problems arise. The data is loaded very slowly and
Hmmm..,
manually.
I try to use this library. It is proven to load data 12,000 records much
faster.
Awesome right.
rendering, etc.
How lucky you are finding this article. Because I will explore
Step 1. Preparation
This is important!
If you missed this step, it means you missed the whole of this tutorial.
The better your preparation, the more likely you will succeed in
website: www.codeigniter.com
2. Jquery
website: www.jquery.com
3. Bootstrap
website: www.getbootstrap.com
4. Ignited-datatables
github: https://github.com/IgnitedDatatables/Ignited-Datatables
5. Datatables
and has integrated with filters, show per page, and pagination.
link: https://datatables.net/download/index.
6. Bootstrap Datatables
link: https://datatables.net/manual/styling/bootstrap
(DBMS).
But,
If you are using other DBMS like Oracle, SQL Server, Maria DB, or
MongoDB.
No, Problem!
named crud_ignited.
After that,
3 category_name VARCHAR(50)
4 )ENGINE INNODB;
And then create one more table by following this query:
1
CREATE TABLE product(
2 product_code VARCHAR(10) PRIMARY KEY,
3 product_name VARCHAR(100),
4 product_price DOUBLE,
5 product_category_id INT,
like this:
After that,
2 values ('Drinks'),('Foods');
Like this:
1. Autoload.php
application/config/autoload.php
like this:
1 $autoload['libraries'] = array();
2 $autoload['helper'] = array();
1 $autoload['libraries'] = array('database');
2 $autoload['helper'] = array('url');
2. Config.php
application/config/config.php
like this:
Open config.php file using text editor, and then find this code:
1 $config['base_url'] = '';
1 $config['base_url'] = 'http://localhost/crud_serverside/';
3. Database.php
application/config/database.php
like this:
Open database.php file using text editor, and then find this code:
1 $active_group = 'default';
$query_builder = TRUE;
2
3
$db['default'] = array(
4
'dsn' => '',
5
'hostname' => 'localhost',
6
'username' => '',
7
'password' => '',
8 'database' => '',
9 'dbdriver' => 'mysqli',
10 'dbprefix' => '',
23
24
1 $active_group = 'default';
2 $query_builder = TRUE;
4 $db['default'] = array(
22 );
23
24
Step 5. Controller
Go ahead and create a controller file controllers/Crud.php with the
1 <?php
function __construct(){
3
parent::__construct();
4
$this->load->library('datatables'); //load library ignited-dataTable
5
$this->load->model('crud_model'); //load model crud_model
6
}
7 function index(){
8 $x['category']=$this->crud_model->get_category();
9 $this->load->view('crud_view',$x);
10 }
11
header('Content-Type: application/json');
13
echo $this->crud_model->get_all_product();
14
}
15
16
function save(){ //insert record method
17
$this->crud_model->insert_product();
18 redirect('crud');
19 }
20
23 redirect('crud');
}
24
25
function delete(){ //delete record method
26
$this->crud_model->delete_product();
27
redirect('crud');
28
}
29
30 }
31
32
33
Step 6. Model
Go ahead and create a model file models/Crud_model.php with the
1 <?php
function get_category(){
4
$result=$this->db->get('categories');
5
return $result;
6
}
7
//generate dataTable serverside method
8 function get_all_product() {
9 $this->datatables->select('product_code,product_name,product_price,category_id,c
10 $this->datatables->from('product');
11 $this->datatables->join('categories', 'product_category_id=category_id');
12 $this->datatables->add_column('view', '<a href="javascript:void(0);" class="edit
category="$4">Edit</a> <a href="javascript:void(0);" class="delete_record btn btn-dan
13code="$1">Delete</a>','product_code,product_name,product_price,category_id,category_na
14 return $this->datatables->generate();
15 }
25 }
27 function update_product(){
$product_code=$this->input->post('product_code');
28
$data=array(
29
'product_name' => $this->input->post('product_name'),
30
'product_price' => $this->input->post('price'),
31
'product_category_id' => $this->input->post('category')
32
);
33
$this->db->where('product_code',$product_code);
34
$result=$this->db->update('product', $data);
35 return $result;
36 }
38 function delete_product(){
39 $product_code=$this->input->post('product_code');
$this->db->where('product_code',$product_code);
40
$result=$this->db->delete('product');
41
return $result;
42
43 }
44}
45
46
Step 7. View
Go ahead and create a view file views/crud_view.php with the
1 <html lang="en">
2 <head>
<meta charset="utf-8">
3
<title>Crud ignited datatables in CodeIgniter</title>
4
<link href="<?php echo base_url().'assets/css/bootstrap.css'?>" rel="stylesheet"
5
<link href="<?php echo base_url().'assets/css/jquery.datatables.min.css'?>" re
6
<link href="<?php echo base_url().'assets/css/dataTables.bootstrap.css'?>" rel="
7
</head>
8 <body>
9 <div class="container">
10 <h2>Product List</h2>
19 </tr>
20 </thead>
21 </table>
</div>
22
23
<!-- Modal Add Product-->
24
<form id="add-row-form" action="<?php echo site_url('crud/save');?>" method="
25
<div class="modal fade" id="myModalAdd" tabindex="-1" role="dialog" aria-lab
26
<div class="modal-dialog">
27 <div class="modal-content">
28 <div class="modal-header">
29 <button type="button" class="close" data-dismiss="modal" aria
31 </div>
<div class="modal-body">
32
<div class="form-group">
33
<input type="text" name="product_code" class="form-contro
34
</div>
35
<div class="form-group">
36 <input type="text" name="product_name" class="form-contro
37 </div>
38 <div class="form-group">
<option value="<?php e
41 >category_name;?></option>
42 <?php endforeach;?>
43 </select>
44 </div>
45 <div class="form-group">
48
</div>
49
<div class="modal-footer">
50
51 <button type="button" class="btn btn-default" data-dismiss="
</div>
53
</div>
54
</div>
55
</div>
56
</form>
57
61 <div class="modal-dialog">
62 <div class="modal-content">
<div class="modal-header">
63
<button type="button" class="close" data-dismiss="modal" aria
64
<h4 class="modal-title" id="myModalLabel">Update Product</h4
65
</div>
66
<div class="modal-body">
67 <div class="form-group">
68 <input type="text" name="product_code" class="form-contro
69 </div>
70 <div class="form-group">
</div>
72
<div class="form-group">
73
<select name="category" class="form-control" required>
74
<?php foreach ($category->res
75
<option value="<?php echo
76 <?php endforeach;?>
77 </select>
78 </div>
79 <div class="form-group">
</div>
81
82
83 </div>
84 <div class="modal-footer">
<div class="modal-dialog">
95
<div class="modal-content">
96
<div class="modal-header">
97
<button type="button" class="close" data-dismiss="modal" aria
98
<h4 class="modal-title" id="myModalLabel">Delete Product</h4
99 </div>
100 <div class="modal-body">
103 </div>
<div class="modal-footer">
104
<button type="button" class="btn btn-default" data-dismiss="
105
<button type="submit" class="btn btn-success">Yes</button>
106
</div>
107
</div>
108 </div>
109 </div>
110 </form>
111
116
<script>
117
$(document).ready(function(){
118
// Setup datatables
119
$.fn.dataTableExt.oApi.fnPagingInfo = function(oSettings)
120 {
121 return {
122 "iStart": oSettings._iDisplayStart,
"iTotal": oSettings.fnRecordsTotal(),
125
"iFilteredTotal": oSettings.fnRecordsDisplay(),
126
"iPage": Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLen
127
"iTotalPages": Math.ceil(oSettings.fnRecordsDisplay() / oSettings._i
128
};
129 };
130
.off('.DT')
135
.on('input.DT', function() {
136
api.search(this.value).draw();
137
});
138
},
139 oLanguage: {
140 sProcessing: "loading..."
141 },
145 columns: [
{"data": "product_code"},
146
{"data": "product_name"},
147
//render number format for price
148
{"data": "product_price", render: $.fn.dataTable.render.nu
149
{"data": "category_name"},
150 {"data": "view"}
151 ],
159
});
160 // end setup datatables
161 // get Edit Records
162 $('#mytable').on('click','.edit_record',function(){
var price=$(this).data('price');
165
var category=$(this).data('category');
166
$('#ModalUpdate').modal('show');
167
$('[name="product_code"]').val(code);
168
$('[name="product_name"]').val(name);
169 $('[name="price"]').val(price);
170 $('[name="category"]').val(category);
171 });
$('#mytable').on('click','.delete_record',function(){
174
175 var code=$(this).data('code');
176 $('#ModalDelete').modal('show');
$('[name="product_code"]').val(code);
177
});
178
// End delete Records
179
});
180
</script>
181 </body>
182 </html>
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Please click add new button to add new record, click edit button to
If you want to see how awesome this is. Go ahead insert more than
20000 records.
If you feel this tutorial is useful for you. Please share! Perhaps, this
CONCLUSION
quality.