ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Add Grouping module

Open swalters opened this issue 12 years ago • 13 comments

Add grouping module using grouping concepts from 2.x

swalters avatar Jan 22 '14 14:01 swalters

Possibly include group tristate checkbox to aggregate template which selects/deselects all children (using intermediate state when some of the children are unselected).

NewMediaRoc avatar Mar 07 '14 18:03 NewMediaRoc

Does this mean grouping is currently not supported in 3.0? I'm working to upgrade to fix a few issues I was having in v2 with getting the grid to load data when it was hidden during the initial load via ng-show.

andythecoderman avatar Oct 01 '14 15:10 andythecoderman

Correct, grouping is currently not supported. But expandable rows is, which is very cool. :-)

PaulL1 avatar Oct 01 '14 20:10 PaulL1

is grouping going to be part of ui-grid, as i would like to upgrade from V2 but still need grouping

meirb avatar Oct 22 '14 14:10 meirb

Yes, our intent is to have grouping added, hence this open item in the issue tracker. We've had a few people offer to assist in building it, but so far it hasn't turned into a pull request, and the core team are quite busy with the tasks they already have underway.

I'd say it will come, given the interest, but probably not in the next 1 month.

PaulL1 avatar Oct 23 '14 00:10 PaulL1

I'm thinking about trying to implement this. Having some trouble getting started. So far, I'm thinking about renderContainers, but really can't grok some of the concepts after sitting down with it for a few hours. If someone could throw some bones, that would be awesome.

EDIT: Seems that renderContainers are coupled (meant for) the pinning feature. Perhaps they, too, are just another rabbit hole.

EDIT: Perhaps, rowBuilder will do the trick.

jarrodpayne avatar Dec 17 '14 17:12 jarrodpayne

I'd suggest having a chat on the gitter room - that's likely to get a good response. https://gitter.im/angular-ui/ng-grid

PaulL1 avatar Dec 17 '14 18:12 PaulL1

It's been a few months since there has been any chatter on this thread. How is this feature coming along? Really looking forward to using this grid in my project, so far it is amazing!

ACDN avatar Feb 12 '15 17:02 ACDN

Here's some fairly awful, throw-away code that pulls it off :)

Keep in mind, this depends on underscore / lodash, and I don't know how well it plays with many other ui-grid features.

grouping.js

(function() {
  'use strict';

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

  module.constant('uiGridGroupingConstants', {
    featureName: 'grouping'
  });

  module.service('uiGridGroupingService', ['$templateCache', 'uiGridGroupingConstants',
    function($templateCache, uiGridGroupingConstants){

      var service = {

        initializeGrid: function (grid) {
          //add feature namespace and any properties to grid for needed state
          grid.grouping = {};

          service.defaultGridOptions(grid.options);
        },

        defaultGridOptions: function(gridOptions) {
          gridOptions.groupingRowHeight = gridOptions.groupingRowHeight || 30;
          gridOptions.groupingRowHeaderWidth = gridOptions.groupingRowHeaderWidth || 40;
        }

      };

      return service;

    }]);

  module.directive('uiGridGrouping', ['$timeout', 'uiGridGroupingConstants', 'uiGridGroupingService', 'gridUtil', 'GridRow', 'GridColumn', 'rowSorter',
    function($timeout, uiGridGroupingConstants, uiGridGroupingService, gridUtil, GridRow, GridColumn, rowSorter) {
      return {
        replace: false,
        priority: -1,
        require: '^uiGrid',
        scope: false,
        compile: function() {
          return {
            pre: preLink,
            post: postLink
          };
        }
      };

      function preLink($scope, $elm, $attrs, uiGridCtrl) {
        var grid = uiGridCtrl.grid;
        var columnDefs = grid.options.columnDefs;
        var groupingColumns = _.filter(columnDefs, function(colDef) {
          return colDef.grouping;
        });

        if (!groupingColumns.length) return;

        grid.groupingColumns = groupingColumns;

        uiGridGroupingService.initializeGrid(grid);

        _.each(createGroupColDefs(), function(col, index) {
          columnDefs.splice(0, 0, col);
        });

        function createGroupColDefs() {
          var defs = [];

          _.eachRight(groupingColumns, function(col, index) {
            defs.push({
              name: col.name + ' Group',
              displayName: '',
              enableSorting: false,
              enableColumnResizing: false,
              width: 40,
              field: col.name + 'Group',
              allowCellFocus: false
            });
          });

          return defs;
        }
      }

      function postLink($scope, $elm, $attrs, uiGridCtrl) {
        var grid = uiGridCtrl.grid;
        var columnDefs = grid.options.columnDefs;
        var groupingColumns = grid.groupingColumns;

        grid.registerRowsProcessor(rowsProcessor);

        $timeout(function() {
          uiGridCtrl.grid.handleWindowResize();
        }, 0);

        function rowsProcessor(rows, columns) {
          _.each(groupingColumns, addSortPropertiesToColDef);
          rowSorter.sort(grid, rows, columns);
          _.each(groupingColumns, addGroupHeaderRows);

          return rows;

          function addGroupHeaderRows(groupCol, index) {
            var lastVal = null;

            for (var i = 0; i <= rows.length - 1; i++) {
              var row = rows[i];

              if (row.entity[groupCol.field] !== lastVal && row.entity[groupCol.field] !== undefined && !row.groupHeader) {
                var currentRowCopy = angular.copy(row.entity);
                var gridRow = new GridRow(currentRowCopy, i + 1, grid);
                gridRow.entity[groupCol.name+'Group'] = row.entity[groupCol.field];
                gridRow.groupHeader = true;
                gridRow.groupLevel = index;
                gridRow.allowCellFocus = false;
                rows.splice(i, 0, gridRow);

                i++;
                lastVal = row.entity[groupCol.field];
              }
            }
          }

          function addSortPropertiesToColDef(col) {
            var matchedCol = _.find(columns, function(mCol) {
              return col.name === mCol.name;
            });

            _.assign(matchedCol.sort, {
              direction: col.grouping.direction,
              priority: col.grouping.priority
            });
          }
        }
      }
    }]);

})();

Example Grid Options

var gridOptions = {
  data: 'someData',
  columnDefs: [
    {  name: 'groupableYear', visible: false, grouping: { priority: -100, direction: 'desc' } },
    {  name: 'groupableNumber', visible: false, grouping: { priority: -99, direction: 'desc' } }, 
    {  name: 'name' },
    {  name: 'date' }
  ]
};

The above example, gives you two levels of grouping. I really don't feel good about the code, but, this is what I've got. Hopefully, it will give someone some starting point for a rewrite.

I very quickly threw this response together. If you have issues, make a plunk, or something, and I'll see if I can help.

jarrodpayne avatar Feb 12 '15 17:02 jarrodpayne

Dang... didn't expect that. I am not in a rush situation, will definitely try this code out if the feature doesn't get into the grid in the next month or so.

ACDN avatar Feb 12 '15 21:02 ACDN

#2503 also offered an interesting solution to this, which was a different way to get to the outcome.

PaulL1 avatar Feb 12 '15 22:02 PaulL1

Ya detail grids are cool n all but they don't look as clean as the groups do.

ACDN avatar Feb 13 '15 07:02 ACDN

I've pushed today, PR #2935. This is alpha only, so the API is be no means stable, refer the new tutorial for known limitations.

I'm closing this master issue for grouping, and we can raise new issues as new issue requests.

PaulL1 avatar Mar 07 '15 00:03 PaulL1