Search results

Footer Aggregate in React TreeGrid component

25 Jan 2023 / 2 minutes to read

Footer aggregate value is calculated for all the rows, and it is displayed in the footer cells. Use the footerTemplate property to render the aggregate value in footer cells.

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { getObject } from '@syncfusion/ej2-grids';
import { Aggregate, AggregatesDirective, ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { AggregateColumnDirective, AggregateColumnsDirective, AggregateDirective, Inject } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { summaryRowData } from './datasource';
function App() {
    const footerSum = (props) => {
        return (<span>Minimum: {getObject('Min', props)}</span>);
    };
    const footerSum2 = (props) => {
        return (<span>Maximum: {getObject('Max', props)}</span>);
    };
    return <TreeGridComponent dataSource={summaryRowData} treeColumnIndex={0} childMapping='children' height='260'>
        <ColumnsDirective>
            <ColumnDirective field='FreightID' headerText='Freight ID' width='130'/>
            <ColumnDirective field='FreightName' headerText='Freight Name' width='180'/>
            <ColumnDirective field='UnitWeight' headerText='Weight Per Unit' width='130' textAlign='Right' type='number'/>
            <ColumnDirective field='TotalUnits' headerText='Total Units' type='number' width='125' textAlign='Right'/>
        </ColumnsDirective>
            <AggregatesDirective>
            <AggregateDirective showChildSummary={false}>
                <AggregateColumnsDirective>
                <AggregateColumnDirective field='TotalUnits' columnName='TotalUnits' type='Min' footerTemplate={footerSum}/>
                <AggregateColumnDirective field='UnitWeight' columnName='UnitWeight' type='Max' footerTemplate={footerSum2}/>
                </AggregateColumnsDirective>
                </AggregateDirective>
        </AggregatesDirective>
        <Inject services={[Aggregate]}/>
    </TreeGridComponent>;
}
;
export default App;
Copied to clipboard
import { getObject } from '@syncfusion/ej2-grids';
import { Aggregate, AggregatesDirective, ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { AggregateColumnDirective, AggregateColumnsDirective, AggregateDirective, Inject } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { summaryRowData } from './datasource';

function App() {
    const footerSum = (props: object) => {
        return(<span>Minimum: {getObject('Min', props)}</span>)
    }

    const footerSum2 = (props: object) => {
        return(<span>Maximum: {getObject('Max', props)}</span>)
    }

    return <TreeGridComponent dataSource={summaryRowData} treeColumnIndex={0} childMapping='children' height='260'>
        <ColumnsDirective>
            <ColumnDirective field='FreightID' headerText='Freight ID' width='130'/>
            <ColumnDirective field='FreightName' headerText='Freight Name' width='180'/>
            <ColumnDirective field='UnitWeight' headerText='Weight Per Unit' width='130' textAlign='Right' type='number'/>
            <ColumnDirective field='TotalUnits' headerText='Total Units' type='number' width='125' textAlign='Right'/>
        </ColumnsDirective>
            <AggregatesDirective>
            <AggregateDirective showChildSummary={false}>
                <AggregateColumnsDirective>
                <AggregateColumnDirective field='TotalUnits' columnName='TotalUnits' type='Min'
                    footerTemplate={footerSum}/>
                <AggregateColumnDirective field='UnitWeight' columnName='UnitWeight' type='Max'
                    footerTemplate={footerSum2}/>
                </AggregateColumnsDirective>
                </AggregateDirective>
        </AggregatesDirective>
        <Inject services={[Aggregate]}/>
    </TreeGridComponent>
};
export default App;

The aggregate values must be accessed inside the template using their corresponding type name.

How to format aggregate value

You can format the aggregate value result by using the format property.

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { getObject } from '@syncfusion/ej2-grids';
import { Aggregate, AggregatesDirective, ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { AggregateColumnDirective, AggregateColumnsDirective, AggregateDirective, Inject } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { summaryData } from './datasource';
function App() {
    const footerSum = (props) => {
        return (<span>Total: {getObject('Sum', props)}</span>);
    };
    return <TreeGridComponent dataSource={summaryData} treeColumnIndex={0} childMapping='subtasks' height='260'>
          <ColumnsDirective>
              <ColumnDirective field='category' headerText='Category' width='160'/>
              <ColumnDirective field='units' headerText='Total Units' width='130' type='number' textAlign='Right'/>
              <ColumnDirective field='unitPrice' headerText='Unit Price($)' width='110' textAlign='Right' type='number' format='C2'/>
              <ColumnDirective field='price' headerText='Price($)' type='number' width='160' textAlign='Right' format='C2'/>
          </ColumnsDirective>
              <AggregatesDirective>
              <AggregateDirective showChildSummary={true}>
                  <AggregateColumnsDirective>
                  <AggregateColumnDirective field='price' columnName='price' type='Sum' footerTemplate={footerSum} format='C2'/>
                  </AggregateColumnsDirective>
                  </AggregateDirective>
          </AggregatesDirective>
          <Inject services={[Aggregate]}/>
      </TreeGridComponent>;
}
;
export default App;
Copied to clipboard
import { getObject } from '@syncfusion/ej2-grids';
import { Aggregate, AggregatesDirective, ColumnDirective, ColumnsDirective, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import { AggregateColumnDirective, AggregateColumnsDirective, AggregateDirective, Inject } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { summaryData } from './datasource';

function App() {
    const footerSum = (props: object) => {
        return(<span>Total: {getObject('Sum', props)}</span>)
      }

      return <TreeGridComponent dataSource={summaryData} treeColumnIndex={0} childMapping='subtasks' height='260'>
          <ColumnsDirective>
              <ColumnDirective field='category' headerText='Category' width='160'/>
              <ColumnDirective field='units' headerText='Total Units' width='130' type='number' textAlign='Right'/>
              <ColumnDirective field='unitPrice' headerText='Unit Price($)' width='110' textAlign='Right' type='number' format='C2' />
              <ColumnDirective field='price' headerText='Price($)' type='number' width='160' textAlign='Right' format='C2' />
          </ColumnsDirective>
              <AggregatesDirective>
              <AggregateDirective showChildSummary={true}>
                  <AggregateColumnsDirective>
                  <AggregateColumnDirective field='price' columnName='price' type='Sum'
                      footerTemplate={footerSum} format='C2'/>
                  </AggregateColumnsDirective>
                  </AggregateDirective>
          </AggregatesDirective>
          <Inject services={[Aggregate]}/>
      </TreeGridComponent>
};
export default App;