Skip to content

Commit

Permalink
Fix(grouping) Change group comparison to use sortingAlgorithm
Browse files Browse the repository at this point in the history
Change the grouping processor to use the sortingAlgorithm for each column,
instead of strict equality for determining group membership.

Change implementation of aggregationFinalizerFn on grouped rows to support
customTreeAggregationFinalizerFn on grouped columns.

Never show counts for date columns, since they will almost always have a
cellFilter applied and changing the data type to a string will break that.

Fixes #3629
  • Loading branch information
AgDude committed Jun 1, 2015
1 parent 814585a commit e45f2e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
23 changes: 13 additions & 10 deletions misc/tutorial/209_grouping.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ function will stop working), and writes them to the console.
{ name: 'gender', grouping: { groupPriority: 1 }, sort: { priority: 1, direction: 'asc' }, width: '20%', cellFilter: 'mapGender' },
{ name: 'age', treeAggregationType: uiGridGroupingConstants.aggregation.MAX, width: '20%' },
{ name: 'company', width: '25%' },
{ name: 'registered', width: '40%', cellFilter: 'date', type: 'date' },
{ name: 'state', grouping: { groupPriority: 0 }, sort: { priority: 0, direction: 'desc' }, width: '35%', cellTemplate: '<div><div ng-if="!col.grouping || col.grouping.groupPriority === undefined || col.grouping.groupPriority === null || ( row.groupHeader && col.grouping.groupPriority === row.treeLevel )" class="ui-grid-cell-contents" title="TOOLTIP">{{COL_FIELD CUSTOM_FILTERS}}</div></div>' },
{ name: 'balance', width: '25%', cellFilter: 'currency', treeAggregationType: uiGridGroupingConstants.aggregation.AVG, customTreeAggregationFinalizerFn: function( aggregation ) {
aggregation.rendered = aggregation.value;
Expand All @@ -116,16 +117,18 @@ function will stop working), and writes them to the console.
}
};

$http.get('/data/500_complex.json')
.success(function(data) {
for ( var i = 0; i < data.length; i++ ){
data[i].state = data[i].address.state;
data[i].gender = data[i].gender === 'male' ? 1: 2;
data[i].balance = Number( data[i].balance.slice(1).replace(/,/,'') );
}
delete data[2].age;
$scope.gridOptions.data = data;
});
$http.get('/data/500_complex.json')
.success(function(data) {
for ( var i = 0; i < data.length; i++ ){
var registeredDate = new Date( data[i].registered );
data[i].state = data[i].address.state;
data[i].gender = data[i].gender === 'male' ? 1: 2;
data[i].balance = Number( data[i].balance.slice(1).replace(/,/,'') );
data[i].registered = new Date( registeredDate.getFullYear(), registeredDate.getMonth(), 1 )
}
delete data[2].age;
$scope.gridOptions.data = data;
});

