AngularJS - SPLessons

Angularjs ng-repeat filter

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Angularjs ng-repeat filter

Angularjs ng-repeat filter

As we all know angularjs ng-repeat directive will iterate over collection. Now we will see how to use the filter, filter will selects the subset of items in collection or array and returns a new array. Note: ng-repeat directive will create new object on each iteration. If you see following line of code ng-repeat="question_details in list"  here list is collection or array so when ever iteration start then it will create a new array named as question_details.

Code behind the scene of AngularJS ng-repeat

 

Quick Solution 

[html] // app.js $scope.userfilter = [ {"key":"text","value":"Text Box"}, {"key":"textarea","value":"Text Area"}, {"key":"checkbox","value":"Check Box"} ]; $scope.list = [ {"id":1,"description":"What's your first car?","questionType":"Question","controlType":"text","status":"A"}, {"id":2,"description":"What is your last friend name?","questionType":"Question","controlType":"text","status":"A"}, {"id":3,"description":"Where did you work?","questionType":"Question","controlType":"checkbox","status":"A"}, {"id":4,"description":"Do you get enough salary ?","questionType":"Question","controlType":"text","status":"A"}, {"id":5,"description":"Last four digits of your phone ?","questionType":"Essay","controlType":"textarea","status":"A"}]; // index.html  <select ng-model="selectedcontroltype" ng-options="filter.value for filter in userfilter track by filter.key" > <option value=""> Select Control Type </option> </select> <tr ng-repeat="question_details in list | filter:{controlType:selectedcontroltype.key}:true" > <td class="col-md-1" > {{$index+1}} </td> <td class="col-md-5 text-left" > {{question_details.description}}</td> <td class="col-md-2" > {{question_details.questionType}}</td> <td class="col-md-2" > {{question_details.controlType}}</td> <td class="col-md-1" > {{question_details.status}}</td> </tr> [/html]

Steps 

  • Here we have one drop down list and it contains the filter values. I mean filter the list object by control type column.
  • We have table content data from list json object.
  • We used the ng-repeat to iterate over the list json object collection.
  • We have passed the express to ng-repeat to filter list object " filter:{controlType:selectedcontroltype.key}:true "
    • In the above expression "controlType" is column name of the list object.
    • "selectedcontroltype.key"  is value model name of control type drop down.
    • "true" - if you do not pass the this option then filter will do operation  "Eg: select * from list like '%text%' " out put is : text, textarea 
    • "true" - if  you  pass the this option then filter will do operation "Eg: select * from list like 'text' "  out put is : text
 

Project Structure

[html] + index.html + app.js [/html]

app.js

[javascript] /** * Created by sinukoll on 4/15/15. */ 'use strict'; var splessonsApp = angular.module('splessonsApp',[]); splessonsApp.controller("splessonsController",function($scope){ $scope.userfilter = [ {"key":"text","value":"Text Box"}, {"key":"textarea","value":"Text Area"}, {"key":"checkbox","value":"Check Box"} ]; $scope.list = [ {"id":1,"description":"What's your first car?","questionType":"Question","controlType":"text","status":"A"}, {"id":2,"description":"What is your last friend name?","questionType":"Question","controlType":"text","status":"A"}, {"id":3,"description":"Where did you work?","questionType":"Question","controlType":"checkbox","status":"A"}, {"id":4,"description":"Do you get enough salary ?","questionType":"Question","controlType":"text","status":"A"}, {"id":5,"description":"Last four digits of your phone ?","questionType":"Essay","controlType":"textarea","status":"A"}]; }); [/javascript]

index.html

[html] <!DOCTYPE html> <html ng-app="splessonsApp"> <head lang="en"> <meta charset="utf-8"> <title>SPLessons</title> <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"> </head> <body ng-controller="splessonsController"> <div class="container"> <div class="jumbotron"> <h3> <a href="http://www.splessons.com/2015/05/angularjs-value-attribute-for-select/" > SPLessons.com </a> </h3> <form class="form-horizontal"> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">Control Type: </label> <div class="col-sm-10"> <select name="degree" class="form-control" ng-model="selectedcontroltype" ng-options="filter.value for filter in userfilter track by filter.key" > <option value=""> Select Control Type </option> </select> </div> </div> </form> <p> {{selectedcontroltype.key}}</p> <table class="table table-bordered"> <thead> <tr> <th class="col-md-1"> Sequence</th> <th class="col-md-5" > Description </th> <th class="col-md-2"> Question Type </th> <th class="col-md-2"> Control Type </th> <th class="col-md-1"> Status </th> </tr> </thead> <tbody> <tr ng-repeat="question_details in list | filter:{controlType:selectedcontroltype.key}:true" > <td class="col-md-1" > {{$index+1}} </td> <td class="col-md-5 text-left" > {{question_details.description}}</td> <td class="col-md-2" > {{question_details.questionType}}</td> <td class="col-md-2" > {{question_details.controlType}}</td> <td class="col-md-1" > {{question_details.status}}</td> </tr> </tbody> </table> </div> </div> <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js" type="text/javascript" ></script> <script src="app.js" type="text/javascript"></script> </body> </html> [/html]