使用选择清除angularJS中的过滤器 [英] clearing filter in angularJS using select
问题描述
我在下面给出的 HTML 中使用了 ngOptions 指令:
我在 ng-repeat 中用来显示我的数据的过滤器是这样的:
filter:{GroupId:selectedGroupToShow.Id}"
即使我使用
filter:selectedGroupToShow
问题保持不变.重置过滤器..
这很完美,但是当我想清除过滤器(选择默认选项 - 'All')时,它只会显示根本没有设置 GroupId 参数的数据.
在选择中选择默认选项元素时,如何显示所有数据(清除过滤器)?
我想我了解您的问题,您面临的问题是下拉列表选择为 --ALL-- 其值为 null 且未定义.因此,要重置它,您必须将其设置为未定义.
<div ng-repeat="item in items| filter:{itemId:selectedGroupToShow.Id||undefined}">{{item.itemId}}
请参考我为您的问题创建的以下小提琴.
I use the ngOptions directive in the HTML given below:
<select ng-model="selectedGroupToShow.Id" ng-options="g.Id as g.Name for g in myGroups">
<option value>-- All --</option>
</select>
The filter I am using in ng-repeat to show my data goes like this:
filter:{GroupId:selectedGroupToShow.Id}"
Even if I use
filter:selectedGroupToShow
the issue stays the same.Resetting the filter..
This works perfectly, but when I want to clear the filter (select default option - 'All') it will only show data that doesn't have GroupId parameter set at all.
How can I show ALL the data (clear the filter) when choosing default option element within select?
I think I understand your problem and the issue you are facing is when dropdown selected to --ALL-- its value is null and not undefined. So to reset it you have to set it for undefined.
<div ng-repeat="item in items| filter:{itemId:selectedGroupToShow.Id||undefined}">
{{item.itemId}}
</div>
Please refer the below fiddle I have created for your problem.
这篇关于使用选择清除angularJS中的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!