简体   繁体   中英

Filtering nested JSON data based on id Angularjs

I checked this plunker: Plunker: http://plnkr.co/edit/5E7FYqNNqDuqFBlyDqRh?p=preview

It worked for me to some extent. But my issue is i have a nested json data. Where the above filter code is not working. Following is my json data where I am filtering based upon "category_id" key.

JSON ::

[{
"category_id": 5,
"category_name": "Home",
"image": "Home_544f75960ee0e",
"parent_id": 0,
"i": 1,
"categories": [
  {
    "category_id": 7,
    "category_name": "Home Safe",
    "image": "Home Safe_5411af45ac923",
    "parent_id": 5,
    "i": 2,
    "categories": [
      {
        "category_id": 13,
        "category_name": "Mechanical Safes ",
        "image": "Mechanical Safes _540ab92ee1ff7",
        "parent_id": 7,
        "i": 3
      },
      {
        "category_id": 14,
        "category_name": "Electronic Safes ",
        "image": "Electronic Safes _540ab93c6e305",
        "parent_id": 7,
        "i": 4
      }
    ]
  },
  {
    "category_id": 8,
    "category_name": "Video Door Phones ",
    "image": "Video Door Phones _540ab57a466ff",
    "parent_id": 5,
    "i": 3
  },
  {
    "category_id": 9,
    "category_name": "Alarm Systems ",
    "image": "Alarm Systems _540ab58b903e9",
    "parent_id": 5,
    "i": 4
  },
  {
    "category_id": 10,
    "category_name": "Home CCTV Cameras ",
    "image": "Home CCTV Cameras _540ab59c59f44",
    "parent_id": 5,
    "i": 5
  },
  {
    "category_id": 11,
    "category_name": "Car Safes ",
    "image": "Car Safes _540ab5b0dcc57",
    "parent_id": 5,
    "i": 6
  },
  {
    "category_id": 12,
    "category_name": "Hotel Safes ",
    "image": "Hotel Safes _540ab5bddae51",
    "parent_id": 5,
    "i": 7
  }
]},{
"category_id": 6,
"category_name": "Institution",
"image": "Institution_541304aa0a52d",
"parent_id": 0,
"i": 2,
"categories": [
  {
    "category_id": 15,
    "category_name": "Physical Security Products ",
    "image": "Physical Security Products _54130515e2cb3",
    "parent_id": 6,
    "i": 3,
    "categories": [
      {
        "category_id": 18,
        "category_name": "Record Protecting Equipment ",
        "image": "Record Protecting Equipment _541305cb5f47a",
        "parent_id": 15,
        "i": 4
      },
      {
        "category_id": 19,
        "category_name": "Burglary and Fire Resistant Safes",
        "image": "Burglary and Fire Resistant Safes_541305db69acf",
        "parent_id": 15,
        "i": 5
      },
      {
        "category_id": 20,
        "category_name": "Vault Equipment ",
        "image": "Vault Equipment _541305e8d905c",
        "parent_id": 15,
        "i": 6
      },
      {
        "category_id": 21,
        "category_name": "Vault Accessories",
        "image": "Vault Accessories_541305f6ed3a4",
        "parent_id": 15,
        "i": 7
      }
    ]
  },
  {
    "category_id": 16,
    "category_name": "Premises Security Solutions ",
    "image": "Premises Security Solutions _54130525074c9",
    "parent_id": 6,
    "i": 4
  },
  {
    "category_id": 17,
    "category_name": "Marine Solutions ",
    "image": "Marine Solutions _54130530a10da",
    "parent_id": 6,
    "i": 5
  }
]}]

here i am able to filter "category_id" = 5 but not able to filter "category_id" = 7

@josep Following is my code :

var categoriesdata = $filter('filter')($rootScope.catjsondata, {category_id:$stateParams.categoryId})[0];

here $rootscope.catjsondata contains the nested json data. $stateparams.categoryId will provide me the category_id of the object clicked in the list. so every time the id will change. Its not hardcoded. the values are dynamic.

Following is my controller code:

.controller('SubCategoriesCtrl', function($scope, $filter, $stateParams, $ionicNavBarDelegate, subcategoriesfactory, globalurlfactory, $rootScope) {
$scope.baseUrl = globalurlfactory;
console.log("$rootScope.catjsondata :: ", $rootScope.catjsondata);
console.log("$stateParams.categoryId :: ", $stateParams.categoryId);    

var categoriesdata = $filter('filter')($rootScope.catjsondata, {category_id:$stateParams.categoryId})[0];

console.log("categoriesdata ::::: ", categoriesdata);
    //$rootScope.catjsondata = categoriesdata;
    $scope.categoryTitle = categoriesdata.category_name;
    $scope.categoriesIn = categoriesdata.cats_in;
    $scope.categories = categoriesdata.categories;
    $scope.has_product = categoriesdata.has_product;

    if(categoriesdata.has_product == "yes")
    {
        $scope.categoryTitle = categoriesdata.category_name;
        $scope.products = categoriesdata.product;
    }


$scope.goBack = function(){
    $scope.isBack = true;
    $ionicNavBarDelegate.back();
}})

I think you need to iterate over the categories because they are nested inside the others.

I changed the json from your plunkr to simulate your real json.
Take a look at this one .

Nothing else comes to my mind right now.

-- Edited

Okay, this is the working plunkr with your JSON: plunkr

As other mentioned, since there is no built in support for this (as far as I know) and you don't know the tree's depth, you have to recursively (or iteratively, recursively is simpler) find it. Here is an updated plunker: http://plnkr.co/edit/sFaBnCnhXzx08pVX5qXe?p=preview

Also the key function is attached for others (it is just a simple recursion that looks for the element by expression):

.filter('recursiveExpression', function($filter) {
    return function(input, exp) {
        return recursivelyFilterExpression(input, exp);
    }

    function recursivelyFilterExpression(input, exp) {
        var filterResult = $filter('filter')(input, exp);
        // Any result?
        if (filterResult.length > 0) {
            return filterResult[0];
        }

        if (angular.isArray(input) || angular.isObject(input)) {
            for (var key in input) {
                if (angular.isArray(input[key]) || angular.isObject(input[key])) {
                    var internalResult = recursivelyFilterExpression(input[key], exp);
                    if (internalResult) {
                        return internalResult;
                    }   
                }
            }
        }

        return null;
     }
  });

** Make sure the expression is not costly to evaluate, in which case this won't be very so sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM