1

I would like to use AngularJS to filter users status active or reject. but one user whose name is peter reject. when I use select tag method to show the user whose status is reject and hide the user whose status is Active. Unfortunately, it also show "Peter Reject"'s information, but his status is Active.

My Html file.

Status: 
<select class="select" ng-model="select1">
       <option value="Active">Active</option>     
        <option value="Reject">Reject</option>
 </select>

 <table>
      <th>Name</th>
      <th>Status</th>
    <tr ng-repeat="item in items | filter : select1>
      <td>{{item.name}}</td>
      <td>{{item.status}}</td>
    </tr>
</table>

My json file:

 [{"name":"joe jonas","status":"Active"},
{"name":"Peter Reject","status":"Active"},
{"name":"Marilyn Monroe","status":"Reject"}]

enter image description here enter image description here

I just want to show the users who status is Reject column, any idea?? Thanks you very much.

1 Answer 1

2

You are adding the filter to all the columns instead add it to a particular column like so.

    <tr ng-repeat="item in items | filter : {status:select1}">
      <td>{{item.name}}</td>
      <td>{{item.status}}</td>
    </tr>

A closing comma was also missing in the above tr tag, I added that also!

Also you need to initialize the select1 variable to a blank string for all the fields to be displayed initially. I am using ng-init to do this. Refer the below line.

<div ng-controller='MyController' ng-app="myApp" ng-init="select1 = ''">

var app = angular.module('myApp', []);

app.controller('MyController', function MyController($scope) {
  $scope.items = [{
    "name": "joe jonas",
    "status": "Active"
  }, {
    "name": "Peter Reject",
    "status": "Active"
  }, {
    "name": "Marilyn Monroe",
    "status": "Reject"
  }]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller='MyController' ng-app="myApp" ng-init="select1 = ''">
  <select class="select" ng-model="select1">
    <option value="Active">Active</option>
    <option value="Reject">Reject</option>
  </select>

  <table>
    <th>Name</th>
    <th>Status</th>
    <tr ng-repeat="item in items | filter : {status:select1}">
      <td>{{item.name}}</td>
      <td>{{item.status}}</td>
    </tr>
  </table>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.