0

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.

2 Answers 2

1

Did u try:

    if ( angular.isUndefined(aggregation.stats) ){ 
aggregation.stats={accumulator : []};
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes I tried it and the error is gone , but i dont understand why, in the aggregationFn i defined it , do you know the sequence of the execution?
0

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];
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.