Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Clear a Filter - AngularJS

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?

In the following Plunker, you can see the code I was using in attempts achieve this: Plunker - AngularJS Sample

like image 370
Sparda Avatar asked Oct 05 '12 02:10

Sparda


People also ask

How to clear filters in AG Grid?

The Clear button clears just the filter UI, whereas the Reset button clears the filter UI and removes any active filters for that column. The Cancel button will discard any changes that have been made in the UI, restoring the state of the filter to match the applied model.

How does filter work in AngularJS?

The “filter” Filter in AngularJS is used to filter the array and object elements and return the filtered items. In other words, this filter selects a subset (a smaller array containing elements that meet the filter criteria) of an array from the original array.


1 Answers

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 {}.

In the following code, clicking on the div would reset the customFilter model.

<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

like image 109
Walter Roman Avatar answered Sep 19 '22 09:09

Walter Roman