简体   繁体   English

如何更改已在 ag-Grid 中编辑的单元格的样式?

[英]How to change styling of cells that has been edited in ag-Grid?

I want to mark cells who has been edited so the user can see which cells have been touched and altered.我想标记已编辑的单元格,以便用户可以看到哪些单元格已被触摸和更改。 I know there is a cell flash option, but that just changes the background colors for a bit.我知道有一个单元格 flash 选项,但这只是稍微改变了背景 colors。 What I want is a background color change when an edit has been done.我想要的是编辑完成后背景颜色的变化。

Cannot seem to find any specific documentation on accessing for example the html element or the styling of affected cell.似乎找不到任何有关访问的特定文档,例如 html 元素或受影响单元格的样式。

Anyone got any ideas?有人有什么想法吗?

You can use ColDef.onCellValueChanged to detect if something changes and update the cell style accordingly using GridApi.refreshCells()您可以使用ColDef.onCellValueChanged来检测是否有变化并使用GridApi.refreshCells()相应地更新单元格样式

const columnDefs = [{
  headerName: "Athlete",
  field: "athlete",
  onCellValueChanged: this.markAsDirty
},...]

...

private markAsDirty(params: ICellRendererParams) {
  params.colDef.cellClass = (p) =>
    p.rowIndex.toString() === params.node.id ? "ag-cell-dirty" : "";

  params.api.refreshCells({
    columns: [params.column.getId()],
    rowNodes: [params.node],
    force: true // without this line, the cell style is not refreshed at the first time
  });
}

In your css file在您的 css 文件中

:host ::ng-deep .ag-cell-dirty {
  background-color: rgba(255, 193, 7, 0.5) !important;
}

You may also want to use defaultColDef if you want this behavior applied to all columns如果您希望将此行为应用于所有列,您可能还需要使用defaultColDef

this.gridOptions = {
  defaultColDef: {
    editable: true,
    onCellValueChanged: this.markAsDirty
  },
};

Live Demo现场演示

编辑 AgGrid 在编辑时标记脏

I did this on a project I was working on.我在我正在做的一个项目上做了这个。

There is a cellClass property that you can define in your column definitions ( https://www.ag-grid.com/javascript-grid-cell-styles/ ) and it can take a callback function with params: CellClassParams .您可以在列定义中定义一个cellClass属性( https://www.ag-grid.com/javascript-grid-cell-styles/ ),它可以使用params: CellClassParams

So try doing:所以尝试这样做:

cellClass: (params: CellClassParams) => {
  // you need to have a handle on the original untouched data
  // See if the original value does not equal the modified value in params
  // For shortness, I will write pseudocode
   if (originalValue !== modifiedValue) {
     return 'ag-cell-was-modified';
   }
}

If many columns are editable, you may want to use a re-usable function for cellClass for each column.如果许多列是可编辑的,您可能希望为每列的cellClass使用可重复使用的 function。

That should apply the class ag-cell-was-modified conditionally whether the cell was modified or not and in your style.scss or main stylesheet, you can add:无论单元ag-cell-was-modified是否被修改,并且在您的 style.scss 或主样式表中,您可以添加:

.ag-cell-was-modified {
  background: red;
}

Of course, if you are using SASS, you can place this class definition in somewhere more appropriate.当然,如果您使用的是 SASS,则可以将此 class 定义放在更合适的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM