简体   繁体   中英

ng-repeat filter key and Data

i am having trouble iterating over keys and data using ng-repeat i have a fiddle here https://jsfiddle.net/prantikv/ddLpfmvh/2/

i have json data like so:

    {
  "container1": {
      "value1": "data1",
      "value2": "data2",
      "value3": "data3",
      "value4": "data4",
      "value5":{
            "innerArray":
                  [{"innerValue1":"innerData1"},
                  {"innerValue1":"innerData2"}
                  ]
              }

   },
  "container2": {
      "value1": "data1",
      "value2": "data2",
      "value3": "data3",
      "value4": "data4",
      "value5":{
            "innerArray":
                  [{"innerValue1":"innerData1"},
                  {"innerValue1":"innerData2"}
                  ]
              }

   }

}

my view is this

<input type="text" ng-model="searchInput" />
 <ul>
    <li data-ng-repeat="(key,value) in reports | filter:searchInput">
        {{ key }}</li>
</ul>

What i want to achieve is that i want to filter over the keys(container1,container2,etc) and also over values (value1,value2,etc)

How can this be achieved? should I change my JSON if so the please do tell.

Using your object structure, which is not optimal, you could use ng-if and create a function in controller to check the values

<li data-ng-repeat="(key,value) in reports " ng-if="hasSearch(value)" >

I'm not 100% clear of what you are trying to do and your data sample is almost duplicated but take a look at something like:

$scope.hasSearch = function(obj){
    if(!$scope.searchInput){
        return true;
    }else{            
        for (key in obj){
            if(typeof obj[key] == 'string' && obj[key].indexOf( $scope.searchInput) >-1){
                return true
            }                
        }
        return false;
    }
}

to better make use of angular I would convert your structure to arrays which can be ordered and filtered whereas objects can not

DEMO

How can this be achieved? should I change my JSON if so the please do tell.

You can create a custom filter function which will require you to modify you json to something like

[{
        "containers":[
        {
            "container":{
                "value1":["data1"],
                "value2":["data2"],
                "name":"container1"
            }
        },
        {
            "container":{
                "value1":["data3"],
                "value2":["data4"],                
                "name":"container2"
             }
        }]
    }];

And your filter can look something like this

app.filter('containerFilter', function(){
    return function(objects, criteria){
        var filterResult = new Array();
        if(!criteria)
            return objects;

        for(index in objects) {
            // add more 
            if(objects[index].container.name.indexOf(criteria) != -1 ||
              objects[index].container.value1.indexOf(criteria) != -1 ||
                objects[index].container.value2.indexOf(criteria) != -1 )
                filterResult.push(objects[index]);
        }

        return filterResult;  
    }    

Here is a link to the demo

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