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、大菠萝免费辅助模...
目前!红龙poker辅助工具,... 目前!红龙poker辅助工具,娱乐辅助系统,一直真的是有辅助工具(哔哩哔哩)1、任何红龙poker辅...
解密透视!拱趴大菠萝怎么开挂,... 解密透视!拱趴大菠萝怎么开挂,德州来玩辅助器(透视)一直是有辅助技巧(哔哩哔哩)1、拱趴大菠萝怎么开...
现就发布提示!wepoker可... 现就发布提示!wepoker可以开透视吗,欢乐茶馆挂有没有,真是存在有辅助工具(哔哩哔哩)1、点击下...
科普透视!竞技联盟辅助,智星德... 科普透视!竞技联盟辅助,智星德州有脚本吗(透视)都是有辅助技巧(哔哩哔哩)1、玩家可以在智星德州有脚...
今年以来!wepoker私人局... 今年以来!wepoker私人局怎么玩,哥哥打大a辅助工具,一贯是真的有辅助插件(哔哩哔哩)1、哥哥打...
了解透视!哈糖大菠萝攻略,佛手... 了解透视!哈糖大菠萝攻略,佛手在线大菠萝辅助(透视)原来是有辅助插件(哔哩哔哩)1、该软件可以轻松地...
近期!pokermaster脚... 近期!pokermaster脚本,天天爱柳州辅助,都是有辅助工具(哔哩哔哩)1、下载好天天爱柳州辅助...
专业透视!来玩app破解,德州... 专业透视!来玩app破解,德州透视脚本(透视)果然一直都是有辅助神器(哔哩哔哩)1、这是跨平台的德州...
针对!epoker透视底牌,新... 针对!epoker透视底牌,新畅游互娱辅助,切实存在有辅助技巧(哔哩哔哩)进入游戏-大厅左侧-新手福...