|
58 | 58 | * will turn on any sorts that haven't run. It will then call a recursive sort on the tree.
|
59 | 59 | *
|
60 | 60 | * Tree base provides treeAggregation. It checks the treeAggregation configuration on each column, and aggregates based on
|
61 |
| - * the logic provided as it builds the tree. Aggregation information will be collected in the format: |
| 61 | + * the logic provided as it builds the tree. Footer aggregation from the uiGrid core should not be used with treeBase aggregation, |
| 62 | + * since it operates on all visible rows, as opposed to to leaf nodes only. Setting `showColumnFooter: true` will show the |
| 63 | + * treeAggregations in the column footer. Aggregation information will be collected in the format: |
62 | 64 | *
|
63 | 65 | * ```
|
64 | 66 | * {
|
|
70 | 72 | * ```
|
71 | 73 | *
|
72 | 74 | * A callback is provided to format the value once it is finalised (aka a valueFilter).
|
73 |
| - * |
| 75 | + * |
74 | 76 | * <br/>
|
75 | 77 | * <br/>
|
76 | 78 | *
|
|
1299 | 1301 | grid.columns.forEach( function(column){
|
1300 | 1302 | if ( typeof(column.treeAggregationFn) !== 'undefined' ){
|
1301 | 1303 | aggregateArray.push( service.buildAggregationObject(column) );
|
| 1304 | + |
| 1305 | + if ( grid.options.showColumnFooter && typeof(column.colDef.aggregationType) === 'undefined' && column.treeAggregation ){ |
| 1306 | + // Add aggregation object for footer |
| 1307 | + column.treeFooterAggregation = service.buildAggregationObject(column); |
| 1308 | + column.aggregationType = service.treeFooterAggregationType; |
| 1309 | + } |
1302 | 1310 | }
|
1303 | 1311 | });
|
1304 | 1312 | return aggregateArray;
|
|
1308 | 1316 | /**
|
1309 | 1317 | * @ngdoc function
|
1310 | 1318 | * @name aggregate
|
1311 |
| - * @methodOf ui.grid.grouping.service:uiGridGroupingService |
| 1319 | + * @methodOf ui.grid.treeBase.service:uiGridTreeBaseService |
1312 | 1320 | * @description Accumulate the data from this row onto the aggregations for each parent
|
1313 | 1321 | *
|
1314 | 1322 | * Iterate over the parents, then iterate over the aggregations for each of those parents,
|
|
1323 | 1331 | return;
|
1324 | 1332 | }
|
1325 | 1333 |
|
1326 |
| - parents.forEach( function( parent ){ |
| 1334 | + parents.forEach( function( parent, index ){ |
1327 | 1335 | if ( parent.treeNode.aggregations ){
|
1328 | 1336 | parent.treeNode.aggregations.forEach( function( aggregation ){
|
1329 | 1337 | var fieldValue = grid.getCellValue(row, aggregation.col);
|
1330 | 1338 | var numValue = Number(fieldValue);
|
1331 | 1339 | aggregation.col.treeAggregationFn(aggregation, fieldValue, numValue, row);
|
| 1340 | + |
| 1341 | + if ( index === 0 && typeof aggregation.col.treeFooterAggregation !== 'undefined' ){ |
| 1342 | + aggregation.col.treeAggregationFn(aggregation.col.treeFooterAggregation, fieldValue, numValue, row); |
| 1343 | + } |
1332 | 1344 | });
|
1333 | 1345 | }
|
1334 | 1346 | });
|
|
1419 | 1431 | return nativeAggregations;
|
1420 | 1432 | },
|
1421 | 1433 |
|
| 1434 | + /** |
| 1435 | + * @ngdoc function |
| 1436 | + * @name finaliseAggregation |
| 1437 | + * @methodOf ui.grid.treeBase.service:uiGridTreeBaseService |
| 1438 | + * @description Helper function used to finalize aggregation nodes and footer cells |
| 1439 | + * |
| 1440 | + * @param {gridRow} row the parent we're finalising |
| 1441 | + * @param {aggregation} the aggregation object manipulated by the aggregationFn |
| 1442 | + */ |
| 1443 | + finaliseAggregation: function(row, aggregation){ |
| 1444 | + if ( aggregation.col.treeAggregationUpdateEntity && typeof(row) !== 'undefined' && typeof(row.entity[ '$$' + aggregation.col.uid ]) !== 'undefined' ){ |
| 1445 | + angular.extend( aggregation, row.entity[ '$$' + aggregation.col.uid ]); |
| 1446 | + } |
| 1447 | + |
| 1448 | + if ( typeof(aggregation.col.treeAggregationFinalizerFn) === 'function' ){ |
| 1449 | + aggregation.col.treeAggregationFinalizerFn( aggregation ); |
| 1450 | + } |
| 1451 | + if ( typeof(aggregation.col.customTreeAggregationFinalizerFn) === 'function' ){ |
| 1452 | + aggregation.col.customTreeAggregationFinalizerFn( aggregation ); |
| 1453 | + } |
| 1454 | + if ( typeof(aggregation.rendered) === 'undefined' ){ |
| 1455 | + aggregation.rendered = aggregation.label ? aggregation.label + aggregation.value : aggregation.value; |
| 1456 | + } |
| 1457 | + }, |
| 1458 | + |
1422 | 1459 | /**
|
1423 | 1460 | * @ngdoc function
|
1424 | 1461 | * @name finaliseAggregations
|
1425 |
| - * @methodOf ui.grid.grouping.service:uiGridGroupingService |
| 1462 | + * @methodOf ui.grid.treeBase.service:uiGridTreeBaseService |
1426 | 1463 | * @description Format the data from the aggregation into the rendered text
|
1427 | 1464 | * e.g. if we had label: 'sum: ' and value: 25, we'd create 'sum: 25'.
|
1428 | 1465 | *
|
|
1443 | 1480 | }
|
1444 | 1481 |
|
1445 | 1482 | row.treeNode.aggregations.forEach( function( aggregation ) {
|
1446 |
| - if ( aggregation.col.treeAggregationUpdateEntity && typeof(row.entity[ '$$' + aggregation.col.uid ]) !== 'undefined' ){ |
1447 |
| - angular.extend( aggregation, row.entity[ '$$' + aggregation.col.uid ]); |
1448 |
| - } |
1449 |
| - |
1450 |
| - if ( typeof(aggregation.col.treeAggregationFinalizerFn) === 'function' ){ |
1451 |
| - aggregation.col.treeAggregationFinalizerFn( aggregation ); |
1452 |
| - } |
1453 |
| - if ( typeof(aggregation.col.customTreeAggregationFinalizerFn) === 'function' ){ |
1454 |
| - aggregation.col.customTreeAggregationFinalizerFn( aggregation ); |
1455 |
| - } |
1456 |
| - if ( typeof(aggregation.rendered) === 'undefined' ){ |
1457 |
| - aggregation.rendered = aggregation.label ? aggregation.label + aggregation.value : aggregation.value; |
1458 |
| - } |
| 1483 | + service.finaliseAggregation(row, aggregation); |
1459 | 1484 |
|
1460 | 1485 | if ( aggregation.col.treeAggregationUpdateEntity ){
|
1461 | 1486 | var aggregationCopy = {};
|
|
1468 | 1493 | row.entity[ '$$' + aggregation.col.uid ] = aggregationCopy;
|
1469 | 1494 | }
|
1470 | 1495 | });
|
| 1496 | + }, |
| 1497 | + |
| 1498 | + /** |
| 1499 | + * @ngdoc function |
| 1500 | + * @name treeFooterAggregationType |
| 1501 | + * @methodOf ui.grid.treeBase.service:uiGridTreeBaseService |
| 1502 | + * @description Uses the tree aggregation functions and finalizers to set the |
| 1503 | + * column footer aggregations. |
| 1504 | + * |
| 1505 | + * @param {rows} visible rows. not used, but accepted to match signature of GridColumn.aggregationType |
| 1506 | + * @param {gridColumn} the column we are finalizing |
| 1507 | + */ |
| 1508 | + treeFooterAggregationType: function( rows, column ) { |
| 1509 | + service.finaliseAggregation(undefined, column.treeFooterAggregation); |
| 1510 | + return column.treeFooterAggregation.rendered; |
1471 | 1511 | }
|
1472 | 1512 | };
|
1473 | 1513 |
|
|
0 commit comments