(Codeigniter) Ajax CRUD using Bootstrap modals and DataTables
Screenshot :
Ajax CRUD DataTables
Bootstrap Modals (Adding data)
Bootstrap Modals (Editing data)
Required and Included on this source :
- PHP+ MySQL or you may use XAMPP –> Download
- Codeigniter 3.0 –> Download
- jQuery 2.1.4 –> Download
- Twitter Bootstrap 3.3.5 –> Download
- Bootstrap-datepicker 1.4.0 –> Download
- DataTables 1.10.7 –> Download
Database and Query :
03 | CREATE TABLE `persons` ( |
04 | ` id ` int ( 11 ) unsigned NOT NULL AUTO_INCREMENT, |
05 | `firstName` varchar( 100 ) DEFAULT NULL, |
06 | `lastName` varchar( 100 ) DEFAULT NULL, |
07 | `gender` enum( 'male' , 'female' ) DEFAULT NULL, |
08 | `address` varchar( 200 ) DEFAULT NULL, |
09 | `dob` date DEFAULT NULL, |
11 | ) ENGINE = InnoDB DEFAULT CHARSET = utf8; |
13 | - - example data persons |
14 | INSERT INTO `persons` (` id `, `firstName`, `lastName`, `gender`, `address`, `dob`) |
16 | ( 1 , 'Airi' , 'Satou' , 'female' , 'Tokyo' , '1964-03-04' ), |
17 | ( 2 , 'Garrett' , 'Winters' , 'male' , 'Tokyo' , '1988-09-02' ), |
18 | ( 3 , 'John' , 'Doe' , 'male' , 'Kansas' , '1972-11-02' ), |
19 | ( 4 , 'Tatyana' , 'Fitzpatrick' , 'male' , 'London' , '1989-01-01' ), |
20 | ( 5 , 'Quinn' , 'Flynn' , 'male' , 'Edinburgh' , '1977-03-24' ); |
Configuring Codeigniter :
assets folder structure :
Assets Folder Codeigniter
Routing
path : config/route.php
1 | $route[ 'default_controller' ] = 'person' ; |
2 | $route[ '404_override' ] = ''; |
3 | $route[ 'translate_uri_dashes' ] = FALSE; |
Base URL Cofig (required if using Codeigniter 3.0.3 or later)
path : config/config.php
see for dynamic base_url :
Dynamic base_url() and site_url() Codeigniter 3.0.3 +
in this source leave empty because using codeigniter 3.0
Source Code
model : Person_model.php
path : application/models/Person_model.php
01 | 'desc' ); / / default order |
03 | public function __construct() |
05 | parent::__construct(); |
06 | $this - >load - >database(); |
09 | private function _get_datatables_query() |
12 | $this - >db - > from ($this - >table); |
16 | foreach ($this - >column_search as $item) / / loop column |
18 | if ($_POST[ 'search' ][ 'value' ]) / / if datatable send POST for search |
21 | if ($i = = = 0 ) / / first loop |
23 | $this - >db - >group_start(); / / open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND. |
24 | $this - >db - >like($item, $_POST[ 'search' ][ 'value' ]); |
28 | $this - >db - >or_like($item, $_POST[ 'search' ][ 'value' ]); |
31 | if (count($this - >column_search) - 1 = = $i) / / last loop |
32 | $this - >db - >group_end(); / / close bracket |
37 | if (isset($_POST[ 'order' ])) / / here order processing |
39 | $this - >db - >order_by($this - >column_order[$_POST[ 'order' ][ '0' ][ 'column' ]], $_POST[ 'order' ][ '0' ][ 'dir' ]); |
41 | else if (isset($this - >order)) |
43 | $order = $this - >order; |
44 | $this - >db - >order_by(key($order), $order[key($order)]); |
48 | function get_datatables() |
50 | $this - >_get_datatables_query(); |
51 | if ($_POST[ 'length' ] ! = - 1 ) |
52 | $this - >db - >limit($_POST[ 'length' ], $_POST[ 'start' ]); |
53 | $query = $this - >db - >get(); |
54 | return $query - >result(); |
57 | function count_filtered() |
59 | $this - >_get_datatables_query(); |
60 | $query = $this - >db - >get(); |
61 | return $query - >num_rows(); |
64 | public function count_all() |
66 | $this - >db - > from ($this - >table); |
67 | return $this - >db - >count_all_results(); |
70 | public function get_by_id($ id ) |
72 | $this - >db - > from ($this - >table); |
73 | $this - >db - >where( 'id' ,$ id ); |
74 | $query = $this - >db - >get(); |
79 | public function save($data) |
81 | $this - >db - >insert($this - >table, $data); |
82 | return $this - >db - >insert_id(); |
85 | public function update($where, $data) |
87 | $this - >db - >update($this - >table, $data, $where); |
88 | return $this - >db - >affected_rows(); |
91 | public function delete_by_id($ id ) |
93 | $this - >db - >where( 'id' , $ id ); |
94 | $this - >db - >delete($this - >table); |
Controller Person.php
path : applications/controllers/Person.php
01 | load - >model( 'person_model' , 'person' ); |
04 | public function index() |
06 | $this - >load - >helper( 'url' ); |
07 | $this - >load - >view( 'person_view' ); |
10 | public function ajax_list() |
12 | $ list = $this - >person - >get_datatables(); |
14 | $no = $_POST[ 'start' ]; |
15 | foreach ($ list as $person) { |
18 | $row[] = $person - >firstName; |
19 | $row[] = $person - >lastName; |
20 | $row[] = $person - >gender; |
21 | $row[] = $person - >address; |
22 | $row[] = $person - >dob; |
25 | $row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" onclick="edit_person(' ." person - = " " title=" Edit ">id." '".' )"><i class = "glyphicon glyphicon-pencil" >< / i> Edit< / a> |
26 | <a class = "btn btn-sm btn-danger" href = "javascript:void(0)" onclick = "delete_person('." person - = " " title=" Hapus ">id." ' ".')" ><i class = "glyphicon glyphicon-trash" >< / i> Delete< / a>'; |
32 | "draw" = > $_POST[ 'draw' ], |
33 | "recordsTotal" = > $this - >person - >count_all(), |
34 | "recordsFiltered" = > $this - >person - >count_filtered(), |
37 | / / output to json format |
38 | echo json_encode($output); |
41 | public function ajax_edit($ id ) |
43 | $data = $this - >person - >get_by_id($ id ); |
44 | echo json_encode($data); |
47 | public function ajax_add() |
50 | 'firstName' = > $this - > input - >post( 'firstName' ), |
51 | 'lastName' = > $this - > input - >post( 'lastName' ), |
52 | 'gender' = > $this - > input - >post( 'gender' ), |
53 | 'address' = > $this - > input - >post( 'address' ), |
54 | 'dob' = > $this - > input - >post( 'dob' ), |
56 | $insert = $this - >person - >save($data); |
57 | echo json_encode(array( "status" = > TRUE)); |
60 | public function ajax_update() |
63 | 'firstName' = > $this - > input - >post( 'firstName' ), |
64 | 'lastName' = > $this - > input - >post( 'lastName' ), |
65 | 'gender' = > $this - > input - >post( 'gender' ), |
66 | 'address' = > $this - > input - >post( 'address' ), |
67 | 'dob' = > $this - > input - >post( 'dob' ), |
69 | $this - >person - >update(array( 'id' = > $this - > input - >post( 'id' )), $data); |
70 | echo json_encode(array( "status" = > TRUE)); |
73 | public function ajax_delete($ id ) |
75 | $this - >person - >delete_by_id($ id ); |
76 | echo json_encode(array( "status" = > TRUE)); |
view : person_view.php
path : application/views/person_view.php
004 | <meta charset = "utf-8" > |
005 | <meta http - equiv = "X-UA-Compatible" content = "IE=edge" > |
006 | <meta name = "viewport" content = "width=device-width, initial-scale=1" > |
007 | <title>Ajax CRUD with Bootstrap modals and Datatables< / title> |
008 | <link href = "<?php echo base_url('assets/bootstrap/css/bootstrap.min.css')?>" rel = "stylesheet" > |
009 | <link href = "<?php echo base_url('assets/datatables/css/dataTables.bootstrap.css')?>" rel = "stylesheet" > |
010 | <link href = "<?php echo base_url('assets/bootstrap-datepicker/css/bootstrap-datepicker3.min.css')?>" rel = "stylesheet" > |
011 | <! - - HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries - - > |
012 | <! - - WARNING: Respond.js doesn't work if you view the page via file : / / - - > |
019 | <div class = "container" > |
020 | <h1 style = "font-size:20pt" >Ajax CRUD with Bootstrap modals and Datatables< / h1> |
025 | <button class = "btn btn-success" onclick = "add_person()" ><i class = "glyphicon glyphicon-plus" >< / i> Add Person< / button> |
026 | <button class = "btn btn-default" onclick = "reload_table()" ><i class = "glyphicon glyphicon-refresh" >< / i> Reload < / button> |
031 | <table id = "table" class = "table table-striped table-bordered" cellspacing = "0" width = "100%" > |
038 | <th>Date of Birth< / th> |
039 | <th style = "width:125px;" >Action< / th> |
051 | <th>Date of Birth< / th> |
058 | <script src = "<?php echo base_url('assets/jquery/jquery-2.1.4.min.js')?>" >< / script> |
059 | <script src = "<?php echo base_url('assets/bootstrap/js/bootstrap.min.js')?>" >< / script> |
060 | <script src = "<?php echo base_url('assets/datatables/js/jquery.dataTables.min.js')?>" >< / script> |
061 | <script src = "<?php echo base_url('assets/datatables/js/dataTables.bootstrap.js')?>" >< / script> |
062 | <script src = "<?php echo base_url('assets/bootstrap-datepicker/js/bootstrap-datepicker.min.js')?>" >< / script> |
065 | <script type = "text/javascript" > |
067 | var save_method; / / for save method string |
070 | $(document).ready(function() { |
073 | table = $( '#table' ).DataTable({ |
075 | "processing" : true, / / Feature control the processing indicator. |
076 | "serverSide" : true, / / Feature control DataTables' server - side processing mode. |
077 | "order" : [], / / Initial no order. |
079 | / / Load data for the table's content from an Ajax source |
081 | "url" : "<?php echo site_url('person/ajax_list')?>" , |
085 | / / Set column definition initialisation properties. |
088 | "targets" : [ - 1 ], / / last column |
089 | "orderable" : false, / / set not orderable |
096 | $( '.datepicker' ).datepicker({ |
098 | format : "yyyy-mm-dd" , |
099 | todayHighlight: true, |
100 | orientation: "top auto" , |
102 | todayHighlight: true, |
112 | $( '#form' )[ 0 ].reset(); / / reset form on modals |
113 | $( '.form-group' ).removeClass( 'has-error' ); / / clear error class |
114 | $( '.help-block' ).empty(); / / clear error string |
115 | $( '#modal_form' ).modal( 'show' ); / / show bootstrap modal |
116 | $( '.modal-title' ).text( 'Add Person' ); / / Set Title to Bootstrap modal title |
119 | function edit_person( id ) |
121 | save_method = 'update' ; |
122 | $( '#form' )[ 0 ].reset(); / / reset form on modals |
123 | $( '.form-group' ).removeClass( 'has-error' ); / / clear error class |
124 | $( '.help-block' ).empty(); / / clear error string |
126 | / / Ajax Load data from ajax |
128 | url : "<?php echo site_url('person/ajax_edit/')?>/" + id , |
131 | success: function(data) |
134 | $( '[name="id"]' ).val(data. id ); |
135 | $( '[name="firstName"]' ).val(data.firstName); |
136 | $( '[name="lastName"]' ).val(data.lastName); |
137 | $( '[name="gender"]' ).val(data.gender); |
138 | $( '[name="address"]' ).val(data.address); |
139 | $( '[name="dob"]' ).datepicker( 'update' ,data.dob); |
140 | $( '#modal_form' ).modal( 'show' ); / / show bootstrap modal when complete loaded |
141 | $( '.modal-title' ).text( 'Edit Person' ); / / Set title to Bootstrap modal title |
144 | error: function (jqXHR, textStatus, errorThrown) |
146 | alert( 'Error get data from ajax' ); |
151 | function reload_table() |
153 | table.ajax. reload (null,false); / / reload datatable ajax |
158 | $( '#btnSave' ).text( 'saving...' ); / / change button text |
159 | $( '#btnSave' ).attr( 'disabled' ,true); / / set button disable |
162 | if (save_method = = 'add' ) { |
163 | url = "<?php echo site_url('person/ajax_add')?>" ; |
165 | url = "<?php echo site_url('person/ajax_update')?>" ; |
168 | / / ajax adding data to database |
172 | data: $( '#form' ).serialize(), |
174 | success: function(data) |
177 | if (data.status) / / if success close modal and reload ajax table |
179 | $( '#modal_form' ).modal( 'hide' ); |
183 | $( '#btnSave' ).text( 'save' ); / / change button text |
184 | $( '#btnSave' ).attr( 'disabled' ,false); / / set button enable |
188 | error: function (jqXHR, textStatus, errorThrown) |
190 | alert( 'Error adding / update data' ); |
191 | $( '#btnSave' ).text( 'save' ); / / change button text |
192 | $( '#btnSave' ).attr( 'disabled' ,false); / / set button enable |
198 | function delete_person( id ) |
200 | if (confirm( 'Are you sure delete this data?' )) |
202 | / / ajax delete data to database |
204 | url : "<?php echo site_url('person/ajax_delete')?>/" + id , |
207 | success: function(data) |
209 | / / if success reload ajax table |
210 | $( '#modal_form' ).modal( 'hide' ); |
213 | error: function (jqXHR, textStatus, errorThrown) |
215 | alert( 'Error deleting data' ); |
224 | <! - - Bootstrap modal - - > |
225 | <div class = "modal fade" id = "modal_form" role = "dialog" > |
226 | <div class = "modal-dialog" > |
227 | <div class = "modal-content" > |
228 | <div class = "modal-header" > |
229 | <button type = "button" class = "close" data - dismiss = "modal" aria - label = "Close" ><span aria - hidden = "true" >×< / span>< / button> |
230 | <h3 class = "modal-title" >Person Form< / h3> |
232 | <div class = "modal-body form" > |
233 | <form action = "#" id = "form" class = "form-horizontal" > |
234 | < input type = "hidden" value = " " name=" id " / > |
235 | <div class = "form-body" > |
236 | <div class = "form-group" > |
237 | <label class = "control-label col-md-3" >First Name< / label> |
238 | <div class = "col-md-9" > |
239 | < input name = "firstName" placeholder = "First Name" class = "form-control" type = "text" > |
240 | <span class = "help-block" >< / span> |
243 | <div class = "form-group" > |
244 | <label class = "control-label col-md-3" >Last Name< / label> |
245 | <div class = "col-md-9" > |
246 | < input name = "lastName" placeholder = "Last Name" class = "form-control" type = "text" > |
247 | <span class = "help-block" >< / span> |
250 | <div class = "form-group" > |
251 | <label class = "control-label col-md-3" >Gender< / label> |
252 | <div class = "col-md-9" > |
253 | <select name = "gender" class = "form-control" > |
254 | <option value = ""> - - Select Gender - - < / option> |
255 | <option value = "male" >Male< / option> |
256 | <option value = "female" >Female< / option> |
258 | <span class = "help-block" >< / span> |
261 | <div class = "form-group" > |
262 | <label class = "control-label col-md-3" >Address< / label> |
263 | <div class = "col-md-9" > |
264 | <textarea name = "address" placeholder = "Address" class = "form-control" >< / textarea> |
265 | <span class = "help-block" >< / span> |
268 | <div class = "form-group" > |
269 | <label class = "control-label col-md-3" >Date of Birth< / label> |
270 | <div class = "col-md-9" > |
271 | < input name = "dob" placeholder = "yyyy-mm-dd" class = "form-control datepicker" type = "text" > |
272 | <span class = "help-block" >< / span> |
278 | <div class = "modal-footer" > |
279 | <button type = "button" id = "btnSave" onclick = "save()" class = "btn btn-primary" >Save< / button> |
280 | <button type = "button" class = "btn btn-danger" data - dismiss = "modal" >Cancel< / button> |
282 | < / div><! - - / .modal - content - - > |
283 | < / div><! - - / .modal - dialog - - > |
284 | < / div><! - - / .modal - - > |
285 | <! - - End Bootstrap modal - - > |
sumber