简体   繁体   English

将自定义的“ALL”(总计)行添加到可以像任何其他行一样选择的 ag-grid 的顶部

[英]Adding a custom “ALL” (Total) row to the top of an ag-grid that is selectable like any other row

I have an ag-grid table with 2 columns - one text and one number.我有一个包含 2 列的 ag-grid 表 - 一个文本和一个数字。

I need to add a custom row to the top of my ag-grid table with the total value of the number column and the word "ALL" in the text column.我需要在我的 ag-grid 表的顶部添加一个自定义行,其中包含数字列的总值和文本列中的“ALL”一词。

This needs to be on the top.这需要在顶部。 And should not change its position even if the table is sorted.即使表格已排序,也不应更改其位置。

Also, the row should be selectable.此外,该行应该是可选择的。

Any help in this regard will be highly appreciated!在这方面的任何帮助将不胜感激!

Sounds like you are describing Row Pinning which is already a feature in AG Grid.听起来你在描述行固定,这已经是 AG Grid 中的一个功能。 However, since you've also stated that row selection is a requirement, this will not be possible with Row Pinning as it's not supported .但是,由于您还声明行选择是一项要求,因此 Row Pinning 无法实现,因为它不受支持

Instead what I'd recommend is adding an extra row object inside the rowData for the custom row, and handling the update of the number column and the custom row position yourself when necessary:相反,我建议在rowData为自定义行添加一个额外的行对象,并在必要时自己处理数字列和自定义行位置的更新:

If you want to handle the total value of the number column then you can use the following logic:如果要处理数字列的总值,则可以使用以下逻辑:

function updateTotalRowNode() {
  let totalRowNode;
  let sum = 0;
  gridOptions.api.forEachNode((node) => {
    if (node.data.totalRow) {
      totalRowNode = node;
    } else {
      sum += node.data.gold;
    }
  });
  totalRowNode.setDataValue('gold', sum);
}

If you want to keep the custom row always on the top row then you can implement the postSort callback :如果要始终将自定义行保留在顶行,则可以 实现postSort回调

  postSort: (rowNodes) => {
    // here we put All row on top while preserving the sort order
    let nextInsertPos = 0;
    for (let i = 0; i < rowNodes.length; i++) {
      const athlete = rowNodes[i].data.athlete;
      if (athlete === 'All') {
        rowNodes.splice(nextInsertPos, 0, rowNodes.splice(i, 1)[0]);
        nextInsertPos++;
      }
    }
  },

See the following plunker for the implementation .有关实现,请参阅以下 plunker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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