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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:08:46  来源:igfitidea点击:

How to Clear a Filter - AngularJS

javascriptangularjs

提问by Sparda

I'm very new to AngularJS and was playing with filters today. I was able to apply the filterfilter 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-clickdirective can be used to set the filter model to any falsey-but-not-falseJavascript value, such as '', undefined, nullor {}.

ng-click指令可用于将过滤器模型设置为任何 falsey-but-not- falseJavascript 值,例如'', undefined,null{}

In the following code, clicking on the divwould reset the customFiltermodel.

在以下代码中,单击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

这是使用 Angular v1.4.1 的 Plunker 项目更新分支的链接

回答by Tosh

You can use ng-clickto bind click handler to clear queryvariable within a scope.

您可以使用ng-click绑定单击处理程序来清除query范围内的变量。

http://plnkr.co/edit/p1DnoV

http://plnkr.co/edit/p1DnoV

回答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 });