Implementing a DateTimePicker Filter Row 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
FilterCellTemplateDirectivein the column whose filter needs to be customized.html<kendo-grid-column field="FirstOrderedOn"> <ng-template kendoGridFilterCellTemplate let-filter> ... </ng-template> </kendo-grid-column> -
In a separate file create the
DateTimePickercomponent that is going to act as a filter in the Grid.html<kendo-datetimepicker (valueChange)="onChange($event)" [format]="'dd/MM/yyyy'"> </kendo-datetimepicker> -
Handle the
DateTimePicker'sbuilt-invalueChangeevent and use theapplyFilterandupdateFiltermethods of theBaseFilterCellComponentto filter the column.tsexport 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
removeFiltermethod of theBaseFilterCellComponent.tspublic 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.
html<button kendoButton *ngIf='clearButton' ...></button>
The following example shows the full implementation of the described approach.