Search code examples
angularag-grid

In Angular ag-grid ,how to remove Total on footer for a column/field?


I want to remove Total value in footer for a specific column in ag-grid and leave Total for all other columns as it is! Have used groupIncludeTotalFooter = true to set the Total.

I tried using custom aggregate function but with no luck!


Solution

  • We can simply use Component Cell Renderer for this purpose.

    In this example, we group our data by country and year respectively. We also show totals for gold, silver and bronze medals.

    Let's say we don't wanna show totals for gold medals in our Total footer

    my-inner-renderer.component.ts

    import { Component } from '@angular/core';
    import { ICellRendererAngularComp } from 'ag-grid-angular';
    import { ICellRendererParams } from 'ag-grid-community';
    
    @Component({
      selector: 'total-value-component',
      template: `<span>{{ cellValue }}</span>`,
    })
    export class MyInnerRenderer implements ICellRendererAngularComp {
      public cellValue = '';
    
      agInit(params: ICellRendererParams): void {
        if (params.node.footer) {
          if (params.node.level === -1) {
            // if it is gold field, show empty string
            if (params.colDef.field == 'gold') {
              this.cellValue = '';
            } else {
              this.cellValue = params.hasOwnProperty('value') ? params.value : '';
            }
          } else {
            this.cellValue = params.hasOwnProperty('value') ? params.value : '';
          }
        } else {
          this.cellValue = params.hasOwnProperty('value') ? params.value : '';
        }
      }
    
      refresh(params: ICellRendererParams) {
        return false;
      }
    }
    

    Then we use it in our Column Definitions

    public columnDefs: ColDef[] = [
      { field: 'country', rowGroup: true, hide: true },
      { field: 'year', rowGroup: true, hide: true },
      { field: 'gold', aggFunc: 'sum', cellRenderer: MyInnerRenderer },
      { field: 'silver', aggFunc: 'sum', cellRenderer: MyInnerRenderer },
      { field: 'bronze', aggFunc: 'sum', cellRenderer: MyInnerRenderer },
    ];
    

    Result Result

    Here's the working plunker: https://plnkr.co/edit/wA4siGPsykpc91rO?preview