如何在ag-grid的页脚中显示特定列的总和

Pra*_*nar 4 javascript ag-grid

在此输入图像描述如何在 ag-grid 表的页脚中显示特定列的总和。例如,如果我在 ag-grid 表中有列名工资,那么我想在网格底部显示工资总额,如以下屏幕所示。

rdr*_*drw 5

您可以使用AgGrid 文档的行固定部分中讨论的pinnedBottomRowData网格属性。

使用问题中的列,您可以执行如下操作:

getTotalRow(rowData) {
  const initial = {
    fname: undefined,
    lname: undefined,
    position: undefined,
    office: 'Total', // if you want to display in the 'Total' in the Office column
    salary: 0,
  };

  // iterate overRow data summing each property for total
  // assuming lodash reduce is available and skipping actual summing logic of salary
  const rowTotal = reduce((totals, rowData) => {...}, initial) 


  // rowTotal has a structure like:
  //  {
  //    fname: undefined, // undefined should render blank cell in row at column
  //    lname: undefined,
  //    position: undefined,
  //    office: 
  //    salary: 1234567.89,
  //  }

  // have to format in an array as pinnedBottomRowData expects the same format as rowData
  return [rowTotal];
}
Run Code Online (Sandbox Code Playgroud)

这种方法的缺点是,您必须手动运行列求和并rowData自己构建对象,但我认为这是一个比列聚合更琐碎的解决方案,如果您要做的不仅仅是显示数据(进行单元格编辑),列聚合会很快变得复杂,编辑后重新计算特定单元格等)

此外,它固定在网格的底部,因此如果您没有足够的行,则最后一行数据和总计行之间将有空间(请参见屏幕截图)。行/列标题并不重要,所以我将它们涂黑。

最后一行与固定底行之间的空间