I want to use the filter
in angular and want to filter for multiple values, if it has either one of the values then it should be displayed.
I have for example this structure:
An object movie
which has the property genres
and I want to filter for Action
and Comedy
.
I know I can do filter:({genres: 'Action'} || {genres: 'Comedy'})
, but what to do if I want to filter it dynamically. E.g. filter: variableX
How do I set variableX
in the $scope
, when I have an array of the genres I have to filter?
I could construct it as a string and then do an eval()
but I don't want to use eval()...
Angular Or Filter Module
$filter('orFilter')([{..}, {..} ...], {arg1, arg2, ...}, false)
here is the link: https://github.com/webyonet/angular-or-filter
Too late to join the party but may be it can help someone:
We can do it in two step, first filter by first property and then concatenate by second filter:
See the working fiddle with multiple property filter
You can also use
ngIf
if the situation permits:I had similar situation. Writing custom filter worked for me. Hope this helps!
JS:
HTML:
the best answer is :
Here is the implementation of custom filter, which will filter the data using array of values.It will support multiple key object with both array and single value of keys. As mentioned inangularJS API AngularJS filter Doc supports multiple key filter with single value, but below custom filter will support same feature as angularJS and also supports array of values and combination of both array and single value of keys.Please find the code snippet below,
Usage:
Here is a fiddle example with implementation of above "filterMutiple" custom filter. :::Fiddle Example:::