I'm going off by this example fiddle where it demonstrates the use of parator parameter to filter exact matches....:
/
priority is a number from 1-100, but I have it input as text and filtered as string so any data that includes a substring will also pass through the ng-repeat...like when I type 1 it will also display 11, 111, 132 etc...which is how I came across the :true parator.
I've read other stackflow answers suggesting to write custom filter functions but with the true parator it looks like I can achieve what I want just by:
<input type="text" class="form-control" id="search.priority"
title='Priority number to filter by'
ng-model="search.priority" >
<tr ng-repeat="workflowItem in workflows | filter:search:true">
<td>{{workflowItem.priority}}</td>
where it does only filter the exact matches. However, obviously it does not pass anything when the input field is empty since nothing matches the empty string.
My question is: Is there a way I can allow ng-repeat to still display everything when the field is empty while keeping the exact match filter? Appreciate everyone's time!
I'm going off by this example fiddle where it demonstrates the use of parator parameter to filter exact matches....:
http://jsfiddle/api/post/library/pure/
priority is a number from 1-100, but I have it input as text and filtered as string so any data that includes a substring will also pass through the ng-repeat...like when I type 1 it will also display 11, 111, 132 etc...which is how I came across the :true parator.
I've read other stackflow answers suggesting to write custom filter functions but with the true parator it looks like I can achieve what I want just by:
<input type="text" class="form-control" id="search.priority"
title='Priority number to filter by'
ng-model="search.priority" >
<tr ng-repeat="workflowItem in workflows | filter:search:true">
<td>{{workflowItem.priority}}</td>
where it does only filter the exact matches. However, obviously it does not pass anything when the input field is empty since nothing matches the empty string.
My question is: Is there a way I can allow ng-repeat to still display everything when the field is empty while keeping the exact match filter? Appreciate everyone's time!
Share Improve this question asked Jan 18, 2014 at 3:46 ZvKaZvKa 1382 gold badges4 silver badges21 bronze badges 3- Check the jsfiddle url. – Joel Skrepnek Commented Jan 18, 2014 at 4:14
- My understanding is that the filter filter will return all elements when the predicate is an empty string. See jsfiddle/3aD8N. – Joel Skrepnek Commented Jan 18, 2014 at 4:18
- Sorry - I see the issue now. – Joel Skrepnek Commented Jan 18, 2014 at 4:23
1 Answer
Reset to default 14You'll want to use a custom parator function. It will allow you to perform a strict parison except when the predicate is falsy.
Your markup would then be:
<input type="text" class="form-control" id="search.priority"
title='Priority number to filter by'
ng-model="search.priority" >
<tr ng-repeat="workflowItem in workflows | filter:search:exceptEmptyComparator">
<td>{{workflowItem.priority}}</td>
And define the parator function on your controller:
$scope.exceptEmptyComparator = function (actual, expected) {
if (!expected) {
return true;
}
return angular.equals(expected, actual);
}
That should do the trick.