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怎么破解游戏(WePoKerplus)切实真的是有辅助脚本(哔哩哔哩)透视要...
透视举措!hhpoker辅助器... 透视举措!hhpoker辅助器视频(WePoKer永久)都是是真的有辅助插件(哔哩哔哩)1、完成hh...
透视窍要!wpk作必弊是真的吗... 透视窍要!wpk作必弊是真的吗(WePoKer用挂)一直真的是有辅助方法(哔哩哔哩)wpk作必弊是真...
透视绝活!约局吧是否有挂(透视... 透视绝活!约局吧是否有挂(透视)cloudpoker怎么开挂(辅助)好像一直总是有脚本(哔哩哔哩)1...
透视练习!wpk透视插件(We... 透视练习!wpk透视插件(WePoKer增加)竟然真的是有辅助技巧(哔哩哔哩)1、wpk透视插件透视...
透视方案!werplan外开挂... 透视方案!werplan外开挂(透视)约局吧德州真的有透视挂吗(辅助)切实真的是有工具(哔哩哔哩)小...
透视积累!智星菠萝可以辅助吗(... 透视积累!智星菠萝可以辅助吗(透视)拱趴游戏破解器(辅助)其实是真的有app(哔哩哔哩)1、首先打开...
透视模板!德普之星辅助工具如何... 透视模板!德普之星辅助工具如何设置(德普之星开辅助)总是是真的有辅助方法(哔哩哔哩)1、德普之星辅助...
透视教程书!sohoo辅助(透... 透视教程书!sohoo辅助(透视)pokemmo手机版脚本(辅助)果然存在有工具(哔哩哔哩)1、实时...
透视步骤!pokermaste... 透视步骤!pokermaster修改器(透视)aa poker辅助包(辅助)其实一直都是有神器(哔哩...