JavaScript - Add Entry To Table Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 121 times.

In this tutorial we will create a Add Entry To Table Using AngularJS. This code will add a new entry to HTML table without databases when the user submit the form input. The code use angular directives that can add new entry as an array in order to store a temporary data without sending to the database server. 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="<a href="https://www.sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://www.sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Add Entry To Table Using AngularJS</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form>
  20. <div class = "form-group">
  21. <label>Firstname</label>
  22. <input type = "text" class = "form-control" ng-model = "newMember.firstname"/>
  23. </div>
  24. <div class = "form-group">
  25. <label>Lastname</label>
  26. <input type = "text" class = "form-control" ng-model = "newMember.lastname"/>
  27. </div>
  28. <div class = "form-group">
  29. <label>Address</label>
  30. <input type = "text" class = "form-control" ng-model = "newMember.address"/>
  31. </div>
  32.  
  33. <center><button type="button" class="btn btn-primary" ng-click = "saveMember()"><span class = "glyphicon glyphicon-save"></span> Save</button></center>
  34. </form>
  35. </div>
  36. <div class="col-md-8">
  37. <table class="table table-bordered">
  38. <thead class="alert-info">
  39. <tr>
  40. <th>Firstname</th>
  41. <th>Lastname</th>
  42. <th>Address</th>
  43. </tr>
  44. </thead>
  45. <tr ng-repeat="member in members">
  46. <td>{{member.firstname}}</td>
  47. <td>{{member.lastname}}</td>
  48. <td>{{member.address}}</td>
  49. </tr>
  50. </tbody>
  51. </table>
  52. </div>
  53. </div>
  54. <div class="modal fade" id="update_member" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  55. <div class="modal-dialog" role="document">
  56. <div class="modal-content">
  57. <form>
  58. <div class="modal-header">
  59. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  60. <h4 class="modal-title text-info" id="myModalLabel">Updating Member</h4>
  61. </div>
  62. <div class="modal-body">
  63. <div class = "form-group">
  64. <label>Full Name</label>
  65. <input type = "text" class = "form-control" ng-model = "selectedMember.name"/>
  66. </div>
  67. <div class = "form-group">
  68. <label>Address</label>
  69. <input type = "text" class = "form-control" ng-model = "selectedMember.address"/>
  70. </div>
  71. </div>
  72. <div class="modal-footer">
  73. <button class="btn btn-success" data-dismiss = "modal"><span class = "glyphicon glyphicon-edit"></span> Update</button>
  74. </div>
  75. </form>
  76. </div>
  77. </div>
  78. </div>
  79. <div class="modal fade" id="delete_member" aria-hidden="true">
  80. <div class="modal-dialog">
  81. <div class="modal-content">
  82. <form>
  83. <div class="modal-body">
  84. <center><h4 class="text-danger">Are you sure you want to delete this record?</h4></center>
  85. </div>
  86. <div class="modal-footer">
  87. <button class="btn btn-danger" data-dismiss="modal" ng-click="deleteMember()"><span class="glyphicon glyphicon-check"></span> Yes</button>
  88. <button class="btn btn-success" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
  89. </div>
  90. </form>
  91. </div>
  92. </div>
  93. </div>
  94. </body>
  95. <script src="js/angular.js"></script>
  96. <script src="js/script.js"></script>
  97. <script src="js/jquery-3.2.1.min.js"></script>
  98. <script src="js/bootstrap.js"></script>
  99. </html>

Creating the Main Function

This code contains the main function of the application. This code will add new entry to table 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.newMember = {};
  5. $scope.clickedMembers = [];
  6.  
  7.  
  8. $scope.members = [];
  9.  
  10. $scope.saveMember = function(){
  11. $scope.members.push($scope.newMember);
  12. $scope.newMember = {};
  13. };
  14.  
  15. });

There you have it we successfully created a Add Entry To Table 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.