Question

I am having the exact same problem as Setting the group option dynamically not working in ng-grid 2.0.7.

The short of it (newbie here):

I have successfully managed to work out how to create an aggregation row template, load from the server, and specify groups (hard-coded in gridOptions) to group the data. It works beautifully.

My groupings, however, are dynamic, and are also available from the server. When i try and dynamically retrieve them and set them in gridOptions, they are ignored. If they are hardcoded, they work, and if they are retrieved and populated I am presented with just the ungrouped data.

I have tried many things, and have spent more than a day on this. It seems to me that something is up with the initialization of the several internal places where state related to the groups are held (config.groups, config.columnDefs, configGroups, etc are null when i would expect them not to be.

Has anyone else come across this problem?

Thanks.

Chris

Was it helpful?

Solution

The grid object has a groupBy property which you can use to aggregate your data dynamically. Example:

html:

<body ng-controller="MyCtrl">
<button type="button" ng-click="changeGroupBy('name')">Group By Name</button>
<button type="button" ng-click="changeGroupBy('age')">Group By Age</button>
<div class="gridStyle"  ng-grid="gridOptions"></div>
</body>

js:

app.controller('MyCtrl', function($scope) {

    $scope.myData = [{"name": "Moroni", "age": 50},
              {"name": "Tiancum", "age": 53},
              {"name": "Jacob", "age": 27},
              {"name": "Nephi", "age": 54},
              {"name": "Alex", "age": 53},
              {"name": "Jhonny", "age": 22},
              {"name": "Ben", "age": 11},
              ],

    $scope.gridOptions = { 
        data: 'myData'
        };

    $scope.changeGroupBy = function (group) {
     $scope.gridOptions.groupBy(group);
   }
});

Live example: http://plnkr.co/edit/A0q3c98s8K4h3n83MULE?p=preview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top