AngularMaterial表格动态垂直高度限制
创始人
2024-10-28 08:01:30
0

使用MatTableDataSource和ViewChild来动态计算表格高度并设置max-height属性。以下是代码示例:

HTML:


  ...

TypeScript:

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';

@Component({
  selector: 'app-table-example',
  templateUrl: './table-example.component.html',
  styleUrls: ['./table-example.component.scss']
})
export class TableExampleComponent implements OnInit {
  @ViewChild('table') table: ElementRef;

  dataSource = new MatTableDataSource();
  tableHeight: string;

  ngOnInit(): void {
    this.dataSource.data = [{...}, {...}, ...];
    this.calculateTableHeight();
  }

  calculateTableHeight() {
    const tableElement = this.table.nativeElement;
    const headerEnd = tableElement.querySelector('.mat-header-row').getBoundingClientRect().bottom;
    const footerStart = tableElement.querySelector('.mat-footer-row').getBoundingClientRect().top;
    const height = footerStart - headerEnd;
    this.tableHeight = `${height}px`;
  }
}

在ngOnInit中设置数据源并调用calculateTableHeight来计算表格高度。calculateTableHeight中使用ElementRef和querySelector获取表格头和表格尾的坐标,并计算表格高度。最后将表格高度设置为max-height属性的值。

相关内容

热门资讯

1分钟实锤!智星德州菠萝外挂(... 1分钟实锤!智星德州菠萝外挂(德州)总是真的是有挂(详细辅助AA德州教程);1、任何智星德州菠萝外挂...
2分钟实锤!智星德州菠萝外挂(... 2分钟实锤!智星德州菠萝外挂(来玩德州)竟然是真的有挂(详细辅助技巧教程)1、任何智星德州菠萝外挂a...
第8分钟实锤!智星德州菠萝辅助... 第8分钟实锤!智星德州菠萝辅助器(德州ai)总是真的是有挂(详细辅助介绍教程)1、用户打开应用后不用...
8分钟实锤!德扑之星猫腻(德州... 8分钟实锤!德扑之星猫腻(德州wepower)确实真的是有挂(详细辅助第三方教程)1、德扑之星猫腻系...
第2分钟实锤!智星德州菠萝(线... 第2分钟实锤!智星德州菠萝(线上德州)切实有挂(详细辅助第三方教程)第2分钟实锤!智星德州菠萝(线上...
3分钟实锤!德扑ai助手(nz... 3分钟实锤!德扑ai助手(nzt德州)确实是有挂(详细辅助透明挂教程);1、玩家可以在德扑ai助手软...
第二分钟实锤!德扑之星有猫腻(... 第二分钟实锤!德扑之星有猫腻(德州app)本来真的有挂(详细辅助wpk教程)1、第二分钟实锤!德扑之...
第十分钟实锤!德州之星外挂(德... 第十分钟实锤!德州之星外挂(德州nzt)果然真的有挂(详细辅助AI教程)该软件可以轻松地帮助玩家将德...
第3分钟实锤!智星德州菠萝有挂... 第3分钟实锤!智星德州菠萝有挂吗(nzt德州)其实有挂(详细辅助科技教程)1、点击下载安装,智星德州...
第6分钟实锤!德扑之星有作弊(... 第6分钟实锤!德扑之星有作弊(nzt德州)真是存在有挂(详细辅助黑科技教程)进入游戏-大厅左侧-新手...