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属性的值。

相关内容

热门资讯

外挂秘籍!pokerworld... 外挂秘籍!pokerworld软件,哈糖大菠萝可以开挂吗-一贯存在有辅助方法(哔哩哔哩)1、下载好哈...
外挂课程!拱趴大菠萝机器人,竞... 外挂课程!拱趴大菠萝机器人,竞技联盟透视-总是是有辅助技巧(哔哩哔哩)1、首先打开拱趴大菠萝机器人辅...
外挂法门!we poker免费... 外挂法门!we poker免费辅助器,悦扑克脚本-一贯真的有辅助软件(哔哩哔哩);小薇(辅助器软件下...
外挂教程书!约局吧app有挂吗... 外挂教程书!约局吧app有挂吗,约局吧是否有挂-切实是有辅助软件(哔哩哔哩)1)约局吧app有挂吗免...
外挂烘培!约局吧德州有挂吗,智... 外挂烘培!约局吧德州有挂吗,智星菠萝透视-本来真的是有辅助插件(哔哩哔哩)1、外挂烘培!约局吧德州有...
外挂课程!poker红龙辅助,... 外挂课程!poker红龙辅助,德普之星透视辅助插件-确实真的是有辅助技巧(哔哩哔哩)1、poker红...
外挂办法!拱趴大菠萝自动计算机... 外挂办法!拱趴大菠萝自动计算机器人,德普之星私人局透视-确实真的是有辅助app(哔哩哔哩);拱趴大菠...
外挂技法!约局吧开挂神器是真的... 外挂技法!约局吧开挂神器是真的吗,xpoker怎么作必弊-一贯存在有辅助攻略(哔哩哔哩)1、许多玩家...
外挂法子!德州透视是真的吗,哈... 外挂法子!德州透视是真的吗,哈糖大菠萝破解器-一贯存在有辅助app(哔哩哔哩)1、玩家可以在德州透视...
外挂秘籍!大菠萝手游辅助,哈糖... 外挂秘籍!大菠萝手游辅助,哈糖大菠萝挂法-竟然真的是有辅助教程(哔哩哔哩)一、大菠萝手游辅助游戏安装...