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

相关内容

热门资讯

透视美元局!如何下载wepok... 透视美元局!如何下载wepoker安装包(透视)确实是有辅助平台(有挂教程)-哔哩哔哩1、如何下载w...
透视挂透视!wepoker怎么... 透视挂透视!wepoker怎么开辅助(透视)切实是有辅助安装(有挂助手)-哔哩哔哩1、这是跨平台的w...
透视苹果版!pokemomo辅... 透视苹果版!pokemomo辅助软件(透视)一贯是真的辅助修改器(的确有挂)-哔哩哔哩1、玩家可以在...
随着!约局吧德州真的有透视挂吗... 随着!约局吧德州真的有透视挂吗(透视)一直是有辅助平台(有挂详情)-哔哩哔哩1、打开软件启动之后找到...
经核实!约局吧德州真的存在透视... 经核实!约局吧德州真的存在透视吗(透视)都是有辅助app(有挂方法)-哔哩哔哩1)约局吧德州真的存在...
昨日!steampokerma... 昨日!steampokermaster辅助(透视)本来是真的辅助安装(有挂分析)-哔哩哔哩1、超多福...
透视攻略!wepoker究竟有... 透视攻略!wepoker究竟有没有透视(透视)切实真的有辅助插件(有挂详细)-哔哩哔哩1、该软件可以...
有了最新消息!hhpoker有... 有了最新消息!hhpoker有没有外开挂(透视)确实真的有辅助软件(有挂总结)-哔哩哔哩1、完成hh...
此事引发广泛关注!大菠萝789... 您好,大菠萝789辅助器下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
透视黑科技!wpk俱乐部是做什... 透视黑科技!wpk俱乐部是做什么的(透视)原来是真的辅助app(有挂分析)-哔哩哔哩暗藏猫腻,小编详...