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

相关内容

热门资讯

第8分钟了解!情怀宜春辅助!一... 第8分钟了解!情怀宜春辅助!一贯是有辅助神器(有挂教程)-哔哩哔哩1、进入游戏-大厅左侧-新手福利-...
7分钟了解!微信小程序功夫川免... 7分钟了解!微信小程序功夫川免费辅助!一直一直都是有辅助脚本(有挂细节)-哔哩哔哩1、游戏颠覆性的策...
一分钟了解!潮娱乐鱼虾蟹公式辅... 一分钟了解!潮娱乐鱼虾蟹公式辅助软件!真是是有辅助技巧(有挂方略)-哔哩哔哩1、让任何用户在无需潮娱...
第四分钟了解!欢乐卡五星插件!... 第四分钟了解!欢乐卡五星插件!果然是有辅助技巧(有挂解惑)-哔哩哔哩1、下载好欢乐卡五星插件透视辅助...
九分钟了解!哈糖大菠萝万能挂!... 九分钟了解!哈糖大菠萝万能挂!一贯真的是有辅助神器(有挂教学)-哔哩哔哩哈糖大菠萝万能挂透视方法中分...
第8分钟了解!蜀山四川辅助脚本... 第8分钟了解!蜀山四川辅助脚本多少钱!本来是真的有辅助方法(果真有挂)-哔哩哔哩1、起透看视 蜀山四...
第6分钟了解!万能微信小游戏辅... 第6分钟了解!万能微信小游戏辅助器!确实真的有辅助app(有挂技术)-哔哩哔哩1、任何万能微信小游戏...
1分钟了解!潮汕来物局开挂方法... 1分钟了解!潮汕来物局开挂方法!其实存在有辅助神器(果真有挂)-哔哩哔哩1、每一步都需要思考,不同水...
第1分钟了解!佛手十三道破解版... 第1分钟了解!佛手十三道破解版安装!确实一直都是有辅助方法(有挂工具)-哔哩哔哩1、许多玩家不知道佛...
1分钟了解!赣南辅助软件哪个好... 1分钟了解!赣南辅助软件哪个好!果然是有辅助app(有挂猫腻)-哔哩哔哩在进入赣南辅助软件哪个好软件...