AngularJS How to add index column in table using $index

In this AngularJS example you will learn how to add index column in table using $index. In this example you will learn how to use $index with ng-repeat directive to display index column in HTML table.

Here you can view the output of the example and you can also "try it yourself" by clicking on "Try Now" button given at the bottom.

Syntax

<table>  
	<th>{{'Index' | uppercase }}</th><th>{{'Name' | uppercase }}</th>
	<th>{{'Last Name' | uppercase }}</th><th>{{'Designation' | uppercase }}</th>  
	<th>{{'Salary' | uppercase }}</th><th>{{'Phone' | uppercase }}</th>  
	<tr ng-repeat="emp in employees | orderBy : 'firstName'" ng-class-odd="'odd'" ng-class-even="'even'"> 
	  <td>{{ $index + 1 }}</td>
	  <td>{{ emp.firstName }}</td>  
	  <td>{{ emp.lastName }}</td>  
	  <td>{{ emp.jobTitle }}</td>  
	  <td>{{ emp.salary }}</td>  
	  <td>{{ emp.phone }}</td>  
	</tr>  
</table>       

Example Display table with orderby column data

<!DOCTYPE html>              
<html>              
<head> <!-- www.techstrikers.com -->               
<script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">  
</script>                
    <meta charset="utf-8">              
    <title>AngularJS  How to add index column in table code Example</title>  
 <style>  
   table, th , td  {  
  border: 1px solid grey;  
  border-collapse: collapse;  
  padding: 5px;  
  
  font-family:arial;  
  }
  .odd {
  background-color: #ffffff;
  }
  .even {
    background-color: #f1f1f1;
  }
  </style>  
  <script>              
var app = angular.module('app', []);              
    app.controller("appController", function ($scope) {        
    $scope.employees = [{    
        firstName:"Jimi",    
        lastName:"Scott",    
        jobTitle:"Developer",    
        salary:20000,    
        phone:"345-333-7845"    
      },    
      {    
          firstName:"Bailey",    
          lastName:"Lee",    
          jobTitle:"Lead",    
          salary:30000,    
          phone:"322-121-213"    
      },    
      {    
          firstName:"Aura",    
          lastName:"Martin",    
          jobTitle:"QA",    
          salary:35000,    
          phone:"322-333-0987"    
      },    
      {    
          firstName:"Austin",    
          lastName:"Williams",    
          jobTitle:"Designer",    
          salary:20000,    
          phone:"322-265-2321"    
      },    
      {    
          firstName:"Bara",    
          lastName:"Miller",    
          jobTitle:"Tester",    
          salary:15000,    
          phone:"322-890-2321"    
      },{    
          firstName:"Carlton",    
          lastName:"Taylor",    
          jobTitle:"Manager",    
          salary:45000,    
          phone:"322-456-2321"    
      },    
      {    
          firstName:"Adisa",    
          lastName:"White",    
          jobTitle:"Developer",    
          salary:25000,    
          phone:"121-333-2321"    
      }];    
    });       
</script>              
</head>              
<body style="background-color:#DDE4E9;font-family:arial;">              
  <fieldset style="background-color:#DDE4E9;">                          
    <legend>AngulerJS  How to add index column in table   
    code Example</legend>               
<div ng-app="app">       
<div ng-controller="appController">       
  <table>  
	<th>{{'Index' | uppercase }}</th><th>{{'Name' | uppercase }}</th>
	<th>{{'Last Name' | uppercase }}</th><th>{{'Designation' | uppercase }}</th>  
	<th>{{'Salary' | uppercase }}</th><th>{{'Phone' | uppercase }}</th>  
	<tr ng-repeat="emp in employees | orderBy : 'firstName'" ng-class-odd="'odd'" ng-class-even="'even'"> 
	  <td>{{ $index + 1 }}</td>
	  <td>{{ emp.firstName }}</td>  
	  <td>{{ emp.lastName }}</td>  
	  <td>{{ emp.jobTitle }}</td>  
	  <td>{{ emp.salary }}</td>  
	  <td>{{ emp.phone }}</td>  
	</tr>  
</table>  
  </div>      
</div>              
</div>              
 </fieldset>               
</body>              
</html>    

Try Now

More AngularJS With HTML Table Examples

 
 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.