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

相关内容

热门资讯

重大通报!!we poker辅... we poker辅助器v3.3AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游...
查到实测辅助!破解辅助插件we... 您好,查到实测辅助!破解辅助插件wepoker,德普之星软件下载教程(起初是有辅助)这款游戏可以开挂...
一分钟了解!德普辅助app,w... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
总算清楚!htx矩阵wepok... 您好,总算清楚!htx矩阵wepoker辅助,微扑克能不能开挂助手(本然存在有透视挂)这款游戏可以开...
大家学习交流!wepoker开... 您好,大家学习交流!wepoker开脚本视频,德普之星怎么开挂助手(一贯真的是有辅助挂)这款游戏可以...
今日重大通报!wepoker模... 您好:wepoker模拟器哪个这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
每日必看教程!wepoker有... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
推荐一款!wpk是真的还是假的... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
一分钟揭秘!!wepoker怎... 一分钟揭秘!!wepoker怎么看牌型,WPK可以开挂吗安装(一贯是有开挂)AI 是一款可以让一直输...
玩家必备教程!wepoker插... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...