Aggregates in Vue Treegrid component

11 Jun 20248 minutes to read

Aggregate values are displayed in the TreeGrid footer and in parent row footer for child row aggregate values. It can be configured through aggregates property. field and type are the minimum properties required to represent an aggregate column.

To use the aggregate feature, you have to inject the Aggregate module.

By default, the aggregate value can be displayed in the treegrid footer, and footer of child rows. To show the aggregate value in one of the cells, use the footerTemplate.

To get start quickly with aggregates in Vue tree grid component, you can check on this video:

Built-in aggregate types

The aggregate type should be specified in the type property to configure an aggregate column.

The built-in aggregates are,

  • Sum
  • Average
  • Min
  • Max
  • Count
  • Truecount
  • Falsecount
  • Multiple aggregates can be used for an aggregate column by setting the type property
    with an array of aggregate types.
  • Multiple types for a column is supported only when one of the aggregate templates is used.

Child aggregate

Aggregate value is calculated for child rows, and it is displayed in the parent row footer. Use the childSummary property to render the child rows aggregate value.

<template>
<div id="app">
        <ejs-treegrid :dataSource="data" childMapping='children' :treeColumnIndex='1' height='260px'>
            <e-columns>
                <e-column field='FreightID' headerText='Freight ID' width=90 textAlign='Right'></e-column>
                <e-column field='FreightName' headerText='Freight Name' width=180></e-column>
                <e-column field='UnitWeight' headerText='Unit Per Weight' width=90 type='number'textAlign='Right'></e-column>
                <e-column field='TotalUnits' headerText='Total Units' type='number' width=80 textAlign='Right'></e-column>
            </e-columns>
              <e-aggregates>
                <e-aggregate :showChildSummary='true'>
                    <e-columns>
                        <e-column type="Max" field="UnitWeight" :footerTemplate='footerMax'></e-column>
                        <e-column type="Min" field="TotalUnits" :footerTemplate='footerMin'></e-column>
                    </e-columns>
                </e-aggregate>
          </e-aggregates>
        </ejs-treegrid>
</div>
</template>
<script setup>
import { provide } from "vue";

import { TreeGridComponent as EjsTreegrid, Aggregate, ColumnDirective as EColumn, ColumnsDirective as EColumns,
AggregatesDirective as EAggregates, AggregateDirective as EAggregate
} from "@syncfusion/ej2-vue-treegrid";
import { summaryRowData } from "./datasource.js";
import { createApp } from 'vue';

const app = createApp({});

const minimumTemplate = app.component('minTemplate', {
    template: `<span>Min: </span>`,
    data () {return { data: {}};}
});

const maximumTemplate = app.component('maxTemplate', {
    template: `<span>Max: </span>`,
    data () {return { data: {}};}
});

const data = summaryRowData;

const footerMin = function () {
    return  { template : minimumTemplate}
};

const footerMax = function () {
    return  { template : maximumTemplate}
};

provide('treegrid',  [Aggregate]);

</script>
<template>
<div id="app">
        <ejs-treegrid :dataSource="data" childMapping='children' :treeColumnIndex='1' height='260px'>
            <e-columns>
                <e-column field='FreightID' headerText='Freight ID' width=90 textAlign='Right'></e-column>
                <e-column field='FreightName' headerText='Freight Name' width=180></e-column>
                <e-column field='UnitWeight' headerText='Unit Per Weight' width=90 type='number'textAlign='Right'></e-column>
                <e-column field='TotalUnits' headerText='Total Units' type='number' width=80 textAlign='Right'></e-column>
            </e-columns>
              <e-aggregates>
                <e-aggregate :showChildSummary='true'>
                    <e-columns>
                        <e-column type="Max" field="UnitWeight" :footerTemplate='footerMax'></e-column>
                        <e-column type="Min" field="TotalUnits" :footerTemplate='footerMin'></e-column>
                    </e-columns>
                </e-aggregate>
          </e-aggregates>
        </ejs-treegrid>
</div>
</template>
<script>

import { TreeGridComponent, Aggregate, ColumnDirective, ColumnsDirective, AggregatesDirective, AggregateDirective } from "@syncfusion/ej2-vue-treegrid";
import { summaryRowData } from "./datasource.js";
import { createApp } from 'vue';

const app = createApp({});

const minimumTemplate = app.component('minTemplate', {
    template: `<span>Min: </span>`,
    data () {return { data: {}};}
});

const maximumTemplate = app.component('maxTemplate', {
    template: `<span>Max: </span>`,
    data () {return { data: {}};}
});



export default {
name: "App",
components: {
"ejs-treegrid":TreeGridComponent,
"e-columns":ColumnsDirective,
"e-column":ColumnDirective,
"e-aggregates":AggregatesDirective,
"e-aggregate":AggregateDirective,

},

  data() {
    return {
      data: summaryRowData,
      footerMin: function () {
        return  { template : minimumTemplate}
      },
      footerMax: function () {
        return  { template : maximumTemplate}
      }
    }
  },
    provide: {
      treegrid: [Aggregate]
  }
}
</script>