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

相关内容

热门资讯

带你了解!aapoker安装包... 带你了解!aapoker安装包怎么使用,hhpkoer辅助挂是真的吗,都是是真的有挂(有挂功能)1、...
让我来分享经验!hhpoker... 让我来分享经验!hhpoker是真的吗,如何下载wpk透视版,其实是有挂(存在有挂)1、完成有辅助插...
我来教教大家!xpoker辅助... 我来教教大家!xpoker辅助器,aapoker插件下载,总是真的有挂(有挂透视)1、全新机制【ai...
总算了解!wpk辅助器,soh... 总算了解!wpk辅助器,sohoo poker辅助,切实真的有挂(有挂方式)1、起透看视 辅助软件价...
指导大家!来玩app 德州 辅... 指导大家!来玩app 德州 辅助,wepoker好友助力码,都是是真的有挂(有挂分析)1、这是跨平台...
玩家必看攻略!wepoker轻... 玩家必看攻略!wepoker轻量版透视系统,wepoker高级辅助,真是存在有挂(有挂实锤)辅助器是...
一分钟快速了解!wepoker... 一分钟快速了解!wepoker透视最简单三个步骤,购买的wpk辅助在哪里下载,其实有挂(有挂教学)1...
玩家爆料!wepoker辅助器... 玩家爆料!wepoker辅助器安装包,wepoker辅助器安装包,总是是真的有挂(有挂规律)1、we...
最新技巧!hhpoker有后台... 最新技巧!hhpoker有后台操作吗,pokemmo手机版脚本,竟然存在有挂(有挂教学)1、透视辅助...
带你了解!德普之星有辅助软件吗... 带你了解!德普之星有辅助软件吗,红龙poker有辅助吗,竟然真的有挂(详细教程)1、进入游戏-大厅左...