我在 Angular 项目中使用 ag-grid 社区版。要求是在选项卡上启用内联编辑,它已实现,但还有另一个要求是,当通过选项卡导航并到达最后一列并再次按 Tab 键时,它应该创建一个空行并关注新行的第一个元素好。我可以通过 ag-grid 函数 setRowData(data) 推送新数据。
我尝试了网格 api 中可用的 startEditingCell() 和 setFocusedCell() 。他们确实专注于元素,但输入元素中的光标没有出现。
import {
Component, OnInit
} from '@angular/core';
import {GridOptions, StartEditingCellParams} from "ag-grid-community";
@Component({
selector: 'app-angular-grid',
templateUrl: './angular-grid.component.html',
styleUrls: ['./angular-grid.component.css']
})
export class AngularGridComponent implements OnInit {
gridOptions:GridOptions;
constructor() {
}
ngOnInit() {
this.initializeGridOption();
}
initializeGridOption() {
this.gridOptions = {
onGridReady: (params)=> {
params.api.sizeColumnsToFit();
},
rowData: this.rowData,
columnDefs: this.columnDefs,
rowSelection: 'multiple',
onCellEditingStopped: function (params) {
let rowIndex = params.rowIndex;
let rowDataLength = this.rowData.length;
let colId = params.column.getColId();
if (rowDataLength === rowIndex + 1 && colId === 'price') {
let data = {id: this.rowData.length + 1, make: '', model: '', price: ''};
this.rowData.push(data);
params.api.setRowData(this.rowData);
setTimeout(() => {
params.api.startEditingCell({rowIndex: rowDataLength, colKey: 'make'});
params.api.setFocusedCell(rowDataLength, 'make');
});
}
}
};
}
columnDefs = [
{
headerName: '',
width: 40
},
{headerName: 'Make', field: 'make', editable: true},
{headerName: 'Model', field: 'model', editable: true},
{
headerName: 'Price',
field: 'price',
editable: true
}
];
rowData = [
{id: 1, make: 'Toyota', model: 'Celica', price: 35000},
{id: 2, make: 'Ford', model: 'Mondeo', price: 32000},
{id: 3, make: 'Porsche', model: 'Boxter', price: 72000}
];
}