【问题标题】:How to calculate sum of table columns and show in footer using Angular?如何使用Angular计算表格列的总和并在页脚中显示?
【发布时间】:2019-08-30 00:50:48
【问题描述】:

我正在尝试使用 Angular 在表格页脚中显示列值的总数。

 <mat-header-row class="sticky-header" *matHeaderRowDef="['player', 'team', 'goals']"></mat-header-row>
      <mat-row *matRowDef="let row; columns: ['player', 'team', 'goals']"></mat-row>
      <mat-row class="sticky-footer" *matRowDef="let row: columns: ['total']; when:isLastRow"></mat-row>

...

 export class AppComponent {

  dataSource: PlayerDataSource;

  isLastRow = (data, index) => index === this.players.length;

  players = STATS.slice();

  constructor() {
    this.dataSource = new PlayerDataSource();
    this.dataSource.use(this.players.slice());
  }

}

阅读此github topic 后,我创建了this stackblitz 示例,但总和未显示在页脚中。

任何人都可以对这个主题有所了解吗?没有这方面的例子。谢谢。

【问题讨论】:

    标签: angular html-table sum


    【解决方案1】:

    angular material documentation 中有说明,examples 中有示例。

    您需要做的是以与每列中的标题类似的方式定义页脚单元格。在页脚列的列绑定中,您可以直接定义计算总和的方式。无需添加包含总数据的另一行。之后,您只需添加页脚行定义即可。

    这是您的示例中更改后的模板:

    <mat-table [dataSource]="dataSource">
    
      <!-- Columns -->
      <ng-container matColumnDef="player">
        <mat-header-cell *matHeaderCellDef> Player </mat-header-cell>
        <mat-cell *matCellDef="let player"> {{ player.name }}</mat-cell>
        <mat-footer-cell *matFooterCellDef></mat-footer-cell>
      </ng-container>
    
      <ng-container matColumnDef="team">
        <mat-header-cell *matHeaderCellDef> Team </mat-header-cell>
        <mat-cell *matCellDef="let player"> {{ player.team }}</mat-cell>
        <mat-footer-cell *matFooterCellDef></mat-footer-cell>
      </ng-container>
    
      <ng-container matColumnDef="goals">
        <mat-header-cell class="right-align" *matHeaderCellDef> Goals </mat-header-cell>
        <mat-cell class="right-align" *matCellDef="let player"> {{ player.goals }}</mat-cell>
        <mat-footer-cell *matFooterCellDef> Total: {{ calculateTotal() }}</mat-footer-cell>
      </ng-container>
    
      <!-- Rows -->
      <mat-header-row class="sticky-header" *matHeaderRowDef="['player', 'team', 'goals']"></mat-header-row>
      <mat-row *matRowDef="let row; columns: ['player', 'team', 'goals']"></mat-row>
      <mat-footer-row class="sticky-footer" *matFooterRowDef="['player', 'team', 'goals']"></mat-footer-row>
    
    </mat-table>
    

    还有更改的组件代码,因此您不需要修改数据。

    export class AppComponent {
    
      dataSource: PlayerDataSource;
    
      isLastRow = (data, index) => index === this.players.length;
    
      players = STATS.slice();
    
      constructor() {
        this.dataSource = new PlayerDataSource();
        this.dataSource.use(this.players.slice());
      }
    
      public calculateTotal() {
        return this.players.reduce((accum, curr) => accum + curr.goals, 0);
      }
    
    }
    
    
    export class PlayerDataSource extends DataSource<PlayerOrTotal> {
    
      dataWithTotal = new BehaviorSubject<PlayerOrTotal[]>([]);
    
      use(players: Player[]) {
        this.dataWithTotal.next([ ...players]);
      }
    
      connect(): Observable<PlayerOrTotal[]> {
        return this.dataWithTotal.asObservable();
      }
    
      disconnect() {}
    }
    

    我还创建了您的 StackBlitz 的一个分支,您可以在其中看到它的工作原理。

    【讨论】:

    • 谢谢!所以我错过了 calculateTotal() 函数
    • 是的,还有页脚行定义。您将其定义为另一行。检查我链接的官方文档。
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多