$scope.expandAll = function(){
$scope.gridApi.treeBase.expandAllRows();
Expand Down
61 changes: 36 additions & 25 deletions src/features/grouping/js/grouping.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
*
* @description Services for grouping features
*/
module.service('uiGridGroupingService', ['$q', 'uiGridGroupingConstants', 'gridUtil', 'GridRow', 'gridClassFactory', 'i18nService', 'uiGridConstants', 'uiGridTreeBaseService',
function ($q, uiGridGroupingConstants, gridUtil, GridRow, gridClassFactory, i18nService, uiGridConstants, uiGridTreeBaseService) {
module.service('uiGridGroupingService', ['$q', 'uiGridGroupingConstants', 'gridUtil', 'rowSorter', 'GridRow', 'gridClassFactory', 'i18nService', 'uiGridConstants', 'uiGridTreeBaseService',
function ($q, uiGridGroupingConstants, gridUtil, rowSorter, GridRow, gridClassFactory, i18nService, uiGridConstants, uiGridTreeBaseService) {

var service = {

Expand Down Expand Up @@ -321,8 +321,10 @@
* @ngdoc object
* @name groupingShowCounts
* @propertyOf ui.grid.grouping.api:GridOptions
* @description shows counts on the groupHeader rows
* <br/>Defaults to true
* @description shows counts on the groupHeader rows. Not that if you are using a cellFilter or a
* sortingAlgorithm which relies on a specific format or data type, showing counts may cause that
* to break, since the group header rows will always be a string with groupingShowCounts enabled.
* <br/>Defaults to true except on columns of type 'date'
*/
gridOptions.groupingShowCounts = gridOptions.groupingShowCounts !== false;

Expand Down Expand Up @@ -408,16 +410,7 @@
col.grouping = angular.copy(colDef.grouping);
if ( typeof(col.grouping.groupPriority) !== 'undefined' && col.grouping.groupPriority > -1 ){
col.treeAggregationFn = uiGridTreeBaseService.nativeAggregations[uiGridGroupingConstants.aggregation.COUNT].aggregationFn;
col.customTreeAggregationFinalizerFn = function( aggregation ){
if ( typeof(aggregation.groupVal) !== 'undefined') {
aggregation.rendered = aggregation.groupVal;
if ( gridOptions.groupingShowCounts ){
aggregation.rendered += (' (' + aggregation.value + ')');
}
} else {
aggregation.rendered = null;
}
};
col.treeAggregationFinalizerFn = service.groupedFinalizerFn;
}
} else if (typeof(col.grouping) === 'undefined'){
col.grouping = {};
Expand Down Expand Up @@ -527,6 +520,8 @@
},




/**
* @ngdoc function
* @name groupingColumnProcessor
Expand All @@ -544,6 +539,27 @@
return columns;
},

/**
* @ngdoc function
* @name groupedFinalizerFn
* @methodOf ui.grid.grouping.service:uiGridGroupingService
* @description Used on group columns to display the rendered value and optionally
* display the count of rows.
*
* @param {aggregation} the aggregation entity for a grouped column
*/
groupedFinalizerFn: function( aggregation ){
var col = this;

if ( typeof(aggregation.groupVal) !== 'undefined') {
aggregation.rendered = aggregation.groupVal;
if ( col.grid.options.groupingShowCounts && col.colDef.type !== 'date' ){
aggregation.rendered += (' (' + aggregation.value + ')');
}
} else {
aggregation.rendered = null;
}
},

/**
* @ngdoc function
Expand Down Expand Up @@ -634,13 +650,7 @@

column.treeAggregation = { type: uiGridGroupingConstants.aggregation.COUNT, source: 'grouping' };
column.treeAggregationFn = uiGridTreeBaseService.nativeAggregations[uiGridGroupingConstants.aggregation.COUNT].aggregationFn;
column.customTreeAggregationFinalizerFn = function( aggregation ){
if ( typeof(aggregation.groupVal) !== 'undefined') {
aggregation.rendered = aggregation.groupVal + ' (' + aggregation.value + ')';
} else {
aggregation.rendered = null;
}
};
column.treeAggregationFinalizerFn = service.groupedFinalizerFn;

grid.queueGridRefresh();
},
Expand Down Expand Up @@ -839,11 +849,12 @@
* @description The rowProcessor that creates the groupHeaders (i.e. does
* the actual grouping).
*
* Assumes it is always called after the sorting processor, guaranteed by teh priority setting
* Assumes it is always called after the sorting processor, guaranteed by the priority setting
*
* Processes all the rows in order, inserting a groupHeader row whenever there is a change
* in value of a grouped row. The group header row is looked up in the groupHeaderCache, and used
* from there if there is one. The entity is reset to {} if one is found.
* in value of a grouped row, based on the sortAlgorithm used for the column. The group header row
* is looked up in the groupHeaderCache, and used from there if there is one. The entity is reset
* to {} if one is found.
*
* As it processes it maintains a `processingState` array. This records, for each level of grouping we're
* working with, the following information:
Expand Down Expand Up @@ -883,7 +894,7 @@
}

// look for change of value - and insert a header
if ( !groupFieldState.initialised || fieldValue !== groupFieldState.currentValue ){
if ( !groupFieldState.initialised || rowSorter.getSortFn(grid, groupFieldState.col, renderableRows)(fieldValue, groupFieldState.currentValue) !== 0 ){
service.insertGroupHeader( grid, renderableRows, i, processingState, stateIndex );
i++;
}
Expand Down

0 comments on commit e45f2e2

Please sign in to comment.