Skip to main content

Populate Table With Array Object Using AngularJS Source Code

Language


In this tutorial we will create a Populate Table With Array Object Using AngularJS. This code will dynamically populate the HTML table when the user click the button. The code itself use AngularJS directive ng-click to call a specific method that will populate the HTML table by the use of ng-repeat. This is a free source code you can modify it and use it on your working programs.

AngularJS is a JavaScript-based open-source front-end web application framework . It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, you will need to download the AngularJS here's the link https://angularjs.org/.

Note: This code will only work if run in a local server.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as 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">Populate Table With Array Object Using AngularJS Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <button ng-click="populateArrays();" class="btn btn-primary">Populate Table</button>
  19. </div>
  20. <div class="col-md-8">
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Member ID</th>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Grade</th>
  28. </tr>
  29. </thead>
  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.address}}</td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. </div>
  39. </div>
  40. </body>
  41. <script src = "js/angular.js"></script>
  42. <script src = "js/script.js"></script>
  43. </html>

Creating the Script

This code contains the main function of the application. This code will populate the HTML table when the button is clicked. To that just kindly copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3.  
  4. $scope.populateArrays = function(){
  5. $scope.members = $scope.arrayList;
  6. }
  7.  
  8. $scope.arrayList = [
  9. {firstname: "John", lastname: "Smith", address: "New York"},
  10. {firstname: "Claire", lastname: "Temple", address: "Racoon City"},
  11. {firstname: "Gary", lastname: "Girl", address: "Pallet Town"},
  12. ];
  13.  
  14.  
  15. });

There you have it we successfully created a Populate Table With Array Object Using AngularJS. I hope that this very 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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.