Ячейка нижнего колонтитула не отображается внутри * ngFor в Mat-Table - PullRequest
0 голосов
/ 17 апреля 2020

Я пытаюсь показать сумму внизу таблицы.

Я выполнил те же действия, что и в документации, но все еще не работает.

Ниже приведен код моего шаблона

<table mat-table [dataSource]="transactions" matSort class="table table-hover">
    <ng-container *ngFor="let columnName of displayedColumns" [matColumnDef]="columnName">
        <th mat-header-cell *matHeaderCellDef mat-sort-header class="text-danger">{{ columnName }}</th>
        <td mat-cell *matCellDef="let row">{{ row[columnName] }}</td>
        <td mat-footer-cell *matFooterCellDef> Total </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns" (click)="getRecord(row)"></tr>
    <td mat-footer-cell *matFooterCellDef>{{ total }}</td>
</table>

В компоненте

  total = this.transactions.map(t => t.cost).reduce((acc, value) => acc + value, 0);

Ниже приведен мой рабочий пример. Пожалуйста, скажите мне, что я делаю не так здесь

https://stackblitz.com/edit/angular-ehc6fn?file=src%2Fapp%2Ftable-footer-row-example.ts

1 Ответ

0 голосов
/ 17 апреля 2020

попробуйте с этим.

<table mat-table [dataSource]="transactions" class="mat-elevation-z8">
  <!-- Item Column -->
  <ng-container matColumnDef="item">
    <th mat-header-cell *matHeaderCellDef> Item </th>
    <td mat-cell *matCellDef="let transaction"> {{transaction.item}} </td>
    <td mat-footer-cell *matFooterCellDef> Total </td>
  </ng-container>

  <!-- Cost Column -->
  <ng-container matColumnDef="cost">
    <th mat-header-cell *matHeaderCellDef> Cost </th>
    <td mat-cell *matCellDef="let transaction"> {{transaction.cost | currency}} </td>
    <td mat-footer-cell *matFooterCellDef> {{getTotalCost() | currency}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  <tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>
</table>

проверка рабочая демоверсия

Обновлено

<table mat-table [dataSource]="transactions" matSort class="table table-hover">
    <ng-container *ngFor="let columnName of displayedColumns" [matColumnDef]="columnName">
        <th mat-header-cell *matHeaderCellDef mat-sort-header class="text-danger">{{ columnName }}</th>
        <td mat-cell *matCellDef="let row">{{ row[columnName] }}</td>
        <td mat-footer-cell *matFooterCellDef>
          {{columnName === 'item' ? 'Total': getTotalCost()}}
        </td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns" (click)="getRecord(row)"></tr>
    <tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>
</table>

обновленная демка

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...