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, Here also we have an option to select all or uncheck all records of the table.

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

Checkout other tutorials of jQuery,

delete_mutiple_using_jquery

We’ll implement following Functionality

  • Check all & Uncheck all checkboxes using jQuery
  • Delete a single row from a table using PHP(Server-side) OR jQuery(Client-side)
  • Delete multiple rows from a table with Empty validation using PHP(Server-side) OR jQuery(Client-side)

Simple jQuery Script to Delete Single/Multiple Selected Rows

Step 1: We have included 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.

<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 records of the table. 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 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();
			}
	});

Demo & Download Source Code

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

6 thoughts on “How To Delete Multiple Selected Rows Using jQuery

Leave a Reply

Your email address will not be published.