javascript - 在 ag-grid 中显示列的总和
问题描述
我正在为我的桌子使用 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}
];
}
我的输出如下
我需要在表格末尾显示价格列的总和。我去了显示聚合器功能。但所有都显示在右侧作为分组。但我需要显示在表格的底部。总和需要反映是否有人编辑单元格
请帮助我。
解决方案
以下是添加表格页脚以显示列总和的方法:
- 设置
[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>
现场演示
推荐阅读
- flutter - 实时 OCR 颤振
- unit-testing - 如何使用 Vue 3 Composition API 在测试中获取 Ref 值
- react-typescript - 如何在反应打字稿项目中设置 setupProxy.js 文件
- swift - 当用户在 SwiftUI 中使用 AsyncImageView 查看 TabView 页面轮播中的第一张图片时,如何加载下一张图片?
- java - 超类引用调用的实例方法的返回值与声明的返回类型不兼容
- python - 使用 Plotly Python 将 Xaxis 置于 Y 坐标 0
- vue.js - 页面上的bootstrap-vue N b-form-radio,如何实现v-model
- python - 使用python将excel中特定单元格中的数据导入Visio中的特定单元格
- c# - Blazor Navigation Back Case 页面重新呈现
- visual-studio - 如何将文件添加到已删除的存储库(Visual Studio Code)?