How to make Angular ui grid expand all rows initially?

落花浮王杯 提交于 2019-12-06 22:36:57

问题


I am using ui grid to show a list of data and I am trying to initially expand all rows.

I am trying to do this in the onRegisterApi event:

scope.GridOptions =
        {
            data: properties,
            columnDefs: 
            [
                { name: "Full Address", field: "FullAddress" },
                { name: "Suburb", field: "Suburb" },
                { name: "Property Type", field: "PropertyType" },
                { name: "Price", field: "Price", cellFilter: 'currency'},
                { name: "Status", field: "Status" },
                { name: "Sale Type", field: "SaleType" }, 
                { name: "Date Created", field: "CreateDate", cellFilter: "date:'dd/MM/yyyy HH:mma'"}
            ],
            expandableRowTemplate: 'template.html',
            expandableRowHeight: 200,
            onRegisterApi: (gridApi) => 
            {
                scope.gridApi = gridApi;
                gridApi.expandable.on.rowExpandedStateChanged(scope,(row) =>
                {
                    if (row.isExpanded) {
                        this.scope.GridOptions.expandableRowScope = row.entity;
                    }
                });
                gridApi.expandable.expandAllRows();

            }
        };

But the code above does not work. It looks like when I call expandAllRows() the rows are not rendered yet.


回答1:


In my case, the following worked:

    $scope.gridOptions = {
      ...
      onRegisterApi: function(gridApi) {
        $scope.gridApi = gridApi;
        $scope.gridApi.grid.registerDataChangeCallback(function() {
          $scope.gridApi.treeBase.expandAllRows();
        });
      }
    };



回答2:


I find I can expand all rows by using rowsRendered event:

gridApi.core.on.rowsRendered(scope,() => {
        if (!gridApi.grid.expandable.expandedAll && !initialized)
        {
            gridApi.expandable.expandAllRows();
            initialized = true;
         }
});

I have used a variable initialized to identify if this is the first time rows are rendered as I only want to expand all rows initially.




回答3:


None of the above worked for me for all of my grid use cases.

        $scope.gridApi.grid.registerDataChangeCallback(function() {
            if($scope.gridApi.grid.treeBase.tree instanceof Array){
                $scope.gridApi.treeBase.expandAllRows();
            }

        });

The following works in every case I have tested. DataChangeCallback is called twice (for some unknown reason) on initial page load. The first time, gridApi.grid.treeBase.tree is an object which causes the issue with gridApi.grid.treeBase.tree.forEach above:




回答4:


None of these answers worked for me, the following did:

scope.gridApi.core.on.rowsRendered(null, () => {
    scope.gridApi.treeBase.expandAllRows();
});


来源:https://stackoverflow.com/questions/30893210/how-to-make-angular-ui-grid-expand-all-rows-initially

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