首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Angular计算表列的总和并显示在页脚?

如何使用Angular计算表列的总和并显示在页脚?
EN

Stack Overflow用户
提问于 2019-04-09 03:48:25
回答 1查看 13.1K关注 0票数 2

我尝试使用Angular在表脚中显示列值的总和。

代码语言:javascript
复制
 <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>

..。

代码语言:javascript
复制
 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示例,但总和没有显示在页脚中。

有没有人能解释一下这个问题?没有关于这方面的例子。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-09 04:27:49

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

您需要做的就是定义一个页脚单元格,其方式与为每列中的页眉所做的方式类似。在脚注列的列绑定中,可以直接定义计算总和的方式。不需要添加包含总数据的另一行。在此之后,您只需添加一个脚注行定义,它就可以正常工作。

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

代码语言:javascript
复制
<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>

还有更改的组件代码,这样您就不需要修改数据了。

代码语言:javascript
复制
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的一个分支,你可以在那里看到它的工作。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55580753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档