首先,为了解决你的问题,你需要在调用api.refreshCells()时强制刷新:
var p = {
force: true
};
this.gridApi.refreshCells(p);
这将在编辑后第一次更改单元格样式。
其次,您正在更新整个列的单元格样式,而不是特定单元格。您应该做的是在列定义上设置cellStyle,以在单元格已被编辑时显示您的绿色背景。这意味着您需要保留已编辑单元格的记录。
columnDefs = [
{
field: 'make',
editable: true,
cellStyle: params => {
debugger;
if (this.editedRowIndexes.includes(params.node.rowIndex)) {
return { backgroundColor: 'green' };
}
}
},
{ field: 'model' },
{ field: 'price' }
];
rowData = [
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxter', price: 72000 }
];
gridApi;
editedRowIndexes = [];
onGridReady(params) {
this.gridApi = params.api;
}
onCellValueChanged(params: any) {
if (!this.editedRowIndexes.includes(params.node.rowIndex)) {
this.editedRowIndexes.push(params.rowIndex);
}
this.editedRowIndexes.push(params.rowIndex);
var p = {
force: true
};
this.gridApi.refreshCells(p);
}
每次编辑单元格时,将rowIndex 记录在数组editedRowIndexes 中,然后使用该数组确定是否应显示绿色背景。
Demo.