Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag-grid-Enterprise expand/collapse all row using button? Very slow crashing FF and Edge

I created a button to expand all the rows in ag-grid (Enterprise) having 150 rows in the grid. It is working fine in Chrome but it is showing an alert in the latest FF and Edge, saying the web page is making your browser slow. Any better approach to expand all the row? It is taking almost 10-15 second

HTML

<button (click)="expandAll(expand)">Expand/Collapse</button>  

JavaScript

this.columnDefs = [
           {
                headerName: "",
                field: "",
                cellRenderer: "group",// for rendering cell
                suppressMenu: true,
                suppressSorting: true
            }
           ]
           // This is how I am creating fullrow width
            this.gridOptions = <GridOptions>{
            isFullWidthCell: function (rowNode) {
            var rowIsNestedRow = rowNode.flower;
            return rowIsNestedRow;
            },
            fullWidthCellRendererFramework: AgGridInventorRowComponent,
            doesDataFlower: function (dataItem) {
            return true;
         }
    public expandAll(value:boolean) {
            if(value) {
                this.gridOptions.api.forEachNode((node) =>{
                    node.setExpanded(true);
                });
            } else {
                this.gridOptions.api.forEachNode((node) =>{
                    node.setExpanded(false);
                });
            }
        }

enter image description here

like image 757
DirtyMind Avatar asked Aug 30 '17 19:08

DirtyMind


3 Answers

As per the documentation:

Calling node.setExpanded() causes the grid to get redrawn. If you have many nodes you want to expand, then it is best to set node.expanded=true directly, and then call api.onGroupExpandedOrCollapsed() when finished to get the grid to redraw the grid again just once.

So i modified my code like below:

this.gridOptions.api.forEachNode(node => {
  node.expanded = true;
});
this.gridOptions.api.onGroupExpandedOrCollapsed();

Ag-gridDocumentation page inside Group Api

like image 166
DirtyMind Avatar answered Sep 20 '22 10:09

DirtyMind


I'm supposing that you are using the row grouping feature, and that you meant that there are 150 grouped rows that are able to be expanded.

Currently your code is getting executed for every single row of data... not just the ones that are able to be expanded. So supposing you have 50 rows or so of data in each group, your calling the setExpanded function 7500 times. You can limit this to just calling the setExpanded on the grouped rows by putting in a check before calling setExpanded:

public expandAll(value:boolean) {
    this.gridOptions.api.forEachNode((node) =>{
        if (node.group)
            node.setExpanded(value);
    });
}

testing it on this example, it took roughly 2 seconds for 110 row groups and 5 seconds for 511 row groups in firefox

like image 37
Jarod Moser Avatar answered Sep 21 '22 10:09

Jarod Moser


The API has expandAll and collapseAll:

api.expandAll();
api.collapseAll();

Note that due to the crappy architecture of AG Grid the expansion state (along with row selection etc) is lost if the row data changes or the grid is re-mounted/re-rendered. You should use deltaRowDataMode but make sure you give your rows a unique ID to help prevent this (though this option of course can cause some hard to debug bugs which I have reported).

Also if you want to restore the user expansion in this case you have no choice but to iterate and expand/collapse individual nodes.

Also they don't seem to work on a master-detail (enterprise feature) grid...

like image 35
Dominic Avatar answered Sep 24 '22 10:09

Dominic