angular ui 网格分组显示组行中的第一个元素

angular ui grid grouping display first element in the group row

我需要在组行中显示组中的第一个值,所以首先我尝试使用聚合显示常量,我有这个列 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',

分组是使用另一个隐藏的列完成的,我尝试使用自定义 aggregationTree 和 finalize 方法只做

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

这段代码抛出错误

 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)

我不明白这是怎么引发错误的,我删除了 3 行代码,错误不再存在,但我显然需要访问 tats.accumulator,因为这是我推送我的数组聚合函数中的数据。

你试过了吗:

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

我能够解决问题是聚合函数而不是终结器函数,在我的聚合服务中我有这个函数

 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);

    }

我推的是值而不是 fieldValue,我刚开始推字段值,accumulate 函数现在运行良好。

     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);

    }

要显示组中的第一个元素,您在终结函数中所需要做的就是将 "aggregation.rendered" 分配给数组的第一个元素

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

            aggregation.rendered = aggregation.stats.accumulator[0];
        }
    }