Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  133] [ 5]  / answers: 1 / hits: 30605  / 10 Years ago, mon, march 24, 2014, 12:00:00

I would like to know what is the most elegant and simple way to implement this.
I need to add a filter expression for a ng-repeat that would filter 2 conditions from one property.



In this example http://plnkr.co/edit/OMQxXvSjtuudMRGE4eZ8?p=preview




  • If you enter A, it would display checkbox for A,

  • enter B - display checkbox for B.



But I want to display the specified checkboxes plus anything with empty condition.
There is no condition for C, so:




  • if you enter A, I want to display both A and C checkboxes,

  • enter B, I want to display both B and C checkboxes.


More From » angularjs

 Answers
48

I would create custom filter like:



app.filter('myfilter', function() {
return function( items, condition) {
var filtered = [];

if(condition === undefined || condition === ''){
return items;
}

angular.forEach(items, function(item) {
if(condition === item.condition || item.condition === ''){
filtered.push(item);
}
});

return filtered;
};
});


and usage:



<span ng-repeat=charge in charges  |  myfilter:level.condition>


See Demo in Plunker



It looks pretty elegant and clear.



Hope it will help


[#71803] Sunday, March 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reina

Total Points: 241
Total Questions: 82
Total Answers: 94

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;