角度材质表多粘行

Jos*_*oor 2 css typescript angular-material angular

根据官方文档,我们可以使用row insticky的输入。但我需要使垫子桌的前三排具有粘性。有什么办法可以做到这一点吗?sticky headermat-table

<div class="example-container mat-elevation-z8">
  <table mat-table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef> Weight </th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <th mat-header-cell *matHeaderCellDef> Symbol </th>
      <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

Akb*_*bal 5

如何为前 2 行创建一个数组(您在粘性标题中需要),之后您可以从实际数据集中删除这两行(使用shift())...

相关HTML

<div class="example-container mat-elevation-z8">
  <table mat-table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef> No. 
        <div *ngFor="let data of firstTwoRows; let i = index">
          <p *ngIf="i<2">{{data.position}} </p>
      </div>
     </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name  
        <div *ngFor="let data of firstTwoRows; let i = index">
          <p *ngIf="i<2">{{data.name}} </p>
      </div>
</th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef> Weight  
        <div *ngFor="let data of firstTwoRows; let i = index">
          <p *ngIf="i<2">{{data.weight}} </p>
      </div>
</th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <th mat-header-cell *matHeaderCellDef> Symbol  
        <div *ngFor="let data of firstTwoRows; let i = index">
          <p *ngIf="i<2">{{data.symbol}} </p>
      </div>
</th>
      <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
  </table>
</div>
Run Code Online (Sandbox Code Playgroud)

相关TS

import { Component } from "@angular/core";

/**
 * @title Table with sticky header
 */
@Component({
  selector: "table-sticky-header-example",
  styleUrls: ["table-sticky-header-example.css"],
  templateUrl: "table-sticky-header-example.html"
})
export class TableStickyHeaderExample {
  displayedColumns = ["position", "name", "weight", "symbol"];
  dataSource = ELEMENT_DATA;
  firstTwoRows: PeriodicElement[] = [];
  constructor() {
    this.firstTwoRows = [
      { position: 1, name: "Hydrogen", weight: 1.0079, symbol: "H" },
      { position: 2, name: "Helium", weight: 4.0026, symbol: "He" }
    ];
        this.dataSource.shift();
        this.dataSource.shift();
  }
}

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

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: "Hydrogen", weight: 1.0079, symbol: "H" },
  { position: 2, name: "Helium", weight: 4.0026, symbol: "He" },
  { position: 3, name: "Lithium", weight: 6.941, symbol: "Li" },
  { position: 4, name: "Beryllium", weight: 9.0122, symbol: "Be" },
  { position: 5, name: "Boron", weight: 10.811, symbol: "B" },
  { position: 6, name: "Carbon", weight: 12.0107, symbol: "C" },
  { position: 7, name: "Nitrogen", weight: 14.0067, symbol: "N" },
  { position: 8, name: "Oxygen", weight: 15.9994, symbol: "O" },
  { position: 9, name: "Fluorine", weight: 18.9984, symbol: "F" },
  { position: 10, name: "Neon", weight: 20.1797, symbol: "Ne" }
];
Run Code Online (Sandbox Code Playgroud)

完整的工作堆栈闪电战在这里