Tables are one of the common elements used in HTML when working with web pages.
Tables in HTML are designed using the HTML tag
Using the above available HTML tags along with AngularJS, we can make it easier to populate table data. In short, the ng-repeat directive is used to fill in table data.
We will look at how to achieve this during this chapter. We will also look at how we can use the orderby and uppercase filters along with using the $index attribute to display Angular table indexes.
In this tutorial, you will learn-
As we discussed in the introduction to this chapter, the basis for creating the table structure in an HTML page remains the same.
The structure of the table is still created using the normal HTML tags of <table>,<tr> , <td> and <th>. However, the data is populated by using the ng-repeat directive in AngularJS.
Let's look a simple example of how we can implement Angular tables.
In this example,
We are going to create an Angular table which will have course topics along with their descriptions.
Step 1) We are first going to add a "style" tag to our HTML page so that the table can be displayed as a proper table.
Step 2) The next step is to write the code to generate the table, and it's data.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> </head> <body> <title>Event Registration</title> <style> table,th,td{ border: 1px solid grey; padding:5px; } </style> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.js"></script> <script src="/lib/bootstrap.js"></script> <script src="/lib/bootstrap.css"></script> <h1> Guru99 Global Event</h1> <div ng-app="DemoApp" ng-controller="DemoController"> <table> <tr ng-repeat="ptutor in tutorial"> <td>{{ptutor.Name}}</td> <td>{{ptutor.Description}}</td> </tr> </table> </div> <script type="text/javascript"> var app = angular.module('DemoApp',[]); app.controller('DemoController',function($scope) { $scope.tutorial = [ {Name:"Controllers",Description :"Controllers in action"}, {Name:"Models",Description :"Models and binding data"}, {Name:"Directives",Description :"Flexibility of Directives"} ]}); </script> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the above output,
It's very common to use the inbuilt filters within AngularJS to change the way the data is displayed in the tables. We have already seen filters in action in an earlier chapter. Let's have a quick recap of filters before we proceed ahead.
In Angular.JS filters are used to format the value of expression before it is displayed to the user. An example of a filter is the 'uppercase' filter which takes on a string output and formats the string and displays all the characters in the string as uppercase.
So in the below example, if the value of the variable 'TutorialName' is 'AngularJS', the output of the below expression will be 'ANGULARJS'.
{{ TurotialName | uppercase }}
In this section, we will be looking at how the orderBy and uppercase filters can be used in tables in more detail.
This filter is used to sort the table based on one of the table columns. In the previous example, the output for our Angular table data appeared as shown below.
Controllers | Controllers in action |
Models | Models and binding data |
Directives | Flexibility of Directives |
Let's look at an example, on how we can use the "orderBy" filter and sort the Angular table data using the first column in the table.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> </head> <body> <title>Event Registration</title> <style> table,th,td{ border: 1px solid grey; padding:5px; } </style> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.js"></script> <script src="/lib/bootstrap.js"></script> <script src="/lib/bootstrap.css"></script> <h1> Guru99 Global Event</h1> <div ng-app="DemoApp" ng-controller="DemoController"> <table> <tr ng-repeat="ptutor in tutorial | orderBy : 'Name'"> <td>{{ptutor.Name}}</td> <td>{{ptutor.Description}}</td> </tr> </table> </div> <script type="text/javascript"> var app = angular.module('DemoApp',[]); app.controller('DemoController',function($scope) { $scope.tutorial = [ {Name:"Controllers",Description :"Controllers in action"}, {Name:"Models",Description :"Models and binding data"}, {Name:"Directives",Description :"Flexibility of Directives"} ]}); </script> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
We can also use the uppercase filter to change the data in the Angular table to uppercase.
Let's take a look at an example of how we can achieve this.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> </head> <body> <title>Event Registration</title> <style> table,th,td{ border: 1px solid grey; padding:5px; } </style> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.js"></script> <script src="/lib/bootstrap.js"></script> <script src="/lib/bootstrap.css"></script> <h1> Guru99 Global Event</h1> <div ng-app="DemoApp" ng-controller="DemoController"> <table> <tr ng-repeat="ptutor in tutorial"> <td>{{ptutor.Name | uppercase}}</td> <td>{{ptutor.Description}}</td> </tr> </table> </div> <script type="text/javascript"> var app = angular.module('DemoApp',[]); app.controller('DemoController',function($scope) { $scope.tutorial = [ {Name:"Controllers",Description :"Controllers in action"}, {Name:"Models",Description :"Models and binding data"}, {Name:"Directives",Description :"Flexibility of Directives"} ]}); </script> </body> </html>
Code Explanation:
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
To display the table index, add a <td> with $index.
Let's take a look at an example of how we can achieve this.
<!DOCTYPE html> <html> <head> <meta chrset="UTF 8"> </head> <body> <title>Event Registration</title> <style> table,th,td{ border: 1px solid grey; padding:5px; } </style> <script src="https://code.angularjs.org/1.6.9/angular-route.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.min.js"></script> <script src="https://code.angularjs.org/1.6.9/angular.js"></script> <script src="/lib/bootstrap.js"></script> <script src="/lib/bootstrap.css"></script> <h1> Guru99 Global Event</h1> <div ng-app="DemoApp" ng-controller="DemoController"> <table> <tr ng-repeat="ptutor in tutorial"> <td>{{$index + 1}}</td> <td>{{ptutor.Name}}</td> <td>{{ptutor.Description}}</td> </tr> </table> </div> <script type="text/javascript"> var app = angular.module('DemoApp',[]); app.controller('DemoController',function($scope) { $scope.tutorial = [ {Name:"Controllers",Description :"Controllers in action"}, {Name:"Models",Description :"Models and binding data"}, {Name:"Directives",Description :"Flexibility of Directives"} ]}); </script> </body> </html>
Code Explanation:
In this additional column, we are using the "$index" property provided by AngularJS and then using the +1 operator to increment the index for each row.
If the code is executed successfully, the following Output will be shown when you run your code in the browser.
Output:
From the output,
Summary: