Implementing a DateTimePicker Filter in Grid
Environment
Product | Progress® Kendo UI for Angular Grid |
Description
How to implement a DateTimePicker as a custom Grid filter row component?
Solution
By default, the Grid uses the DatePicker as the built-in component when filtering a date column. To use DateTimePicker instead, customize the default filter with the FilterCellTemplateDirective
.
-
Use the
FilterCellTemplateDirective
in the column whose filter needs to be customized.<kendo-grid-column field="FirstOrderedOn"> <ng-template kendoGridFilterCellTemplate let-filter> ... </ng-template> </kendo-grid-column>
-
In a separate file create the
DateTimePicker
component that is going to act as a filter in the Grid.<kendo-datetimepicker (valueChange)="onChange($event)" [format]="'dd/MM/yyyy'"> </kendo-datetimepicker>
-
Handle the
DateTimePicker's
built-invalueChange
event and use theapplyFilter
andupdateFilter
methods of theBaseFilterCellComponent
to filter the column.export class DateTimeFilterComponent extends BaseFilterCellComponent { ... constructor(filterService: FilterService) { super(filterService); } public onChange(value: any): void { this.applyFilter( this.updateFilter({ field: this.valueField, operator: 'eq', value: value }) ); } }
-
To clear the filter, handle the click event of a button and use the
removeFilter
method of theBaseFilterCellComponent
.public clearFilter() { this.applyFilter(this.removeFilter('FirstOrderedOn')); ... }
-
Optionally, you can toggle the visibility of the button based on the applied filter with the *ngIf Angular directive.
<button kendoButton *ngIf='clearButton' ...></button>
The following example shows the full implementation of the described approach.