简体   繁体   中英

Ag-grid tree data - trying to expand specific parent node but the node is undefined

I have tree data in ag-grid that is coming in like this:

[{"colorName": "Magenta", parentColor: "Red", "namePath": ["Red", "Magenta"]},
{"colorName": "Pink", parentColor: "Red", "namePath": ["Red", "Pink"]},
{"colorName": "Cerulean", parentColor: "Blue", "namePath": ["Blue", "Cerulean"]},
{"colorName": "Goldenrod", parentColor: "Yellow", "namePath": ["Yellow", "Goldenrod"]}]

The ag-grid ends up with Red, Blue, and Yellow as parent rows, which you can click to expand to reveal the child rows, like Magenta and Pink for Red. Now I am trying to automatically expand a specific parent row upon load. For example, say a user's preferred color is Red, so when the ag-grid loads, I want the Red parent group to be already expanded.

The problem is I have trouble identifying the node programmatically. I tried going through the node data with forEachNode, like:

onFirstDataRendered(params) {
   this.gridApi.forEachNode((node) => {
    if (node != undefined && node.data != undefined) {
       console.log("node: " + JSON.stringify(node.data);
    }
  });
}

I only get node data from the child rows, like Magenta or Pink. The nodes for the parent rows are undefined. I thought maybe I could access the parent row by going up a row index from the first child row, but I'm unable to figure out how to traverse up a row index and use it to expand the row.

I figured it out. Instead of using node.data, I used node.key. Now I can see the parent row name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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