JavaScript - Change Entry Using AngularJS

In this tutorial we will create a Change Entry Using AngularJS. This code will change the existing temporary data in the table when user click the update button. The code itself use a angular directives that can change the table row data in without the database storage. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a comprehensive JavaScript framework that extend the browser capabilities. It handles the heavy lifting of DOM manipulation and AJAX request in a whole.

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.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7. <body ng-controller="myController">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="containet-fluid">
  10.                         <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Change Entry Using AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-4">
  18.                         <form ng-submit="addEntry()">
  19.                                 <div class="form-group">
  20.                                         <label>Firstname</label>
  21.                                         <input type="text" class="form-control" ng-model="newEntry.firstname" ng-required="true"/>
  22.                                 </div>
  23.                                 <div class="form-group">
  24.                                         <label>Lastname</label>
  25.                                         <input type="text" class="form-control" ng-model="newEntry.lastname" ng-required="true"/>
  26.                                 </div>
  27.                                 <div class="form-group">
  28.                                         <label>Address</label>
  29.                                         <input type="text" class="form-control" ng-model="newEntry.address" ng-required="true"/>
  30.                                 </div>
  31.                                
  32.                                 <center><button class="btn btn-primary">Add Entry</button></center>
  33.                         </form>
  34.                 </div>
  35.                 <div class="col-md-8">
  36.                         <table class="table table-bordered">
  37.                                 <thead class="alert-info">
  38.                                         <tr>
  39.                                                 <th>Firstname</th>
  40.                                                 <th>Lastname</th>
  41.                                                 <th>Address</th>
  42.                                                 <th>Action</th>
  43.                                         </tr>
  44.                                 </thead>
  45.                                 <tbody>
  46.                                         <tr ng-repeat="member in members">
  47.                                                 <td>{{member.firstname}}</td>
  48.                                                 <td>{{member.lastname}}</td>
  49.                                                 <td>{{member.address}}</td>
  50.                                                 <td><button type="button" data-toggle="modal" data-target="#update_member" ng-click="selectMember(member)" class="btn btn-sm btn-warning"><span class = "glyphicon glyphicon-edit"></span> Update</button></td>
  51.                                         </tr>
  52.                                 </tbody>
  53.                         </table>
  54.                 </div>
  55.         </div>
  56.         <div class="modal fade" id="update_member" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  57.                 <div class="modal-dialog" role="document">
  58.                         <div class="modal-content">
  59.                                 <form>
  60.                                         <div class="modal-header">
  61.                                                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  62.                                                 <h4 class="modal-title text-info" id="myModalLabel">Updating Member</h4>
  63.                                         </div>
  64.                                         <div class="modal-body">
  65.                                                 <div class="col-md-2"></div>
  66.                                                 <div class="col-md-8">
  67.                                                         <div class="form-group">
  68.                                                                 <label>Firstname</label>
  69.                                                                 <input type="text" class="form-control" ng-model="selectedMember.firstname"/>
  70.                                                         </div>
  71.                                                         <div class="form-group">
  72.                                                                 <label>Lastname</label>
  73.                                                                 <input type="text" class="form-control" ng-model="selectedMember.lastname"/>
  74.                                                         </div>
  75.                                                         <div class="form-group">
  76.                                                                 <label>Address</label>
  77.                                                                 <input type="text" class="form-control" ng-model="selectedMember.address"/>
  78.                                                         </div>
  79.                                                 </div> 
  80.                                         </div>
  81.                                         <div style="clear:both;"></div>
  82.                                         <div class="modal-footer">
  83.                                                 <button  class="btn btn-warning"  data-dismiss="modal">Update</button>
  84.                                         </div>
  85.                                 </form>
  86.                         </div>
  87.                 </div>
  88.         </div>
  89. </body>
  90. <script src="js/angular.js"></script>
  91. <script src="js/script.js"></script>
  92. <script src="js/jquery-3.2.1.min.js"></script> 
  93. <script src="js/bootstrap.js"></script>
  94. </html>

Creating the Main Function

This code contains the main function of the application. This code will change a entry in 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.
  1. var app = angular.module("myModule", [])
  2.                                                            .controller("myController", function($scope){
  3.                                                                
  4.                                                                 $scope.newEntry = {};          
  5.                                                                 $scope.members = [];
  6.                                                                
  7.                                                                 $scope.addEntry = function(){
  8.                                                                         $scope.members.push($scope.newEntry);
  9.                                                                         $scope.newEntry = {};
  10.                                                                 };             
  11.                                                                
  12.                                                                 $scope.selectMember =  function(member){
  13.                                                                         $scope.selectedMember = member;
  14.                                                                 };
  15.                                                 });
There you have it we successfully created a Change Entry 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