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、智星德州插件免费脚本咨询教...
6分钟了解!广丰510k辅助插... 6分钟了解!广丰510k辅助插件!真是有辅助脚本(有挂技巧)-哔哩哔哩1、玩家可以在广丰510k辅助...
九分钟了解!威信茶馆app插件... 九分钟了解!威信茶馆app插件!总是有辅助攻略(有挂技术)-哔哩哔哩运威信茶馆app插件辅助工具,进...
2分钟了解!战神辅助众包!好像... 2分钟了解!战神辅助众包!好像存在有辅助脚本(有挂教学)-哔哩哔哩1、上手简单,内置详细流程视频教学...
第三分钟了解!来趣广西辅助!真... 第三分钟了解!来趣广西辅助!真是真的是有辅助方法(详细教程)-哔哩哔哩1、下载好来趣广西辅助透视辅助...
七分钟了解!拱趴大菠萝辅助工具... 七分钟了解!拱趴大菠萝辅助工具下载!一直存在有辅助教程(有挂细节)-哔哩哔哩该软件可以轻松地帮助玩家...
第3分钟了解!九九山城万州版脚... 第3分钟了解!九九山城万州版脚本!切实真的是有辅助插件(详细教程)-哔哩哔哩1、操作简单,无需九九山...
十分钟了解!wepoker辅助... 十分钟了解!wepoker辅助软件价格!真是存在有辅助app(有挂透视)-哔哩哔哩1、下载好wepo...
三分钟了解!川南久久辅助!本来... 三分钟了解!川南久久辅助!本来是真的有辅助插件(有挂助手)-哔哩哔哩1、川南久久辅助辅助器安装包、川...
第三分钟了解!卡五星辅助神器!... 您好,卡五星辅助神器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...