Answers for "multiple delete in codeigniter with checkbox"

PHP
1

multiple delete in codeigniter with checkbox

<?php
  /*PHP CONTROLLER*/
  defined('BASEPATH') OR exit('No direct script access allowed');
class Item extends CI_Controller {
  /**     * Get All Data from this method.     *     * @return Response    */ 
  public function __construct() {    
    parent::__construct();       $this->load->database();   
  }     /**     * Get All Data from this method.     *     * @return Response    */
  public function index()    {    
    $data['data'] = $this->db->get("items")->result();     
    $this->load->view('item', $data);  
  }     /**     * Get All Data from this method.     *     * @return Response    */  
  public function deleteAll()    {    
    $ids = $this->input->post('ids');      
    $this->db->where_in('id', explode(",", $ids));  
    $this->db->delete('items');     
    echo json_encode(['success'=>"Item Deleted successfully."]);  
  } }
?>
/*php route file*/
  <?php
defined('BASEPATH') OR exit('No direct script access allowed');


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


$route['item'] = "item";
$route['itemDelete']['post'] = "item/deleteAll";
?>
  /*Tables
  CREATE TABLE IF NOT EXISTS `items` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;
*/
  /*views
  <!DOCTYPE html>
<html>
<head>
    <title>how to delete multiple records using checkbox in codeigniter - itsolutionstuff.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>how to delete multiple records using checkbox in codeigniter - itsolutionstuff.com</h2>
        </div>
    </div>
</div>
 
<button style="margin-bottom: 10px" class="btn btn-primary delete_all" data-url="/itemDelete">Delete All Selected</button>
 
<table class="table table-bordered" style="margin-top:20px">
 
  <thead>
      <tr>
          <th width="50px"><input type="checkbox" id="master"></th>
          <th>Title</th>
          <th>Description</th>
      </tr>
  </thead>
 
  <tbody>
   <?php foreach ($data as $item) { ?>      
      <tr>
          <td><input type="checkbox" class="sub_chk" data-id="<?php echo $item->id; ?>"></td>
          <td><?php echo $item->title; ?></td>
          <td><?php echo $item->description; ?></td>
      </tr>
   <?php } ?>
  </tbody>
 
</table>
</div>
 
<script type="text/javascript">
    $(document).ready(function () {
 
        $('#master').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".sub_chk").prop('checked', true);  
         } else {  
            $(".sub_chk").prop('checked',false);  
         }  
        });
 
        $('.delete_all').on('click', function(e) {
 
            var allVals = [];  
            $(".sub_chk:checked").each(function() {  
                allVals.push($(this).attr('data-id'));
            });  
 
            if(allVals.length <=0)  
            {  
                alert("Please select row.");  
            }  else {  
 
                var check = confirm("Are you sure you want to delete this row?");  
                if(check == true){  
 
                    var join_selected_values = allVals.join(","); 
 
                    $.ajax({
                        url: $(this).data('url'),
                        type: 'POST',
                        data: 'ids='+join_selected_values,
                        success: function (data) {
                          console.log(data);
                          $(".sub_chk:checked").each(function() {  
                              $(this).parents("tr").remove();
                          });
                          alert("Item Deleted successfully.");
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    });
 
                  $.each(allVals, function( index, value ) {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }  
            }  
        });
    });
</script>
 
</body>
</html>
*/
Posted by: Guest on August-28-2021

Code answers related to "multiple delete in codeigniter with checkbox"

Browse Popular Code Answers by Language