【问题标题】:How do I add additional rows in mat-table without breaking the pattern?如何在不破坏模式的情况下在 mat-table 中添加其他行?
【发布时间】:2021-12-29 20:44:21
【问题描述】:

我正在尝试在 ngfor 之后再添加 2 个列(状态和按钮)。但是,它似乎会破坏整个结构(如图所示)

The Broken Table Screen Shoot

I am modifying code from material.angular.io

我还在columnsToDisplays 中添加了另外 2 行,因此它将显示额外的列。

任何关于修复它的建议将不胜感激!

<table mat-table
       [dataSource]="dataSource" multiTemplateDataRows
       class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
    <th mat-header-cell *matHeaderCellDef> {{column}} </th>
    <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
  </ng-container>

  <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
  <ng-container matColumnDef="expandedDetail">
    <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
      <div class="example-element-detail"
           [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
        <div class="example-element-diagram">
          <div class="example-element-position"> {{element.position}} </div>
          <div class="example-element-symbol"> {{element.symbol}} </div>
          <div class="example-element-name"> {{element.name}} </div>
          <div class="example-element-weight"> {{element.weight}} </div>
        </div>
        
        <ng-container matColumnDef="expandedDetail">
          <mat-header-cell *matHeaderCellDef> Action</mat-header-cell>
          <mat-cell *matCellDef="let element">
            <button mat-raised-button > Accept</button>
            <button mat-raised-button > Reject</button>
          </mat-cell>
        </ngcontainer>
        
        
        <div class="example-element-description">
          {{element.description}}
          <span class="example-element-description-attribution"> -- Wikipedia </span>
        </div>
      </div>
    </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="columnsToDisplays"></tr>
  <tr mat-row *matRowDef="let element; columns: columnsToDisplays;"
      class="example-element-row"
      [class.example-expanded-row]="expandedElement === element"
      (click)="expandedElement = expandedElement === element ? null : element">
  </tr>
  <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>

【问题讨论】:

  • 我忘记添加状态栏了,但是按钮部分是一样的。
  • 您要添加列还是行?在标题中是“行”,在描述中是“列”

标签: angular web frontend


【解决方案1】:

将“状态”和“按钮”添加到 columnsToDisplay

 columnsToDisplay = ['name', 'weight', 'symbol', 'position',  'status', 'button'];

向数据添加“状态”

{
    position: 1,
    name: 'Hydrogen',
    weight: 1.0079,
    symbol: 'H',
    description: `Some description`,
    status: 'Green'
  },

修改表格单元格 HTML -

<td mat-cell *matCellDef="let element">
      <span *ngIf="column !== 'button'"> {{element[column]}} </span>
      <span *ngIf="column === 'button'"> 
        <button>Button1</button>
        <button>Button1</button>
       </span>
         </td>

更新的 HTML(完整)

<table mat-table
       [dataSource]="dataSource" multiTemplateDataRows
       class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
    <th mat-header-cell *matHeaderCellDef> {{column}} </th>
    <td mat-cell *matCellDef="let element">
      <span *ngIf="column !== 'button'"> {{element[column]}} </span>
      <span *ngIf="column === 'button'"> 
        <button>Button1</button>
        <button>Button1</button>
       </span>
         </td>
  </ng-container>

  <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
  <ng-container matColumnDef="expandedDetail">
    <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
      <div class="example-element-detail"
           [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
        <div class="example-element-diagram">
          <div class="example-element-position"> {{element.position}} </div>
          <div class="example-element-symbol"> {{element.symbol}} </div>
          <div class="example-element-name"> {{element.name}} </div>
          <div class="example-element-weight"> {{element.weight}} </div>
        </div>
        <div class="example-element-description">
          {{element.description}}
          <span class="example-element-description-attribution"> -- Wikipedia </span>
        </div>
      </div>
    </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
  <tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
      class="example-element-row"
      [class.example-expanded-row]="expandedElement === element"
      (click)="expandedElement = expandedElement === element ? null : element">
  </tr>
  <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>

更新的 .ts 文件 -

import {Component} from '@angular/core';
import {animate, state, style, transition, trigger} from '@angular/animations';

@Component({
  selector: 'table-expandable-rows-example',
  styleUrls: ['table-expandable-rows-example.css'],
  templateUrl: 'table-expandable-rows-example.html',
  animations: [
    trigger('detailExpand', [
      state('collapsed', style({height: '0px', minHeight: '0'})),
      state('expanded', style({height: '*'})),
      transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
    ]),
  ],
})
export class TableExpandableRowsExample {
  dataSource = ELEMENT_DATA;
  columnsToDisplay = ['name', 'weight', 'symbol', 'position',  'status', 'button'];
  expandedElement: PeriodicElement | null;
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
  description: string;
  status: string
}

const ELEMENT_DATA: PeriodicElement[] = [
  {
    position: 1,
    name: 'Hydrogen',
    weight: 1.0079,
    symbol: 'H',
    description: `Hydrogen is a chemical element with symbol H and atomic number 1.`,
    status: 'Green'
  },
  {
    position: 2,
    name: 'Helium',
    weight: 4.0026,
    symbol: 'He',
    description: `Helium is a chemical element with symbol He and atomic number 2.`,
    status: 'Green'
  },
];

【讨论】:

    猜你喜欢
    • 2011-08-19
    • 2014-01-08
    • 1970-01-01
    • 2011-11-26
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多