简体   繁体   English

如何在 ag-grid 表的页脚中启用或显示总行

[英]How to enable or show total row in footer of ag-grid table

I am working with Ag-Grid table and I want to show the total row in the footer of the table.我正在使用 Ag-Grid 表,我想在表的页脚中显示总行。 Some How I achieved it by using 2 tables 1 is for actual data and 2nd is for the Total row.我如何通过使用 2 个表实现它的一些方法,第 1 个用于实际数据,第 2 个用于总计行。

It's working fine with a Normal non-scrollable table but if it's a pined or scrollable table then the top table scrolls but the bottom one sticks in the same place.它在普通不可滚动表上工作正常,但如果它是固定或可滚动表,则顶部表会滚动,但底部表会固定在同一个位置。

I want to scroll both the table at the same time with the same offset.我想以相同的偏移量同时滚动两个表。

For more detail look at the below screenshot which I have the total bottom table.有关更多详细信息,请查看下面的屏幕截图,其中我有总底表。

Normal Table:普通表:

在此处输入图像描述

You can see in the normal table it's showing total perfectly.您可以在普通表中看到它完美地显示了总计。

While in the Pinned column table it's not working as expected.在固定列表中,它没有按预期工作。

Pinned Column Table:固定列表: 在此处输入图像描述

Look at the scroll bar of both the table.查看两个表的滚动条。

I want to scroll both the table parallelly.我想同时滚动两个表。

Or is there any other way to show the Total row other than the dual table?或者除了双表之外还有其他显示总计行的方法吗?

Please Guide me on this.请指导我。

Finally, after lots of R&D on the footer total row, I found that we can implement PinnedBootomRow to show our total for the table.最后,在对页脚总计行进行了大量研发之后,我发现我们可以实现PinnedBootomRow来显示表格的总计。

So I dropped the above idea as it's creating a problem with the pinned columns and also managing 2 tables is tricky.所以我放弃了上述想法,因为它给固定列造成了问题,而且管理 2 个表也很棘手。

Thanks to AreYouSiries who provided such a nice demo on plucker here感谢AreYouSiries这里提供了一个关于 plucker 的精彩演示

Also Thanks to Ag-Grid for such a nice doc with live examples还要感谢Ag-Grid提供了这么好的文档和活生生的例子

My Custom Plucker version for Total Row is here我的 Total Row 的自定义 Plucker 版本在这里

By following the above examples I am able to achieve my exact requirements and Now it's working as expected.通过遵循上述示例,我能够实现我的确切要求,现在它按预期工作。

Let me add sort steps to achieve the total row feature in ag-grid:让我添加排序步骤以实现 ag-grid 中的总行功能:

1st step - Generate Pinned Total Row: Below function will generate an empty target object with all your columns available in the grid.第一步 - 生成固定总行:在 function 下方将生成一个空目标 object,所有列都在网格中可用。

generatePinnedBottomData(){
        // generate a row-data with null values
    let result = {};

    this.gridColumnApi.getAllGridColumns().forEach(item => {
        result[item.colId] = null;
    });
    return this.calculatePinnedBottomData(result);
}

2nd step Calculate Total for some or all the columns: You need to calculate the total from row data and set the value in the above generated targeted row.第二步为部分或所有列计算总计:您需要从行数据中计算总计,并在上面生成的目标行中设置值。

calculatePinnedBottomData(target: any){
        //console.log(target);
        //**list of columns fo aggregation**
        let columnsWithAggregation = ['age']
        columnsWithAggregation.forEach(element => {
          console.log('element', element);
            this.gridApi.forEachNodeAfterFilter((rowNode: RowNode) => {
              //if(rowNode.index < 10){
                //console.log(rowNode);
              //}
                if (rowNode.data[element])
                    target[element] += Number(rowNode.data[element].toFixed(2));
            });
            if (target[element])
                target[element] = `Age Sum: ${target[element].toFixed(2)}`;
        })
        //console.log(target);
        return target;
    }

3rd and last step: Call above generatePinnedBottomData() function where u get your grid data from API or local DB.第三步也是最后一步:调用上面的generatePinnedBottomData() function,您可以从 API 或本地 DB 获取网格数据。 In the above example, we used to call from onGridReady()在上面的例子中,我们曾经从onGridReady()调用

onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;
    console.log(this.gridColumnApi.getAllGridColumns())
    this.http
      .get("https://raw.githubusercontent.com/ag-grid/ag-grid/master/packages/ag-grid-docs/src/olympicWinners.json")
      .subscribe(data => {
        this.rowData = data;
        setTimeout(()=>{
          let pinnedBottomData = this.generatePinnedBottomData();
        this.gridApi.setPinnedBottomRowData([pinnedBottomData]);
        }, 500)
      });
  }

You need to assign generated data to the grid.您需要将生成的数据分配给网格。

That's it now you can see your total row pinned at bottom of the grid.就是这样,现在您可以看到固定在网格底部的总行。

Final Output:最终 Output: 在此处输入图像描述

Hope my post will help you to achieve the total row in the grid.希望我的帖子能帮助您实现网格中的总行。

Ag-Grid has a built-in option to create a group footer that uses aggregation functions such as sum, avg, and more. Ag-Grid 有一个内置选项来创建一个组页脚,该组页脚使用 sum、avg 等聚合函数。 See details here: https://www.ag-grid.com/javascript-data-grid/grouping-footers/在此处查看详细信息: https://www.ag-grid.com/javascript-data-grid/grouping-footers/

Here is a example to add a pinned total row.这是添加固定总行的示例。 Hope it helps anyone looking for it.希望它可以帮助任何寻找它的人。

 const columnDefs = [ { field: "make" }, { field: "model" }, { field: "price" } ]; // specify the data const rowData = [ { make: "Toyota", model: "Celica", price: 35000 }, { make: "Ford", model: "Mondeo", price: 32000 }, { make: "Porsche", model: "Boxster", price: 72000 } ]; const calcTotalCols = ['price']; const totalRow = function(api) { let result = [{}]; // initialize all total columns to zero calcTotalCols.forEach(function (params){ result[0][params] = 0 }); // calculate all total columns calcTotalCols.forEach(function (params){ rowData.forEach(function (line) { result[0][params] += line[params]; }); }); api.setPinnedBottomRowData(result); } // let the grid know which columns and what data to use const gridOptions = { columnDefs: columnDefs, rowData: rowData }; // setup the grid after the page has finished loading document.addEventListener('DOMContentLoaded', () => { const gridDiv = document.querySelector('#myGrid'); new agGrid.Grid(gridDiv, gridOptions); totalRow(gridOptions.api); });
 <:DOCTYPE html> <html lang="en"> <head> <title>Ag-Grid Total Row Example</title> <script src="https.//unpkg.com/ag-grid-community/dist/ag-grid-community.min.js"></script> <script src="main:js"></script> </head> <body> <div id="myGrid" style="height; 200px: width;500px;" class="ag-theme-alpine"></div> </body> </html>

I think this is the current state of the art, and it's very simple.我认为这就是艺术的当前state,而且非常简单。 It's too bad it's so hard to find the article:太糟糕了,很难找到这篇文章:

https://ag-grid.zendesk.com/hc/en-us/articles/360015979971 https://ag-grid.zendesk.com/hc/zh-cn/articles/360015979971

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

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