我试图使用垫表在我的Angular 15 <tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>在正确的位置在底部,和displayedColumns是工作的其他表部分我得到这个错误

Cannot read properties of undefined (reading 'template')
    at MatFooterRowDef.extractCellTemplate (table.mjs:415:38)
    at table.mjs:1904:27

在使用此功能时,我还需要添加/删除/导入哪些内容?我已导入MatTableModule

厌倦了棱角分明的Mat文档只是合法地错误和没有解释.

文件:ts

@Component({
  selector: 'saa-prior-sales',
  templateUrl: './prior-sales.component.html',
  styleUrls: ['./prior-sales.component.css'],
  standalone: true,
  imports: [CommonModule, CurrencyPipe, MatTableModule],
})
export class PriorSalesComponent implements OnChanges {
  @Input() sales?: IVehicleDetails[]; //New Interface for Sale
  //Sort these in descending Date (table cannot be sorted)
  dataSource = new MatTableDataSource<IVehicleDetails[]>();

  displayedColumns: string[] = [
    'model',
    'style',
    'eng',
    'trans',
    'miles',
    'price',
    'date',
  ];
  ngOnChanges(changes: SimpleChanges) {
    const vehicleList = changes['sales'].currentValue;
    this.dataSource = vehicleList;
  }
}

超文本标记语言

<div
  *ngIf="dataSource"
  class="w-full p-6 mt-0 bg-white border border-gray-200 rounded-lg shadow md:mb-4 sm:mb-4 ng-star-inserted saa-green">
  <div class="p-2 mb-4 text-xl font-medium saa-blue">Prior Sales</div>
  <table mat-table [dataSource]="dataSource">
    <!-- Location -->
    <ng-container matColumnDef="model">
      <th mat-header-cell *matHeaderCellDef>Model</th>
      <td mat-cell *matCellDef="let sale">
        {{ sale.vehicle?.location }}
      </td>
    </ng-container>

    <!-- LaneLot Column -->
    <ng-container matColumnDef="style">
      <th mat-header-cell *matHeaderCellDef>Style</th>
      <td mat-cell *matCellDef="let sale">{{ sale.lane }}/{{ sale.lot }}</td>
    </ng-container>

    <!-- VIN Column -->
    <ng-container matColumnDef="eng">
      <th mat-header-cell *matHeaderCellDef>Eng</th>
      <td mat-cell *matCellDef="let sale">
        {{ sale.eng }}
      </td>
    </ng-container>

    <!-- VIN Column -->
    <ng-container matColumnDef="trans">
      <th mat-header-cell *matHeaderCellDef>Trans</th>
      <td mat-cell *matCellDef="let sale">
        {{ sale.eng }}
      </td>
    </ng-container>

    <!-- VIN Column -->
    <ng-container matColumnDef="miles">
      <th mat-header-cell *matHeaderCellDef>Miles</th>
      <td mat-cell *matCellDef="let sale">
        {{ sale.eng }}
      </td>
    </ng-container>

    <!-- VIN Column -->
    <ng-container matColumnDef="price">
      <th mat-header-cell *matHeaderCellDef>Price</th>
      <td mat-cell *matCellDef="let sale">
        {{ sale.eng }}
      </td>
    </ng-container>

    <!-- VIN Column -->
    <ng-container matColumnDef="date">
      <th mat-header-cell *matHeaderCellDef>Date</th>
      <td mat-cell *matCellDef="let sale">
        {{ sale.date | date }}
      </td>
    </ng-container>

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

如果我go 掉垫脚排,只要我补充说它坏了,它就会起作用

推荐答案

<编码>*matFooterRowDef中指定的所有单元格必须在<编码>matColumnDef内有<编码>*matFooterCellDef

<编码>  <ng-container matColumnDef="date">
    <th mat-header-cell *matHeaderCellDef>Date</th>
    <td mat-cell *matCellDef="let sale">
      {{ sale.date | date }}
    </td>
    <td mat-footer-cell *matFooterCellDef>100</td> <!--  must be present if you have the property in matFooterRowDef array! --> 
  </ng-container>

编码

