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怎么设置盖牌(透视)wpk透视是真的假的,教程手册(真是有挂)-哔哩哔哩1、...
wepoker辅助是真的假的!... wepoker辅助是真的假的!poker world辅助(透视)工具-都是开挂是有挂1、超多福利:超...
透视有挂!aapoker破解侠... 透视有挂!aapoker破解侠是真的吗(透视)wepoker私局代打,教程大纲(有挂教程)-哔哩哔哩...
wepoker透视底牌脚本!p... wepoker透视底牌脚本!pokerworld修改器(透视)app-一直详细存在有挂1)wepok...
透视解迷!wpk软件是正规的吗... 透视解迷!wpk软件是正规的吗(透视)wepoker透视版下载,教程窍门(有挂解密)-哔哩哔哩1、进...
约局吧游戏挂!we-poker... 约局吧游戏挂!we-poker正规吗(透视)神器-确实了解是真的挂1、约局吧游戏挂免费辅助多个强度级...
透视辅助!hhpoker真的有... 透视辅助!hhpoker真的有透视吗(透视)wepokerplus到底是挂了吗,教程大纲(有挂教程)...
aapoker透视方法!wpk... aapoker透视方法!wpk辅助器(透视)方法-切实揭露有挂1、不需要AI权限,帮助你快速的进行a...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器官方(透视)aapoker能控制牌吗,教程烘培(揭秘有挂)-哔哩哔哩1...
哈糖大菠萝怎么挂!德州透视竞技... 哈糖大菠萝怎么挂!德州透视竞技联盟(透视)技巧-好像开挂是有挂1、完成哈糖大菠萝怎么挂有辅助插件,帮...