Search code examples
angularjstypescriptangular-ui-grid

angular ui grid grouping display first element in the group row


I need to display the first value in a group in the group row so first i tried to display a constant using aggregation, I have this column def

     name: 'Stack',
    displayName: 'Stack',
    grouping: { groupPriority: 1 },
    treeAggregation: { type:this.uiGridGroupingConstants.aggregation.CUSTOM },
    customTreeAggregationFn: this.aggregateService.accumulateNumValue,
    customTreeAggregationFinalizerFn: this.aggregateService.medianFinalize,
    visible: true,
    minWidth: '70',

Grouping is done using another column which is hidden, I tried to using custom aggregationTree with finalize method doing only

        if ( angular.isUndefined(aggregation.stats.accumulator) ){
            aggregation.stats.accumulator = [];
        }

this piece of code throws an error

 TypeError: Cannot read property 'accumulator' of undefined
at GridColumn.AggregationService.medianFinalize [as customTreeAggregationFinalizerFn] (http://localhost:8080/bundle.js:135388:55)
at Object.finaliseAggregation (http://localhost:8080/bundle.js:74042:28)
at http://localhost:8080/bundle.js:74073:20
at Array.forEach (native)
at Object.finaliseAggregations (http://localhost:8080/bundle.js:74072:36)
at createNode (http://localhost:8080/bundle.js:73647:24)
at Array.forEach (native)
at Object.createTree (http://localhost:8080/bundle.js:73679:25)
at Grid.treeRows (http://localhost:8080/bundle.js:73544:39)
at startProcessor (http://localhost:8080/bundle.js:52026:34)

What I do not understand how this throws an error, I removed the 3 lines of code and the error no longer exists but i clearly need to access the tats.accumulator as this is the array where i push my data in the aggregation function.


Solution

  • I was able to solve the issue the problem was with the aggregation function not the finalizer function, in my aggregation service I had this function

     public accumulateNumValue (aggregation, fieldValue, value) {
    
            if (angular.isUndefined(aggregation.stats) ){
                aggregation.stats = {sum: 0};
            }
            if ( angular.isUndefined(aggregation.stats.accumulator) ){
                aggregation.stats.accumulator = [];
            }
    
            aggregation.stats.accumulator.push(value);
    
        }
    

    I was pushing the value not fieldValue, i just started pushing the field value and the accumulate function is working perfectly now.

         public accumulateNumValue (aggregation, fieldValue, value) {
    
            if (angular.isUndefined(aggregation.stats) ){
                aggregation.stats = {sum: 0};
            }
            if ( angular.isUndefined(aggregation.stats.accumulator) ){
                aggregation.stats.accumulator = [];
            }
    
            aggregation.stats.accumulator.push(fieldValue);
    
        }
    

    and to display the first element in the group all you need in your finalizer function is to assign "aggregation.rendered" to the first element of the array

         public medianFinalize(aggregation) {
            if (angular.isUndefined(aggregation.stats) ) {
                aggregation.stats={accumulator : []};
            }
            if(aggregation.stats.accumulator.length >0) {
    
                aggregation.rendered = aggregation.stats.accumulator[0];
            }
        }