Angular Grid: Number Filter
Number filters allow you to filter number data.
The Provided Filters and Simple Filters pages explain the parts of the Number Filter that the same as the other Provided Filters. This page builds on that and explains some details that are specific to the Number Filter.
Number Filter Parameters
Number Filters are configured though the filterParams
attribute of the column definition. All of the parameters from Provided Filters are available:
buttons | Specifies the buttons to be shown in the filter, in the order they should be displayed in. The options are:
|
closeOnApply | If the Apply button is present, the filter popup will be closed immediately when the Apply or Reset button is clicked if this is set to true . |
debounceMs | By default the Text and Number filters will debounce by 500ms. This is because these filters have text field inputs, so time is given to the user to type items in before the input is formatted and the filtering applied. The Set and Date will execute immediately (no debounce). To override these defaults, set debounceMs to the number of milliseconds to debounce by. |
newRowsAction | This property is for when using the Client Side Row Model only. When set to 'clear' , updating the data in the grid by calling api.setRowData() (or updating the rowData property if bound by a framework) will clear (reset) the filter. If you instead set this to 'keep' , the grid will keep its currently set filter when the data is updated.Default: 'clear' |
In addition, the following parameters are also available:
alwaysShowBothConditions | By default, only one condition is shown, and a second is made visible once a first condition has been entered. Set this to true to always show both conditions. In this case the second condition will be disabled until a first condition has been entered.Default: false | Text, Number, Date |
filterOptions | Array of Filter Options to present to the user. Options: 'equals' , 'notEqual' , 'contains' , 'notContains' , 'startsWith' , 'endsWith' , 'lessThan' , 'lessThanOrEqual' , 'greaterThan' , 'greaterThanOrEqual' , 'inRange' , 'empty' | Text, Number, Date |
defaultOption | The default Filter Option to be selected. Options: 'equals' , 'notEqual' , 'contains' , 'notContains' , 'startsWith' , 'endsWith' , 'lessThan' , 'lessThanOrEqual' , 'greaterThan' , 'greaterThanOrEqual' , 'inRange' , 'empty' | Text, Number, Date |
defaultJoinOperator | By default, the two conditions are combined using AND . You can change this default by setting this property.Options: 'AND' , 'OR' | Text, Number, Date |
suppressAndOrCondition | If true , the filter will only allow one condition.Default: false | Text, Number, Date |
inRangeInclusive | If true , the 'inRange' filter option will include values equal to the start and end of the range. | Number, Date |
includeBlanksInEquals | If true , blank (null or undefined ) values will pass the 'equals' filter option. | Number, Date |
includeBlanksInLessThan | If true , blank (null or undefined ) values will pass the 'lessThan' and 'lessThanOrEqual' filter options. | Number, Date |
includeBlanksInGreaterThan | If true , blank (null or undefined ) values will pass the 'greaterThan' and 'greaterThanOrEqual' filter options. | Number, Date |
includeBlanksInRange | If true , blank (null or undefined ) values will pass the 'inRange' filter option. | Number, Date |
allowedCharPattern | When specified, the input field will be of type text instead of number , and this will be used as a regex of all the characters that are allowed to be typed. This will be compared against any typed character and prevent the character from appearing in the input if it does not match, in supported browsers (all except Safari). | Number |
numberParser | Typically used alongside allowedCharPattern , this provides a custom parser to convert the value entered in the filter inputs into a number that can be used for comparisons. | Number |
Custom Number Support
By default, the Number Filter uses HTML5 number
inputs. However, these have mixed browser support, particularly around locale-specific nuances, e.g. using commas rather than periods for decimal values. You might also want to allow users to type other characters e.g. currency symbols, commas for thousands, etc, and still be able to handle those values correctly.
For these reasons, the Number Filter allows you to control what characters the user is allowed to type, and provide custom logic to parse the provided value into a number to be used in the filtering. In this case, a text
input is used with JavaScript controlling what characters the user is allowed (rather than the browser).
Custom number support is enabled by specifying configuration similar to the following:
<ag-grid-angular
[columnDefs]="columnDefs"
/* other grid options ... */>
</ag-grid-angular>
this.columnDefs = [
{
field: 'age',
filter: 'agNumberColumnFilter',
filterParams: {
allowedCharPattern: '\\d\\-\\,', // note: ensure you escape as if you were creating a RegExp from a string
numberParser: text => {
return text == null ? null : parseFloat(text.replace(',', '.'));
}
}
}
];
The allowedCharPattern
is a regex of all the characters that are allowed to be typed. This is surrounded by square brackets []
and used as a character class to be compared against each typed character individually and prevent the character from appearing in the input if it does not match, in supported browsers (all except Safari).
The numberParser
should take the user-entered text and return either a number if one can be interpreted, or null
if not.
The example below shows custom number support in action. The first column shows the default behaviour, and the second column uses commas for decimals and allows a dollar sign ($) to be included.