0
 <div class="district-list" ng-repeat="office in offices | filter : {'DisplayOffice' : officeLocationLevelList.searchText}|limitTo:1">

Have a multilevel list of data. depending on the condition i want to filter the data.(First Level/ inner level). If the first level is selected then i have to search with one json attribute and if the inner level is selected then search with another json attribute. I have set it in a scope variable. How can use scope variable instead of 'DisplayOffice'

1 Answer 1

1

you can bind a function to filter which returns the filter condition using scope variables.

angular.module("app", [])
  .controller("myCtrl", function($scope) {
    $scope.data = [
      {
        id: 1,
        name: 'name1'
      },{
        id: 2,
        name: 'name2'
      },{
        id: 3,
        name: 'name3'
      },{
        id: 3,
        name: 'aaaaa'
      }
    ];
    
    $scope.getCondition = function() {
      var obj = {};
      obj[$scope.key] = $scope.value;
      return obj;
    }
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  Key: <input type="text" ng-model="key">
  Value: <input type="text" ng-model="value">
  <div ng-repeat="item in data | filter: {[key]:value}">
    {{item.name}}
  </div>
</div>

and there is another way while using ES6' new feature {[key]: value}, but this may now work for some browsers.

angular.module("app", [])
  .controller("myCtrl", function($scope) {
    $scope.$watch("key", function() {
      console.log({[$scope.key]:$scope.value});
    });
    $scope.$watch("value", function() {
      console.log({[$scope.key]:$scope.value});
    });
    $scope.data = [
      {
        id: 1,
        name: 'name1'
      },{
        id: 2,
        name: 'name2'
      },{
        id: 3,
        name: 'name3'
      },{
        id: 3,
        name: 'aaaaa'
      }
    ];
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  Key: <input type="text" ng-model="key">
  Value: <input type="text" ng-model="value">
  
  <div ng-repeat="item in data | filter: {[key]: value}">
    {{item.name}}
  </div>
</div>

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

6 Comments

How can i set it in template without these functions?
Thanks. But I got the ans :)
@ShriyaR that's ok. if you have found some better solutions, you can edit to your question or even post an answer.
have added a condition in filter for enabling and disabling the filter. <div class="district-list" ng-repeat="office in offices | filter : !officeLocationLevelList.locationLevelSelected.single_level && {'DistrictDisplay' : officeLocationLevelList.searchText}|limitTo:1">
actually i have a multilevel list of data. depending on the condition i want to filter the data.(First Level/ inner level filter)
|

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.