Php

How To Delete Multiple Selected Rows Using jQuery

In this jQuery tutorial, I will let you know how to delete multiple records from the table using jQuery with Ajax, You’ll explore options to select all or uncheck all records without reloading the page.

This is a very common functionality of any HTML table listing and, nobody wants to reload the page after the delete record. The methods below are used to delete selected rows from the grid with the help of Ajax.

Check out other tutorials on jQuery,

We’ll implement the following Functionality

  • Enabling users to check all or uncheck all checkboxes using jQuery.
  • Deleting a single row from a table using either PHP on the server-side or jQuery on the client-side.
  • Deleting multiple rows from a table with empty validation, employing either PHP on the server-side or jQuery on the client-side.

Simple jQuery Script to Delete Single/Multiple Selected Rows

Step 1: We have included the jQuery and bootstrap files.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

Step 2: Let’s define HTML table rows.

Related Post
<div class="container" style="padding:0 250px;">
<h1>A Simple Example Delete Multile Rows Using PHP and jQuery</h1>
<div class="row well">
 <a type="button" class="btn btn-primary pull-right delete_all">Delete Selected</a>
</div>
<table id="employee_grid" class="table table-condensed table-hover table-striped bootgrid-table" width="60%" cellspacing="0">
   <thead>
      <tr>
         <th><input type="checkbox" id="master"></th>
         <th>Name</th>
         <th>Salary</th>
         <th>Age</th>
   <th class="pull-right">Delete</th>
      </tr>
   </thead>
   <tbody>
      <tr data-row-id="1">
         <td><input type="checkbox" class="sub_chk" data-id="1"></td>
         <td>Tiger Nixon</td>
         <td>320800</td>
         <td>61</td>
   <td><a class="remove-row pull-right" targetDiv="" data-id="1" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td>
      </tr>
      <tr data-row-id="5">
         <td><input type="checkbox" class="sub_chk" data-id="5"></td>
         <td>Airi Satou</td>
         <td>162700</td>
         <td>33</td>
   <td><a class="remove-row pull-right" targetDiv="" data-id="5" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td>
      </tr>
      <tr data-row-id="6">
         <td><input type="checkbox" class="sub_chk" data-id="6"></td>
         <td>Brielle Williamson</td>
         <td>372000</td>
         <td>61</td>
   <td><a class="remove-row pull-right" targetDiv="" data-id="6" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td>
      </tr>
      <tr data-row-id="7">
         <td><input type="checkbox" class="sub_chk" data-id="7"></td>
         <td>Herrod Chandler</td>
         <td>137500</td>
         <td>59</td>
   <td><a class="remove-row pull-right" targetDiv="" data-id="7" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td>
      </tr>
   </tbody>
</table>
</div>

Step 3: This function is used for selecting all table records. You can use this function to Check all & Uncheck all elements.

jQuery('#master').on('click', function(e) {
 if($(this).is(':checked',true))  
 {
  $(".sub_chk").prop('checked', true);  
 }  
 else  
 {  
  $(".sub_chk").prop('checked',false);  
 }  
});

Step 4: Define the jQuery function to delete all records using Ajax request(Server-Side) or jQuery(Client-Side).

jQuery('.delete_all').on('click', function(e) { 
var allVals = [];  
  $(".sub_chk:checked").each(function() {  
   allVals.push($(this).attr('data-id'));
  });  
  //alert(allVals.length); return false;  
  if(allVals.length &lt;=0)  
  {  
   alert("Please select row.");  
  }  
  else {  
   //$("#loading").show(); 
   WRN_PROFILE_DELETE = "Are you sure you want to delete this row?";  
   var check = confirm(WRN_PROFILE_DELETE);  
   if(check == true){  
    //for server side
    /*
    var join_selected_values = allVals.join(","); 
    
    $.ajax({   
      
     type: "POST",  
     url: "delete.php",  
     cache:false,  
     data: 'ids='+join_selected_values,  
     success: function(response)  
     {   
      $("#loading").hide();  
      $("#msgdiv").html(response);
      //referesh table
     }   
    });*/              //for client side
     $.each(allVals, function( index, value ) {
      $('table tr').filter("[data-row-id='" + value + "']").remove();
     });
    

   }  
  }  
 });

JavaScript Function to Delete Single Record

Step 5: Below jQuery script is used to delete single records from the HTML table.

jQuery('.remove-row').on('click', function(e) {
  WRN_PROFILE_DELETE = "Are you sure you want to delete this row?";  
   var check = confirm(WRN_PROFILE_DELETE);  
   if(check == true){
    $('table tr').filter("[data-row-id='" + $(this).attr('data-id') + "']").remove();
   }
 });

Conclusion:

in this tutorial, You have learned the following functionality:

  1. Initialization: Start by including jQuery and Bootstrap files for the necessary functionalities.
  2. HTML Table: Define the structure of the HTML table with checkboxes to select rows and a delete button to initiate the deletion process.
  3. Selecting Rows: Implement a function to select all table records, allowing users to easily check or uncheck all elements.
  4. Deleting Records: Define a jQuery function to delete selected rows either via Ajax request (server-side) or client-side manipulation. Users are prompted for confirmation before deletion.
  5. Deleting Single Record: Implement a jQuery script to delete single records from the HTML table upon user confirmation.

Demo & Download Source Code

Please feel free to send queries to me using the comment section.

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories