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公共底牌(透视)插件下载(原来是真的有挂);1、上手简单,内置详细流程视频教学...
透视好友!pokermaste... 透视好友!pokermaster脚本,智星菠萝有挂吗(确实存在有挂)1、完成智星菠萝有挂吗的残局,帮...
透视教程!菠萝德州透视脚本,p... 透视教程!菠萝德州透视脚本,pokemmo辅助器手机版下载(都是是真的有挂);1、pokemmo辅助...
透视脚本!aapoker俱乐部... 透视脚本!aapoker俱乐部靠谱吗(透视)透视脚本(总是有挂)1、每一步都需要思考,不同水平的挑战...
透视辅助!拱趴游戏破解器,哈糖... 透视辅助!拱趴游戏破解器,哈糖大菠萝可以开挂吗(真是存在有挂)1、哈糖大菠萝可以开挂吗机器人多个强度...
透视苹果版!aapoker发牌... 透视苹果版!aapoker发牌逻辑(透视)真的假的(竟然是真的有挂)1、首先打开aapoker发牌逻...
透视玄学!约局吧透视挂下载,约... 透视玄学!约局吧透视挂下载,约局吧辅助器(原来真的有挂);小薇(透视辅助)致您一封信;亲爱约局吧透视...
透视ai代打!aapoker ... 透视ai代打!aapoker ai插件(透视)万能辅助器(切实真的是有挂)透视ai代打!aapoke...
透视系统!拱趴大菠萝机器人,哈... 透视系统!拱趴大菠萝机器人,哈糖大菠萝挂(原来有挂)拱趴大菠萝机器人辅助器中分为三种模型:拱趴大菠萝...
透视脚本!aa poker辅助... 透视脚本!aa poker辅助(透视)透视脚本下载(本来是真的有挂)1、下载好aa poker辅助辅...