Search code examples
angularjsdatagridangular-ui-grid

Angular ui-grid grouping showing duplicate rows


I am new to Angular and trying to group row's using Angular ui.grid.grouping. But I am seeing duplicate rows and its not grouped properly. I am using Angular js 1.7.2 version

app.js

var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.grouping' ]);

app.controller('MainCtrl', ['$scope', '$http', '$interval', 'uiGridGroupingConstants', function ($scope, $http, $interval, uiGridGroupingConstants ) {
  $scope.gridOptions = {
    enableSorting: true,
    enableGrouping:true,
    treeRowHeaderAlwaysVisible: false,
    columnDefs: [


            { name: 'City', width:'50%', grouping: { groupPriority: 0 },defaultSort: {priority: 0}},
            { name: 'CustomerName', width:'30%' }

    ],
    onRegisterApi: function( gridApi ) {
      $scope.gridApi = gridApi;
    }
  };

  $scope.gridOptions.data = [
                    {    "City": "TEXAS", "CustomerName": "AAA"},
                    {  "City": "TEXAS", "CustomerName": "BBB"},
                    { "City": "TEXAS", "CustomerName": "CCC"  },
                    { "City": "MICHIGAN", "CustomerName": "DDD" },
                    {  "City": "NEW YORK","CustomerName": "EEE"  },
                    {   "City": "MICHIGAN" ,"CustomerName": "FFF"},
                    { "City": "MICHIGAN", "CustomerName": "GGG" },
                    {  "City": "MICHIGAN", "CustomerName": "HHH"  },
                    {   "City": "NEW YORK","CustomerName": "III" }
                ];


}])

HTML:

<div ng-controller="MainCtrl">

  <div id="grid1" ui-grid="gridOptions" ui-grid-grouping class="grid"></div>
</div>

Actual Result:

enter image description here

Expected Result:

  • Instead of 2 Michigan one Michigan
  • Instead of 2 New York one New York

Tried same thing using ng-grid and I am getting perfect result as below:Exactly same thing I am trying to achieve it using angular ui-grid

enter image description here


Solution

  • @Steve, thanks for your suggestions. As per your suggestion I added grouping with delay while registeringApi as below:

     onRegisterApi: function( gridApi ) {
          $scope.gridApi = gridApi;
          $timeout(function(){
            $scope.gridApi.grouping.clearGrouping();
            $scope.gridApi.grouping.groupColumn('City');
        }, 300);
        }
    

    And it fixed it.