【问题标题】:Show sum of a column in ag-grid在 ag-grid 中显示列的总和
【发布时间】:2021-07-21 19:22:06
【问题描述】:

我正在为我的表使用 ag-grid。我需要在表格末尾显示每列的总和。 编辑单元格后,总和值应更新

下面是我的html:

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

下面是打字稿

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

}

我的输出如下所示

我需要在表格末尾显示价格总和列。 我去了显示聚合器功能。但所有都显示在右侧作为分组。 但我需要显示在表格的底部。总和需要反映是否有人编辑单元格

请帮帮我。

【问题讨论】:

    标签: javascript angular ag-grid ag-grid-angular


    【解决方案1】:

    以下是添加表格页脚以显示列总和的方法:

    • 设置[groupIncludeTotalFooter]="true"
    • aggFunc 属性添加到要计算总和值的列中。
    • valueParser: 'Number(newValue)' 添加到columnDefs 以将更新的值解析为数字,以便在编辑后正确计算总和值。
    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

    【讨论】:

    • 嗨@nearhuscarl,感谢您的帮助,但上面的代码在编辑单元格时不起作用
    • @RajeshKumar 您还需要添加valueParser 以将更新的值解析为数字。查看我的更新答案
    • 感谢您的帮助。如何在 Total Amount 左侧添加标题?比如“总金额”
    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2019-10-04
    • 2018-01-16
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    相关资源
    最近更新 更多