1<?php
2 /*PHP CONTROLLER*/
3 defined('BASEPATH') OR exit('No direct script access allowed');
4class Item extends CI_Controller {
5 /** * Get All Data from this method. * * @return Response */
6 public function __construct() {
7 parent::__construct(); $this->load->database();
8 } /** * Get All Data from this method. * * @return Response */
9 public function index() {
10 $data['data'] = $this->db->get("items")->result();
11 $this->load->view('item', $data);
12 } /** * Get All Data from this method. * * @return Response */
13 public function deleteAll() {
14 $ids = $this->input->post('ids');
15 $this->db->where_in('id', explode(",", $ids));
16 $this->db->delete('items');
17 echo json_encode(['success'=>"Item Deleted successfully."]);
18 } }
19?>
20/*php route file*/
21 <?php
22defined('BASEPATH') OR exit('No direct script access allowed');
23
24
25$route['default_controller'] = 'welcome';
26$route['404_override'] = '';
27$route['translate_uri_dashes'] = FALSE;
28
29
30$route['item'] = "item";
31$route['itemDelete']['post'] = "item/deleteAll";
32?>
33 /*Tables
34 CREATE TABLE IF NOT EXISTS `items` (
35 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
36 `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
37 `description` text COLLATE utf8_unicode_ci NOT NULL,
38 PRIMARY KEY (`id`)
39) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;
40*/
41 /*views
42 <!DOCTYPE html>
43<html>
44<head>
45 <title>how to delete multiple records using checkbox in codeigniter - itsolutionstuff.com</title>
46 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
47 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
48</head>
49<body>
50<div class="container">
51<div class="row">
52 <div class="col-lg-12 margin-tb">
53 <div class="pull-left">
54 <h2>how to delete multiple records using checkbox in codeigniter - itsolutionstuff.com</h2>
55 </div>
56 </div>
57</div>
58
59<button style="margin-bottom: 10px" class="btn btn-primary delete_all" data-url="/itemDelete">Delete All Selected</button>
60
61<table class="table table-bordered" style="margin-top:20px">
62
63 <thead>
64 <tr>
65 <th width="50px"><input type="checkbox" id="master"></th>
66 <th>Title</th>
67 <th>Description</th>
68 </tr>
69 </thead>
70
71 <tbody>
72 <?php foreach ($data as $item) { ?>
73 <tr>
74 <td><input type="checkbox" class="sub_chk" data-id="<?php echo $item->id; ?>"></td>
75 <td><?php echo $item->title; ?></td>
76 <td><?php echo $item->description; ?></td>
77 </tr>
78 <?php } ?>
79 </tbody>
80
81</table>
82</div>
83
84<script type="text/javascript">
85 $(document).ready(function () {
86
87 $('#master').on('click', function(e) {
88 if($(this).is(':checked',true))
89 {
90 $(".sub_chk").prop('checked', true);
91 } else {
92 $(".sub_chk").prop('checked',false);
93 }
94 });
95
96 $('.delete_all').on('click', function(e) {
97
98 var allVals = [];
99 $(".sub_chk:checked").each(function() {
100 allVals.push($(this).attr('data-id'));
101 });
102
103 if(allVals.length <=0)
104 {
105 alert("Please select row.");
106 } else {
107
108 var check = confirm("Are you sure you want to delete this row?");
109 if(check == true){
110
111 var join_selected_values = allVals.join(",");
112
113 $.ajax({
114 url: $(this).data('url'),
115 type: 'POST',
116 data: 'ids='+join_selected_values,
117 success: function (data) {
118 console.log(data);
119 $(".sub_chk:checked").each(function() {
120 $(this).parents("tr").remove();
121 });
122 alert("Item Deleted successfully.");
123 },
124 error: function (data) {
125 alert(data.responseText);
126 }
127 });
128
129 $.each(allVals, function( index, value ) {
130 $('table tr').filter("[data-row-id='" + value + "']").remove();
131 });
132 }
133 }
134 });
135 });
136</script>
137
138</body>
139</html>
140*/