angular - 如何在完成由单元格编辑触发的异步方法之前和之后更改 Ag Grid 中的单元格样式?
问题描述
当用户更新网格中的值时,我正在发出数据请求。当我发起请求时,我希望单元格背景变为橙色。请求完成后,我希望单元格闪烁绿色,然后返回默认背景颜色。
这是我当前onCellValueChanged
方法的简化版本。
onCellValueChanged(params) {
// I thought this would change the background to orange,
// but strangely, it does nothing the first time,
// then permanently changes the background to orange if I edit the cell again.
params.colDef.cellStyle = {backgroundColor: 'orange'};
// This is my data request (works)
this.queryService.getData().then(data => {
const cellLocation = {
rowNodes: [this.gridApi.getDisplayedRowAtIndex(params.rowIndex)],
columns: [params.colDef['field']]
};
// The cells flash correctly
this.gridApi.flashCells(cellLocation);
// This seems to do nothing. I thought it would clear the orange background.
params.colDef.cellStyle = undefined;
// I thought refreshing the grid api might make cell Styles apply, but it doesn't seem to have an impact
this.gridApi.refreshCells(cellLocation);
}
}).catch(err =>
{
// This error check works if my data request fails
console.warn(err);
});
}
根据代码片段注释,getData
请求正在工作并且单元格正在闪烁,但单元格的背景颜色在第二次编辑之前不会改变,之后它会永久变为橙色。这是正确的方法吗?如果是这样,我怎样才能使这项工作?如果没有,有人有什么建议吗?
解决方案
原来问题出在refreshCells
功能上。我需要改用该redrawRows
功能。
首先,我需要获取当前行:
const row = this.gridApi.getDisplayedRowAtIndex(params.rowIndex);
然后我需要在更改单元格样式后刷新该行:
params.colDef.cellStyle = {backgroundColor: 'orange'};
this.gridApi.redrawRows({rowNodes: [row]});
然后我需要在删除样式时做同样的事情:
params.colDef.cellStyle = undefined;
this.gridApi.redrawRows({rowNodes: [row]});
推荐阅读
- javascript - 如何使用纯 JavaScript 模拟 React onMouseEnter/onMouseOver 事件
- java - 需要在骆驼石英的运行时更新 cron 表示的计时器
- java - 为什么 Spring security getPrincipal 提供 NullPointer
- c - 我丢失了结构(c)中的值
- snakemake - snakemake “--max-jobs-per-second” 参数不起作用
- html - 三个 src 相同的 gif,只重新加载其中一个
- sqlite - 如何使用 sqlite 和这个 Cordova 插件搜索 BETWEEN DATES:cordova-sqlite-ext
- function - 使用内部函数修改函数中的值!称呼
- ios - 在 swift 5.2 con Alamofire 中将数据(json)传递给另一个控制器
- html - 根据背景图像中心的屏幕尺寸水平/垂直组织图像