Search code examples
angularag-gridag-grid-angular

how to expand all child nodes inside one parent node/group element on custom get context menu items in Ag grid Enterprise version


I am using the enterprise version of ag grid. I have a custom context menu and inside there are many parent nodes and inside each parent node, there are nested child nodes.Currently I can expand it by the parent level, but don't know how to expand all child nodes at the same time. Any help will be highly appreciated. Thanks

{
    name: 'Expand All',
    action() {
      params.api.forEachNode((node, index) => {
        if (node.group && node.groupData['ag-Grid-AutoColumn'] === 
            params.value) {
          node.setExpanded(true);
        }
      });
    }

Solution

  • I faced the same issue with AG Grid, Unfortunately I couldn't find anything useful in AG Grid API to achieve this functionality. So I had to achieve it in this way:

    name: 'Expand All',
        action: () => {
          let totalChildNodes = 0;
          let checkCount = 0;
          params.api.forEachNode((node, index) => {
            if (node.group && node.groupData['ag-Grid-AutoColumn'] === params.value) {
              totalChildNodes = node.allChildrenCount;
              node.expanded = true;
            }
            if (totalChildNodes > 0 && checkCount <= totalChildNodes) {
              checkCount++;
              node.expanded = true;
            }
          });
          params.api.onGroupExpandedOrCollapsed();
        }
    

    What the above code does is it checks the clicked node and get it's child count and then expand all its child nodes as well.