1

I am using ui-grid to show a list of data and on display of the grid I am trying to expand some of the rows depending on the data.

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) => 
    {
        gridApi.expandable.expandAllRows();
    }
};

The problem is gridApi.expandable.expandAllRows() expands all of the grouped sections. I see there is a expandRow function, but I am not sure how to use it in place of the expandAllRows function. I really would like to expand the group that has the column Status set to a particular value. Can someone help me figure this out?

2 Answers 2

6

Here is a way to handle expanding of selective rows. I have created a Plunker link (http://plnkr.co/edit/CSaROQqKxYnkoxm2Tl6b?p=preview) where I am expanding all the even rows. In a similar way, you can iterate over the array and expand the required rows.

Include:
"$timeout" dependency in the controller

To expand first row:

$timeout(function() {
    var rows = $scope.gridApi.grid.rows;
    var entity = (rows.length && rows[0]) ? rows[0].entity : null;
    $scope.gridApi.expandable.toggleRowExpansion(entity);
  });
Sign up to request clarification or add additional context in comments.

7 Comments

Good answer. It helped me figure out where I need to go in my code, but it doesn't check the existing data values of each row. Can you update you answer to include this? Also, why did you use the $timeout function?
What do you mean by existing values? I understood them as the values that are in the array at the time of rendering the table.
Yes, that is exactly what I am looking for. Your example uses the index and none of the data from the model
Your data is nothing but rows. You can access each row content using rows[i].entity.name, rows[i].entity.phone etc.
it is giving me this error: cnl.ctrl.js:74 Uncaught TypeError: Cannot read property 'toggleRowExpansion' of undefined
|
0

May be I am late to answer. But it may help others.

I have taken the same plunker example as SaiGiridhar posted, but modified

$timeout(function() {
    var rows = $scope.gridApi.grid.rows;
    for(var i=0; i < rows.length; i++){
      if(i%2 === 0){
        var entity = (rows.length && rows[i]) ? rows[i].entity : null;
        $scope.gridApi.expandable.toggleRowExpansion(entity);
      }
    }
});

to

$scope.$$postDigest(function() {
    var rows = $scope.gridApi.grid.rows;
    for(var i=0; i < rows.length; i++){
      if(i%2 === 0){
        var entity = (rows.length && rows[i]) ? rows[i].entity : null;
        $scope.gridApi.expandable.toggleRowExpansion(entity);
      }
    }
});

1 Comment

What is the benifit of using $$postDigest?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.