我有一张棱角桌.当向下滚动表格时,如何使第一列单元格中的SPAN元素粘在粘性的垫子标题行上?

当滚动表格时,SPAN元素应该"粘在"MAT-HEADER-ROW的下边框上,并与表格一起滚动到其单元格的末尾.一旦SPAN元素到达其单元格的末尾,它就应该与其单元格一起"隐藏"在MAT-HEADER-ROW后面.

我已经try 将CSS样式添加到SPAN元素,但它的工作方式与我预期的不同

<div class="table-container" style="height: 80vh; overflow: auto">
  <mat-table [dataSource]="dataSource" class="mat-elevation-z8">
    <ng-container matColumnDef="position">
      <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
      <mat-cell *matCellDef="let element" style="height: 200px">
        <span style="position: sticky; top: 0; left: 0; z-index: 1">
          {{element.position}}
        </span>
      </mat-cell>
    </ng-container>

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

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

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

    <mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>
</div>
import {Component} from '@angular/core';
import {MatTableModule} from '@angular/material/table';

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'},
];

/**
 * @title Basic use of `<table mat-table>`
 */
@Component({
  selector: 'table-basic-example',
  styleUrls: ['table-basic-example.css'],
  templateUrl: 'table-basic-example.html',
  standalone: true,
  imports: [MatTableModule],
})
export class TableBasicExample {
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;
}

推荐答案

有两个选项:

  1. 使用溢出:继承MAT-CELL的属性
  2. 将TABLE标签与MAT-TABLE一起使用

Typescript相关问答推荐

类型脚本不会推断键的值类型

如何判断输入是否是TypeScript中的品牌类型?

类型脚本泛型最初推断然后设置

React typescribe Jest调用函数

带switch 的函数的返回类型的类型缩小

打印脚本中的动态函数重载

记录键的泛型类型

在HighChats列时间序列图中,十字准线未按预期工作

为什么我的动画状态在组件实例之间共享?

如何将对象数组中的键映射到另一种对象类型的键?

TYPE FOR ARRAY,其中每个泛型元素是单独的键类型对,然后在该数组上循环

API文件夹内的工作员getAuth()帮助器返回:{UserID:空}

为什么上下文类型在`fn|curred(Fn)`的联合中不起作用?

在抽象类属性定义中扩展泛型类型

为什么在这种情况下,打字脚本泛型无法正确自动完成?

如何通过转换器类继承使用多态将对象从一种类型转换为另一种类型

推断集合访问器类型

打字:注解`是字符串`而不是`布尔`?

Typescript 是否可以区分泛型参数 void?

在对象类型的类型别名中,属性是用分号 (;) 还是逗号 (,) 分隔的?