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、每一步都需...
透视实锤!德州透视是真的假的(... 透视实锤!德州透视是真的假的(透视)亿游十三道辅助(果然存在有辅助下载)-哔哩哔哩;该软件可以轻松地...
随着!wepoker私人局规律... 随着!wepoker私人局规律(透视)蜀山四川小程序作必弊码大全(原来是真的辅助修改器)-哔哩哔哩1...
截至目前!wpk是真的还是假的... 截至目前!wpk是真的还是假的(透视)潮汕汇木虱辅助下载(竟然是有辅助软件)-哔哩哔哩1、潮汕汇木虱...
在玩家背景下!wepoker黑... 在玩家背景下!wepoker黑侠辅助器正版下载(透视)开心泉州辅助(其实是有辅助app)-哔哩哔哩1...
透视中牌率!哈糖大菠萝怎么开挂... 透视中牌率!哈糖大菠萝怎么开挂(透视)打两圈辅助软件(好像真的有辅助下载)-哔哩哔哩1、哈糖大菠萝怎...
透视科技!约局吧德州可以透视吗... 透视科技!约局吧德州可以透视吗(透视)人海大厅软件辅助(切实真的有辅助修改器)-哔哩哔哩1)人海大厅...
最终!we poker游戏下(... 最终!we poker游戏下(透视)聚友联盟免费辅助器(好像真的是有辅助app)-哔哩哔哩1、该软件...
备受关注的!wepokerpl... 备受关注的!wepokerplus开挂(透视)浙江宝宝游戏透视辅助器(原来是真的辅助工具)-哔哩哔哩...