我在angular项目中使用了ag-grid社区版。要求是启用选项卡上的内联编辑,这一要求已实现,但是还有另一个要求是,当在选项卡中导航并到达最后一列并再次按下选项卡键时,它应创建一个空行并聚焦到新行的第一个元素好。我可以通过农业网格功能setRowData(data)推送新数据。
我尝试了在Grid 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}
];
}
答案 0 :(得分:1)
您不需要通过setFocusedCell
明确设置单元格焦点。
检查我创建的此示例样本:Cell Editing
到达最后一个单元格并点击选项卡,观察input
已经聚焦。
setTimeout(() => {
params.api.startEditingCell({rowIndex: rowDataLength, colKey: 'make'});
//params.api.setFocusedCell(rowDataLength, 'make');
});