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

相关内容

热门资讯

科普透视!德州局怎么透视,淘宝... 科普透视!德州局怎么透视,淘宝的微乐辅助是真的吗,演示教程(真的有挂)-哔哩哔哩1、这是跨平台的德州...
揭幕透视!德州局透视脚本,微乐... 揭幕透视!德州局透视脚本,微乐小程序辅助开发,资料教程(有挂存在)-哔哩哔哩1、上手简单,内置详细流...
教你透视!德普之星辅助器app... 教你透视!德普之星辅助器app,陕西微乐小程序辅助,窍要教程(有挂工具)-哔哩哔哩1、德普之星辅助器...
了解透视!wepoker科技辅... 了解透视!wepoker科技辅助器,微乐春天扑克开挂辅助插件,绝活教程(有挂方针)-哔哩哔哩1、不需...
辅助透视!werplan免费挂... 辅助透视!werplan免费挂下载,陕西微乐小程序脚本,手段教程(有挂技巧)-哔哩哔哩进入游戏-大厅...
专业透视!wejoker免费脚... 专业透视!wejoker免费脚本,微信小程序微乐家乡辅助器,经验教程(有挂详细)-哔哩哔哩wejok...
总结透视!wepoker数据分... 总结透视!wepoker数据分析工具,微乐自建房辅助软件有哪些功能,方案教程(有挂细节)-哔哩哔哩1...
普及透视!aapoker插件下... 普及透视!aapoker插件下载,微信小程序微乐辅助器下载,技法教程(有挂透视)-哔哩哔哩aapok...
揭幕透视!wepoker网页版... 揭幕透视!wepoker网页版透视方法,微乐自建房辅助可信吗,技法教程(有挂助手)-哔哩哔哩一、we...
解密透视!来玩app破解版,微... 解密透视!来玩app破解版,微乐小程序怎么挂脚本,绝活儿教程(有挂方法)-哔哩哔哩1、来玩app破解...