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透视方法,wepoker高级辅助-确实是真的有辅助app(哔哩哔哩)1、进入...
透视开挂!wepoker辅助器... 透视开挂!wepoker辅助器官方,wepoker辅助软件视频-一贯有辅助软件(哔哩哔哩)1、上手简...
透视详情!werplan辅助软... 透视详情!werplan辅助软件,pokemmo脚本辅助下载-其实一直总是有辅助app(哔哩哔哩)1...
透视总结!wepokerplu... 透视总结!wepokerplus透视脚本免费,wepoker怎么拿到好牌-一直真的有辅助软件(哔哩哔...
透视开挂!aapoker脚本,... 透视开挂!aapoker脚本,aapoker发牌逻辑-一直一直总是有辅助工具(哔哩哔哩)该软件可以轻...
透视总结!cloudpoker... 透视总结!cloudpoker外挂,智星德州有脚本吗-真是有辅助方法(哔哩哔哩)1、进入到智星德州有...
透视了解!wepoker代打辅... 透视了解!wepoker代打辅助,wepoker透视脚本-一直真的有辅助攻略(哔哩哔哩)1、epok...
透视关于!wepoker买钻石... 透视关于!wepoker买钻石有用吗,wepoker辅助真的假的-一贯有辅助app(哔哩哔哩)1、辅...
透视普及!htx矩阵wepok... 透视普及!htx矩阵wepoker辅助,We poker辅助器下载-总是是有辅助插件(哔哩哔哩)辅助...
透视普及!hhpoker的辅助... 透视普及!hhpoker的辅助是真的吗,hhpoker德州机器人-本来真的是有辅助技巧(哔哩哔哩)破...