Angular: Empty Fiddle
http://angularjs.org/
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyController">
<input type="text" ng-model="selectedCityId" />
<ul>
<li ng-repeat="shop in shops">
<p ng-repeat = "locations in shop.locations | filter:search" style="display: block">
City id: {{ locations.city_id }}
<span style="padding-left: 20px; display: block;" ng-repeat="detail in locations.details | filter:item">
Pin code: {{detail.pin}}
</span>
</p>
</li>
</ul>
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('MyController', function ($scope) {
$scope.search = function (location) {
if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
return true;
}
var id = parseInt($scope.selectedCityId);
//check if we typed an id of the location
if (location.city_id === id) { return true; }
//check if we typed id of a child detail
return location.details.filter(function(detail) {
return detail.pin === id;
}).length === 1;
};
$scope.item = function (detail) {
if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
return true;
}
var id = parseInt($scope.selectedCityId);
//check if we typed an id of the detail
if (detail.pin == id) { return true; }
//check if we typed an id of the parent location
var isDetailChildOfChosenLocation = false;
$scope.shops.forEach(function(category) {
category.locations.forEach(function(location) {
//find out if the detail we are checking is within this location
var detailInThisLocation = location.details.filter(function(lDetail) {
return lDetail.pin === detail.pin;
}).length === 1;
if(detailInThisLocation && location.city_id === id) {
isDetailChildOfChosenLocation = true;
}
});
});
return isDetailChildOfChosenLocation;
};
$scope.shops =
[
{
"category_id":2,
"locations":[
{
"city_id":368,
"details": [{
"pin": 627718,
"state": 'MH'
}]
}
]
},
{
"name":"xxx",
"category_id":1,
"locations":[
{
"city_id":400,
"region_id":4,
...