Javascript 如何清除过滤器 - AngularJS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12738805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Clear a Filter - AngularJS
提问by Sparda
I'm very new to AngularJS and was playing with filters today. I was able to apply the filter
filter to display only rows matching the criteria from the select menus. However, I was unable to add a "clear filter" function to the button. How can I reset the filter when the button is clicked?
我对 AngularJS 很陌生,今天正在玩过滤器。我能够应用filter
过滤器以仅显示与选择菜单中的条件匹配的行。但是,我无法向按钮添加“清除过滤器”功能。单击按钮时如何重置过滤器?
In the following Plunker, you can see the code I was using in attempts achieve this: Plunker - AngularJS Sample
在下面的 Plunker 中,您可以看到我在尝试中使用的代码: Plunker - AngularJS Sample
回答by Walter Roman
The ng-click
directive can be used to set the filter model to any falsey-but-not-false
Javascript value, such as ''
, undefined
, null
or {}
.
该ng-click
指令可用于将过滤器模型设置为任何 falsey-but-not- false
Javascript 值,例如''
, undefined
,null
或{}
。
In the following code, clicking on the div
would reset the customFilter
model.
在以下代码中,单击div
将重置customFilter
模型。
<input type="text" ng-model="customFilter" />
<div ng-click="customFilter = undefined">
Clear Filter
</div>
Here's a link to an updated fork of your Plunker project using Angular v1.4.1
回答by Tosh
You can use ng-click
to bind click handler to clear query
variable within a scope.
您可以使用ng-click
绑定单击处理程序来清除query
范围内的变量。
回答by Charan Ghate
This example will help you to clear filters if you are using the ng-table filter
如果您使用 ng-table 过滤器,此示例将帮助您清除过滤器
<table ng-table="tableParams" show-filter="true" class="table table-striped table-bordered table-condensed table-hover cf">
<tbody>
<tr ng-repeat="tableData in $data">
<td data-title="'Name'" filter="{Name:'text'}" sortable="'{Name}'" />
</tr>
</tbody>
</table>
You can clear filter from ng-clickas below
您可以从ng-click 中清除过滤器,如下所示
$scope.tableParams.parameters({ filter: {}, sorting: {}, page: 1 });