jQuery DataTable With A Delete button

In this blog, I will show how to use jQuery DataTable with the Delete button.

Why should we use jQuery Datatable

jQuery Datatable is very lightweight and gives us some inbuilt functionalities like searching and sorting etc.

In order to implement the jQuery Datatable, follow the below mentioned steps.

Step 1

Add jQuery script and jQuery Datatable CSS and JS inside the head section of the page. Below, I have given all those scripts.

  1. <!—Jquery Datatable css -->  
  2. <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">  
  3. <!—Jquery Js script -->  
  4. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>  
  5. <!—Jquery Datatable js script -->  
  6. <!-- DataTables -->  
  7. <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>  

Step 2

Design the HTML table. In the below code, as you can see, I have assigned an id to the table and its body. Before moving to the next step, please be sure that your table is in a proper format.

  1. <table id="userTable">  
  2.     <thead>  
  3.         <tr>  
  4.             <th> User Name </th>  
  5.             <th>Email Id</th>  
  6.             <th> Address </th>  
  7.             <th>Mobile Number</th>  
  8.             <th>Action</th>  
  9.         </tr>  
  10.     </thead>  
  11.     <tbody id="tBody"> </tbody>  
  12. </table>  

Step 3

Go to code behind of the page to create a function which will return the data to bind the table. I have created a class UserList with five auto-implemented properties, as you can see in the below code.

  1. public class UserList {  
  2.     public string UserID {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string UserName {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string EmailId {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Address {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string MobileNumber {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  

Step4

Now, create a method which will return the value to bind the table.

Note - Please focus on these points while creating a method.

  1. The method should be static because I have to call this method from jQuery AJAX.
  2. The method should be of public type.
  3. The method should be a WebMethod (available inside System.Web.Services namespace).
  4. In this case, return type is List<UserList>.
    1. [WebMethod]  
    2. public static List < UserList > ReturnUserList() {  
    3.     List < UserList > userList = new List < UserList > ();  
    4.     userList.Add(new UserList() {  
    5.         UserID = "1", UserName = "Amit", EmailId = "amit@gmail.com", Address = "Delhi", MobileNumber = "+9197"  
    6.     });  
    7.     userList.Add(new UserList() {  
    8.         UserID = "2", UserName = "Amit", EmailId = "Amit@gmail.com", Address = "Patna", MobileNumber = "+91975"  
    9.     });  
    10.     userList.Add(new UserList() {  
    11.         UserID = "3", UserName = "Abhi", EmailId = "Abhi@gmail.com", Address = "Allahabad", MobileNumber = "+91967"  
    12.     });  
    13.     userList.Add(new UserList() {  
    14.         UserID = "4", UserName = "Ankit", EmailId = "Ankit@gmail.com", Address = "Bhagalpur", MobileNumber = "+91697"  
    15.     });  
    16.     userList.Add(new UserList() {  
    17.         UserID = "5", UserName = "Denesh", EmailId = "Denesh@gmail.com", Address = "Patna", MobileNumber = "+91937"  
    18.     });  
    19.     userList.Add(new UserList() {  
    20.         UserID = "6", UserName = "Rahul", EmailId = "amit@gmail.com", Address = "New Delhi", MobileNumber = "+9197"  
    21.     });  
    22.     return userList;  
    23. }  

Step 5

Now, call the webmethod ReturnUserList from jQuery AJAX. Write the code inside the head section of .aspx page.

I used UserId property to define all the rows uniquely. By Selecting the UserID property, I will delete the respective row.
 
Now, each button has onclick event with its respective id parameter. If I will click on any button, the CallMe function will get executed and the respective id parameter will be passed.
 
Inside the CallMe function, we can identify which button is clicked by its id and we can perform any operation, like update, delete etc.
  1. <script>  
  2.     $(document).ready(function() {  
  3.         $.ajax({  
  4.             type: "POST",  
  5.             url: "WebForm.aspx/ReturnUserList",  
  6.             dataType: "json",  
  7.             contentType: "application/json",  
  8.             success: function(response) {  
  9.                 $.each(response.d, function(index, value) {  
  10.                     var tr = "<tr>";  
  11.                     tr += "<td>" + value.UserName + "</td>";  
  12.                     tr += "<td>" + value.EmailId + "</td>";  
  13.                     tr += "<td>" + value.Address + "</td>";  
  14.                     tr += "<td>" + value.MobileNumber + "</td>";  
  15.                     tr += "<td>" + "<input type='button' id='" + value.UserID + "' onclick='CallMe(" + value.UserID + ")' value='Delete'>" + "</td>" + "</tr>";  
  16.                     $("#tBody").append(tr);  
  17.                 });  
  18.                 $("#userTable").DataTable();  
  19.             }  
  20.         });  
  21.     });  
  22. </script>  

Step 6

Now, define the CallMe function of jQuery and assign one parameter which will get the button id to perform the operation. Write the code to perform any operation on the id.

  1. function CallMe(data) {  
  2.     alert(data);  
  3. }.  
Step 7

Now, run the application and see the output.

output