简体   繁体   中英

Show sum of a column in ag-grid

I am using ag-grid for my table. I need to show the sum of each column at the end of the table. After editing the cell, the sum value should be updated

Below is my html:

<div>
  <ag-grid-angular style="width: 100%; height: 500px;" class="ag-theme-alpine" [rowData]= "rowData"
    [columnDefs]= "columnDefs" >
  </ag-grid-angular>
</div>

Below is Typescript

import { Component } from '@angular/core';
import { GridRes } from './gridres.model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'ag-custom';


  columnDefs = [
    {headerName: "Make", field: "make"},
    {headerName: "Model", field: "model"},
    {headerName: "Price", field: "price"}
];

rowData = [
  {make: "Toyota", model: "Celica", price: 35000},
  {make: "Ford", model: "Mondeo", price: 32000},
  {make: "Porsche", model: "Boxter", price: 72000}
];

}

My Output is like below

在此处输入图像描述

I need to show the sum of price column at the end of the table. I gone show aggregator functions. But all are showing in the right side as grouping. But I need to show in the bottom of the table. The sum need to reflect if someone edits the cells

Please help me on this.

Here is how you can add a table footer to show the sum of a column:

  • Set[groupIncludeTotalFooter]="true" .
  • Add aggFunc property to the column you want to compute the sum value.
  • Add valueParser: 'Number(newValue)' to the columnDefs to parse the updated value as number to calculate the sum value correctly after an edit.
columnDefs = [
  { field: "make" },
  { field: "model" },
  {
    field: "price",
    aggFunc: "sum",
    editable: true,
    valueParser: "Number(newValue)",
  }
];
<ag-grid-angular style="width: 100%; height: 600px;" class="ag-theme-alpine" [rowData]="rowData"
  [columnDefs]="columnDefs" [groupIncludeTotalFooter]="true">
</ag-grid-angular>

Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM