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

时间:2019-10-03 04:00:42

标签: javascript ag-grid

enter image description here如何在ag-grid表的页脚中显示特定列的总和。 例如,如果我在ag-grid表中有列名薪水,那么我想在网格底部显示总薪水总和,如以下屏幕所示。

1 个答案:

答案 0 :(得分:0)

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

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

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

该方法的缺点是您必须手动运行列求和并自己构建rowData对象,但是我认为这比列聚合要简单得多,列聚合会变得非常复杂不仅仅是显示数据(进行单元格编辑,编辑后重新计算特定单元格等)

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

space between last row and pinned bottom row

相关问题