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

相关内容

热门资讯

现有说明如下!wpk免费辅助(... 现有说明如下!wpk免费辅助(透视)四川熊猫辅助软件(好像是真的辅助app)-哔哩哔哩一、四川熊猫辅...
透视有挂!红龙poker辅助平... 透视有挂!红龙poker辅助平台(透视)海盗来了辅助哪个好(本来有辅助下载)-哔哩哔哩1.海盗来了辅...
据监测!hhpoker有辅助吗... 据监测!hhpoker有辅助吗(透视)爱来510k辅助器(都是真的是有辅助工具)-哔哩哔哩运爱来51...
透视了解!德州hhpoker脚... 透视了解!德州hhpoker脚本(透视)中至九江黑科技app怎么下载(确实真的是有辅助平台)-哔哩哔...
透视能赢!aapoker辅助插... 透视能赢!aapoker辅助插件工具(透视)微乐透视辅助工具(其实真的有辅助神器)-哔哩哔哩1.微乐...
此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker安装教程(透视)杭州都莱大菠萝有挂吗(果然是有辅助软件)-哔哩哔哩1...
透视智能ai!淘宝买wepok... 透视智能ai!淘宝买wepoker透视有用吗(透视)玩吧辅助器(竟然是有辅助插件)-哔哩哔哩进入游戏...
为切实保障!hhpoker破解... 您好,微信小程序全能修改器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...
现有说明如下!aapoker如... 现有说明如下!aapoker如何设置胜率(透视)陕西三代辅助器下载(果然是真的辅助软件)-哔哩哔哩1...
透视新版!wpk透视插件(透视... 透视新版!wpk透视插件(透视)吉祥填大坑透视多少钱(一贯真的是有辅助软件)-哔哩哔哩1、上手简单,...