<编码>import { CommonModule } from '@angular/common';
import { Component, SimpleChanges } from '@angular/core';
import { MatTableModule, MatTableDataSource } from '@angular/material/table';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <div
  *ngIf="dataSource"
  class="w-full p-6 mt-0 bg-white border border-gray-200 rounded-lg shadow md:mb-4 sm:mb-4 ng-star-inserted saa-green">
  <div class="p-2 mb-4 text-xl font-medium saa-blue">Prior Sales</div>
    <table mat-table [dataSource]="dataSource">
      <!-- Location -->
      <ng-container matColumnDef="model">
        <th mat-header-cell *matHeaderCellDef>Model</th>
        <td mat-cell *matCellDef="let sale">
          {{ sale.vehicle?.location }}
        </td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <!-- LaneLot Column -->
      <ng-container matColumnDef="style">
        <th mat-header-cell *matHeaderCellDef>Style</th>
        <td mat-cell *matCellDef="let sale">{{ sale.lane }}/{{ sale.lot }}</td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <!-- VIN Column -->
      <ng-container matColumnDef="eng">
        <th mat-header-cell *matHeaderCellDef>Eng</th>
        <td mat-cell *matCellDef="let sale">
          {{ sale.eng }}
        </td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <!-- VIN Column -->
      <ng-container matColumnDef="trans">
        <th mat-header-cell *matHeaderCellDef>Trans</th>
        <td mat-cell *matCellDef="let sale">
          {{ sale.eng }}
        </td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <!-- VIN Column -->
      <ng-container matColumnDef="miles">
        <th mat-header-cell *matHeaderCellDef>Miles</th>
        <td mat-cell *matCellDef="let sale">
          {{ sale.eng }}
        </td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <!-- VIN Column -->
      <ng-container matColumnDef="price">
        <th mat-header-cell *matHeaderCellDef>Price</th>
        <td mat-cell *matCellDef="let sale">
          {{ sale.eng }}
        </td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <!-- VIN Column -->
      <ng-container matColumnDef="date">
        <th mat-header-cell *matHeaderCellDef>Date</th>
        <td mat-cell *matCellDef="let sale">
          {{ sale.date | date }}
        </td>
        <td mat-footer-cell *matFooterCellDef>100</td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let element; columns: displayedColumns"></tr>
      <tr mat-footer-row *matFooterRowDef="displayedColumns"></tr>
    </table>
  </div>
  `,
  imports: [CommonModule, MatTableModule],
})
export class App {
  sales?: any[] = [
    {
      model: 'test',
      style: 'test',
      eng: 'test',
      trans: 'test',
      miles: 'test',
      price: 'test',
      date: 'test',
    },
  ]; //New Interface for Sale
  //Sort these in descending Date (table cannot be sorted)
  dataSource = new MatTableDataSource<any[]>();

  displayedColumns: string[] = [
    'model',
    'style',
    'eng',
    'trans',
    'miles',
    'price',
    'date',
  ];
  ngOnChanges(changes: SimpleChanges) {
    const vehicleList = changes['sales'].currentValue;
    this.dataSource = vehicleList;
  }
}

bootstrapApplication(App);

stackblitz

Typescript相关问答推荐

net::BER_RECTION_REUSED和Uncatch使用Axios和TanStack-Query useMutation时未捕获错误(DelivercMutation)

等待的R没有用响应对象展开到R

ReturnType此索引方法与点表示法之间的差异

TypeScript实用程序类型—至少需要一个属性的接口,某些指定属性除外

如何在函数参数中使用(或模仿)`success`的行为?

typescribe警告,explate—deps,useEffect依赖项列表

扩展函数签名中的参数类型,而不使用泛型

根据另一个属性的值来推断属性的类型

使用`renderToPipeableStream`时如何正确恢复服务器端Apollo缓存?

如何使所有可选字段在打印脚本中都是必填字段,但不能多或少?

MatTool提示在角垫工作台中不起作用

防止重复使用 Select 器重新渲染

如何将TS泛型用于react-hook-form接口?

复选框Angular 在Ngfor中的其他块中重复

在TypeScript中扩展重载方法时出现问题

TypeScrip:使用Union ToInterval辅助对象,但保留子表达式

创建依赖函数参数的类型

接口中可选嵌套属性的类型判断

如何在Angular /字体中访问API响应的子元素?

Karma Angular Unit测试如何创建未解析的promise并在单个测试中解决它,以判断当时的代码是否只在解决后运行