JavaScript - Simple Remove Table Row Using AngularJS

In this tutorial we will create a Simple Remove Table Row Using AngularJS. This code will automatically eliminate a table row when the user click a delete button. The code itself use AngularJS to remove a specific table row by sending an index array position through bootstrap modal, by using dot notation splice you can remove the array position of that given index. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app = "myModule">
  3.         <head>
  4.                 <meta charset="UTF-8"  name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <title>Sourcecodester</title>
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7.         </head>
  8. <body ng-controller="myController">
  9.         <nav class="navbar navbar-default">
  10.                 <div class="containet-fluid">
  11.                         <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class = "col-md-3"></div>
  15.         <div class = "col-md-6 well">
  16.                 <h3 class = "text-primary">JavaScript - Simple Remove Table Row Using AngularJS</h3>
  17.                 <hr style = "border-top:1px dotted #ccc;"/>
  18.                 <div class = "container-fluid">
  19.                         <table class = "table table-bordered">
  20.                                 <thead class="alert-info">
  21.                                         <tr>
  22.                                                 <th>Member ID</th>
  23.                                                 <th>Firstname</th>
  24.                                                 <th>Lastname</th>
  25.                                                 <th>Gender</th>
  26.                                                 <th>Action</th>
  27.                                         </tr>
  28.                                 </thead>
  29.                                 <tbody>
  30.                                         <tr ng-repeat = "member in members">
  31.                                                 <td>{{$index+1}}</td>
  32.                                                 <td>{{member.firstname}}</td>
  33.                                                 <td>{{member.lastname}}</td>
  34.                                                 <td>{{member.gender}}</td>
  35.                                                 <td><button type = "button" data-toggle = "modal" data-target = "#delete_member" class = "btn btn-sm btn-danger"><span class = "glyphicon glyphicon-trash"></span> Delete</button></td>
  36.                                         </tr>
  37.                                 </tbody>
  38.                         </table>
  39.                 </div>
  40.         </div>
  41.         <div class="modal fade" id="delete_member" aria-hidden="true">
  42.                 <div class="modal-dialog">
  43.                         <div class="modal-content">
  44.                                 <form>
  45.                                 <div class="modal-body">       
  46.                                                 <center><h4 class = "text-danger">Are you sure you want to delete this record?</h4></center>
  47.                                 </div>
  48.                                 <div class="modal-footer">
  49.                                         <button  class="btn btn-danger" data-dismiss = "modal" ng-click = "deleteMember()"><span class = "glyphicon glyphicon-check"></span> Yes</button>
  50.                                         <button  class="btn btn-success"  data-dismiss = "modal"><span class = "glyphicon glyphicon-remove"></span> No</button>
  51.                                 </div>
  52.                                 </form>
  53.                         </div>
  54.                 </div>
  55.         </div>
  56. </body>
  57. <script src="js/angular.js"></script>
  58. <script src="js/script.js"></script>
  59. <script src  = "js/jquery-3.2.1.min.js"></script>      
  60. <script src  = "js/bootstrap.js"></script>     
  61. </html>

Creating the Main Function

This code contains the main function of the application. This code will remove a table row when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory.
  1. var app = angular.module("myModule", [])
  2.                                                            .controller("myController", function($scope){
  3.                        
  4.                                                                
  5.                                                                 $scope.members = [
  6.                                                                         {firstname: "John", lastname: "Smith", gender: "Male"},
  7.                                                                         {firstname: "Claire", lastname: "Temple", gender: "Female"},
  8.                                                                         {firstname: "Regina", lastname: "Wilson", gender: "Female"},
  9.                                                                         {firstname: "Jack", lastname: "Black", gender: "Male"},
  10.                                                                 ];
  11.                                                                
  12.                                                                
  13.                                                                 $scope.selectMember =  function(member){
  14.                                                                         $scope.selectedMember = member;
  15.                                                                 };
  16.                                                                
  17.                                                                
  18.                                                                 $scope.deleteMember = function(){
  19.                                                                         $scope.members.splice($scope.members.indexOf($scope.selectMember), 1);
  20.                                                                 };
  21.                                                 });
There you have it we successfully created a Simple Remove Table Row using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment