您可以定义列配置,如下所示,我在其中添加了AGGridDatePickerComponent。如果要修改Date的格式则需要添加valueFormatter。
[{
headerName: 'Expiry Date',
field: 'expiryDate',
width: 100,
editable: true,
resizable: true,
valueFormatter: this.expiryDateFormatter,
cellEditorFramework: AgGridDatePickerComponent
}]
AGGridDatePickerCompponent 如下所示
import { Component } from '@angular/core';
import { ICellEditorAngularComp } from 'ag-grid-angular/main';
import { NgbDate } from '@ng-bootstrap/ng-bootstrap';
import { DatePipe } from '@angular/common';
@Component({
selector: 'date-editor-cell',
template: `
<ngb-datepicker (select)="onDateSelect($event)" style></ngb-datepicker>
`
})
export class AgGridDatePickerComponent implements ICellEditorAngularComp {
private params: any;
public selectedDate: any;
agInit(params: any): void {
this.params = params;
}
getValue(): any {
return this.selectedDate;
}
isPopup(): boolean {
return true;
}
onDateSelect(date: NgbDate) {
this.selectedDate = { date: { year: date.year, month: date.month + 1, day: '02' } };
this.params.api.stopEditing();
}
}
在我的用例中,我只需要月份和年份。我写了我的值格式化函数,如下所示。
expiryDateFormatter(params) {
if (params.value) {
return `${params.value.date.month - 1}/${params.value.date.year}`;
}
}