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插件,微乐游戏小程序脚本,积累教程(有挂方略)-哔哩哔哩1、上手简单,内置...
辅助透视!wejoker手机版... 辅助透视!wejoker手机版透视脚本,微乐自建房插件免费软件,大纲教程(有挂教程)-哔哩哔哩1、打...
专业透视!佛手大菠萝13道挂哪... 专业透视!佛手大菠萝13道挂哪里,微信小程序微乐自建房脚本下载,演示教程(有挂功能)-哔哩哔哩1、不...
总结透视!wpk免费辅助,微乐... 总结透视!wpk免费辅助,微乐小程序黑科技免费,策略教程(有挂技巧)-哔哩哔哩暗藏猫腻,小编详细说明...
科普透视!wejoker辅助器... 科普透视!wejoker辅助器怎么卖,微信小程序微乐辅助器免费下载,诀窍教程(有挂方法)-哔哩哔哩小...
了解透视!aapoker辅助怎... 了解透视!aapoker辅助怎么用,微乐自建房辅助免费入口,策略教程(有挂解密)-哔哩哔哩1、完成a...
科普透视!哈糖大菠萝辅助器,微... 科普透视!哈糖大菠萝辅助器,微乐广西小程序开挂,模板教程(确实有挂)-哔哩哔哩1、下载好哈糖大菠萝辅...
详细透视!wepoker辅助透... 详细透视!wepoker辅助透视,微乐家乡app下载,指引教程(真实有挂)-哔哩哔哩在进入wepok...
科普透视!wpk作弊是真的吗,... 科普透视!wpk作弊是真的吗,广西微乐小程序辅助器,诀窍教程(有挂辅助)-哔哩哔哩1)wpk作弊是真...
关于透视!pokerworld... 关于透视!pokerworld修改器,微乐自建房脚本入口,方案教程(有挂神器)-哔哩哔哩1、上手简单...