I have a data set that I have retrieved from a service and have used ng-repeat
to display. I need to filter this data based on a number of select elements which have been set up with ng-model=<a_name>
.
Here is the repeated element:
<li ng-repeat="item in dota_have_items | orderItemsBy:'pos':false | filter:search | itemFilter:rarity:'rarityProperName'">
In the above, search and rarity are such ng-models
.
Here is an example of the json data:
[
{
"user": "u2122",
"item": {
"defindex": 15059,
"name": "Bloodstone of the Precursor",
"itemClass": "Socket Gem",
"imageUrl": "http://cdn.dota2.com/apps/570/icons/econ/tools/bloodstone2.f4936e5a87dc66b47dbe653101cfc17de34176e6.png",
"itemHero": null,
"itemRarity": {
"rarityProperName": "Mythical",
"rarityColor": "#8847ff"
}
},
"item_classid": "170676805",
"untradable": true,
"item_quality": {
"dotaName": "Standard",
"qualityColor": "#D2D2D2"
},
"custom_name": null,
"custom_desc": null,
"uses": 1,
"in_trade": false,
"pos": 2
}
]
NOTE: Yes i understand my naming conventions are terrible. I plan on fixing this as soon as I get this working.
Now I have created a custom filter('itemFilter'):
Search.filter('itemFilter', function(){
return function(items, model, field) {
if(angular.isUndefined(model)==true || model==null || model==''){
return items;
}
console.log((model.charAt(0).toUpperCase() + model.slice(1)));
var filtered = [];
angular.forEach(items, function(item){
console.log(item.field);
if(item[field] == (model.charAt(0).toUpperCase() + model.slice(1)) ){
filtered.push(item);
}
});
return filtered;
}
});
Here's the problem. I want to make this filter so that I pass it the items, ng-model, and field that I want the model mapped to. For example, ng-model=rarity
I passed 'itemRarity'. Now what I want to do is find the field, check if the model value is the same and if so add it to the filtered array. Seems so simple but I am not sure how to iterate through the nested object to find the field.
Is there a way to directly find a key within a json object?
SOLUTION: Taken from another post
Wasnt 100% what I was looking for since I didnt want to specify the entire path to the property but it does the job well.