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

相关内容

热门资讯

据悉!wepoker底牌透视(... 据悉!wepoker底牌透视(透视)呱呱丰城双剑辅助(一贯存在有辅助辅助器)-哔哩哔哩暗藏猫腻,小编...
透视计算!wepoker免费脚... 透视计算!wepoker免费脚本咨询(透视)爱玩修改器(确实是真的辅助插件)-哔哩哔哩1、wepok...
近日!wepoker黑侠辅助器... 近日!wepoker黑侠辅助器正版下载(透视)越乡游义乌辅助器微信(原来真的是有辅助安装)-哔哩哔哩...
今日!pokemmo辅助工具(... 今日!pokemmo辅助工具(透视)微友辅助神器下载(确实真的有辅助神器)-哔哩哔哩1、微友辅助神器...
透视中牌率!悦扑克脚本(透视)... 透视中牌率!悦扑克脚本(透视)天蝎大厅辅助(果然真的有辅助app)-哔哩哔哩1、天蝎大厅辅助免费脚本...
网友热议!wepoker插件辅... 网友热议!wepoker插件辅助(透视)云南捞腌菜辅助(确实真的有辅助平台)-哔哩哔哩1、网友热议!...
透视最新!hardrock透视... 透视最新!hardrock透视工具(透视)渝都麻将开挂方法(好像有辅助修改器)-哔哩哔哩1)渝都麻将...
透视神器!wepoker私人局... 透视神器!wepoker私人局规律(透视)丽水都莱有辅助吗(其实存在有辅助安装)-哔哩哔哩1、打开软...
于此同时!wepoker开脚本... 您好,免费游戏辅助软件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩...
今日!智星德州插件怎么下载(透... 今日!智星德州插件怎么下载(透视)非凡贪玩挂(都是存在有辅助平台)-哔哩哔哩1、进入到非凡贪玩挂是否...