Monday, June 29, 2015

AngularJS orderby Filter

AngularJS filters are used to format/transform the data displayed in the view, filters can be used with expressions, ng-bind, ng-repeat etc and act upon the data from the model. The orderby filter is used to sort a list of elements in an array / collection, this comes in handy when we want to implement sorting feature in a table/list in the view.

The following is the syntax for using orderby filters
<li ng-repeat="<collection> | orderBy:'<column>'">

The following is an example of using orderby filters.
<html ng-app>
<head>
    <meta charset="utf-8" />
    <title>AngularJS - Basic</title>
    <script src="angular.min.js"></script>
</head>
<body>
          <div ng-init="Employees=[
                                      {id:1,Name:'Tom'},
                                      {id:2,Name:'Harry'},
                                      {id:3,Name:'Peter'}]">
                    <ul>
                       <li ng-repeat="emp in Employees | orderBy:'Name'">
                         {{ emp.id }} - {{ emp.Name }}<br/>
                       </li>
                   </ul>
          </div>
</body>
</html>


Output:



Search Flipkart Products:
Flipkart.com

No comments: