Ag-Grid Tree like structure attach cellClass to NodeChildren cells based on validation

跟風遠走 提交于 2019-12-08 12:27:09

问题


In my Jquery&JavaScript, Angular, all seperate applications i have included Ag-Grid with columnDefs like below :-

 this.columnDefs = [
  {
    headerName: "Age",
    field: "age",
    cellRenderer: "agGroupCellRenderer"
  },
  {
    headerName: "Name",
    field: "name"
  },
  {
    headerName: "Year",
    field: "year"
  },
  {
    headerName: "Country",
    field: "country"
  }
];

and my row Data is like below

 this.rowData = [
  {
    age: "Group A",
    participants: [
      {
        age: 1,
        name: "Michael Phelps",
        year: "2008",
        country: "United States"
      },
      {
        name: "A.2",
        age: 2,
        year: "2008",
        country: "United States"
      },
      {
        name: "A.3",
        age: 50,
        year: "2008",
        country: "United States"
      }
    ]}];

this.getNodeChildDetails = function getNodeChildDetails(rowItem) {
  if (rowItem.participants) {
    return {
      group: true,
      children: rowItem.participants,
    };
  } else {
    return null;
  }

Now i want to attach cellClass to children grid values based on validation, like:-

 if(age< 23 || ''){
  return['classNameThatiWantToAttach'];
 }

How to do this ??

You can make changes in below plunker also for this: -

https://plnkr.co/edit/lmjtuqfId4FIsTI5luL8?p=preview


回答1:


You can do it like this

edit the column definitions and add a cellClass function to it

 {
    headerName: "Year",
    field: "year",
    editable: true,
    cellClass: this.cellClass
  }

Then define the function and add the conditions you need and return a string with the value of the class

  cellClass(params) {
    if (params.value >= 2015)
      return "redClass"
  }

Don't forget to add the css styling for the classes.

Example




回答2:


Please see the updated plunkr showing a cell in red color as per validation of the age:

https://plnkr.co/edit/QZd2MM1LflQaruxdCmym?p=preview

this.columnDefs = [
    // ...
    {
        headerName: "Age",
        field: "age",
        cellClassRules: this.getCssClass()
    }
    // ...
];

getCssClass(): {[cssClassName: string]: (Function | string)} {
    return {
        'invalid-age': this.validateAge(),
    };
}


来源:https://stackoverflow.com/questions/55666503/ag-grid-tree-like-structure-attach-cellclass-to-nodechildren-cells-based-on-vali

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!