简体   繁体   English

根据单元格值更改时的条件对网格单元格进行样式设置

[英]Styling ag-grid cell based on condition when cell value is changed

I wanted to apply a css style to a cell, only if the oldValue and newValue are different after the cell is edited. 我想对单元格应用css样式,只有在单元格编辑后oldValue和newValue不同的情况下。 So, I did the below in onCellValueChanged() handler, 因此,我在onCellValueChanged()处理程序中执行了以下操作,

 onCellValueChanged: function(params) {
    if (params.oldValue !== params.newValue) {
       params.colDef.cellStyle = function(params) {
         return {
          backgroundColor: 'green'
        };
      }
      params.api.redrawRows();
   }
}

But this apply the css change to all the cells for that particular column when the condition is met. 但这会在条件满足时将css更改应用于该特定列的所有单元格。 I am not sure how to apply 'cellStyle' only to the cell affected. 我不确定如何仅将“ cellStyle”应用于受影响的单元格。

Update 1: I changed it to below and it started working , 更新1:我将其更改为以下内容并开始工作,

onCellValueChanged: function(params) {
console.log(params);
 var cellValue = params.data[params.colDef.field];
if (params.oldValue !== params.newValue) {
    params.colDef.cellStyle = function(params) {
      if(params.value==cellValue){
        return {
          backgroundColor: 'green'
        };
      }
    }
  params.api.redrawRows();
}
}

The problem now I face is that the cell styling (in this case background becoming green) is lost when I edit any other cell under the same column. 我现在面临的问题是,当我在同一列下编辑任何其他单元格时,单元格样式(在这种情况下背景变为绿色)丢失了。 Demo 演示版

This issue is fixed now, this is the working demo 现在已解决此问题,这是有效的演示

function cellValueChangedListener(params) {
  var oldValue = params.oldValue;
  var newValue = params.newValue;

  var recordKey = params.data.key;
  var columnField = params.colDef.field;

  if (!_.isEqual(oldValue, newValue)) {
    if (!cellMetadata[recordKey]) {
      cellMetadata[recordKey] = {};
  }
  if (!cellMetadata[recordKey][columnField]) {
    cellMetadata[recordKey][columnField] = {};
  }

  cellMetadata[recordKey][columnField].trackChanges = true;
  cellMetadata[recordKey][columnField].tooltip = 
  columnField.toUpperCase() + ' ' + 'Changed from' + ' ' + oldValue + ' ' 
+ 'to' + ' ' + newValue;
  }

  params.api.redrawRows();
}

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

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