filter nested properties in angularjs object

filter nested properties in angularjs object

We can write the nested properties look like below code.

Go to live demo link http://embed.plnkr.co/CN8fO7/preview


?
1
<li ng-repeat="country in countries | filter: { locations: [{ cityId: search.locations.cityId }] }">
<li ng-repeat="shop in shops | filter: { locations: [{ cityId: search.locations.cityId }] }">
The AngularJs Reference file is
?
1
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
The code-sample for HTML page
?
1
2
3
4
5
6
7
8
9
<body ng-app="myApp">
  <div ng-controller="ngController">
    <input type="text" ng-model="Selected_CityId" />
    <div ng-repeat="country in countries | filter : search">
      Country : {{country.name}}
      Locations : {{ country.locations }}
    </div>
  </div>
</body>
<body ng-app="myApp">
  <div ng-controller="ngController">
    <input type="text" ng-model="Selected_CityId" />
    <div ng-repeat="country in countries | filter : search">
      Country : {{country.name}} 
      Locations : {{ country.locations }}
    </div>
  </div>
</body>
The code-sample AngularJs
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var app = angular.module('myApp', []);
app.controller('ngController', function($scope) {
  $scope.countries = [{
    "name": "USA",
    "category_id": 1,
    "locations": [{
      "cityId": 22,
      "regionId": 1
    }]
  }, {
    "name": "India",
    "category_id": 2,
    "locations": [{
      "cityId": 30,
      "regionId": 2
    }]
  }];
  $scope.search = function(shop) {
    if ($scope.Selected_CityId === undefined || $scope.Selected_CityId.length === 0) {
      return true;
    }
    var match = false;
    angular.forEach(shop.locations, function(location) {
      if (location.cityId === parseInt($scope.Selected_CityId)) {
        match = true;
      }
    });
    return match;
  };
});
var app = angular.module('myApp', []);
app.controller('ngController', function($scope) {
  $scope.countries = [{
    "name": "USA",
    "category_id": 1,
    "locations": [{
      "cityId": 22,
      "regionId": 1
    }]
  }, {
    "name": "India",
    "category_id": 2,
    "locations": [{
      "cityId": 30,
      "regionId": 2
    }]
  }];

  $scope.search = function(shop) {
    if ($scope.Selected_CityId === undefined || $scope.Selected_CityId.length === 0) {
      return true;
    }

    var match = false;
    angular.forEach(shop.locations, function(location) {
      if (location.cityId === parseInt($scope.Selected_CityId)) {
        match = true;
      }
    });
    return match;
  };
});
The Full Example code-sample as given below
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
  <script>
    var app = angular.module('myApp', []);
    app.controller('ngController', function($scope) {
      $scope.countries = [{
        "name": "USA",
        "category_id": 1,
        "locations": [{
          "cityId": 22,
          "regionId": 1
        }]
      }, {
        "name": "India",
        "category_id": 2,
        "locations": [{
          "cityId": 30,
          "regionId": 2
        }]
      }];
      $scope.search = function(shop) {
        if ($scope.Selected_CityId === undefined || $scope.Selected_CityId.length === 0) {
          return true;
        }
        var match = false;
        angular.forEach(shop.locations, function(location) {
          if (location.cityId === parseInt($scope.Selected_CityId)) {
            match = true;
          }
        });
        return match;
      };
    });
  </script>
</head>
<body ng-app="myApp">
  <div ng-controller="ngController">
    <input type="text" ng-model="Selected_CityId" />
    <div ng-repeat="country in countries | filter : search">
      {{country.name}} {{ country.locations }}
    </div>
  </div>
</body>
</html>
<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>filter nested properties in angularjs</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
  <script>
    var app = angular.module('myApp', []);
    app.controller('ngController', function($scope) {
      $scope.countries = [{
        "name": "USA",
        "category_id": 1,
        "locations": [{
          "cityId": 22,
          "regionId": 1
        }]
      }, {
        "name": "India",
        "category_id": 2,
        "locations": [{
          "cityId": 30,
          "regionId": 2
        }]
      }];

      $scope.search = function(shop) {
        if ($scope.Selected_CityId === undefined || $scope.Selected_CityId.length === 0) {
          return true;
        }

        var match = false;
        angular.forEach(shop.locations, function(location) {
          if (location.cityId === parseInt($scope.Selected_CityId)) {
            match = true;
          }
        });
        return match;
      };
    });
  </script>
</head>

<body ng-app="myApp">
  <div ng-controller="ngController">
    <input type="text" ng-model="Selected_CityId" />
    <div ng-repeat="country in countries | filter : search">
      {{country.name}} {{ country.locations }}
    </div>
  </div>
</body>

</html>

Go to live demo code link http://embed.plnkr.co/CN8fO7/preview
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^