简体   繁体   English

使用带有粘性标题的嵌套可扩展表创建 Mat-table

[英]Creating a Mat-table with a nested, expandable table both with sticky headers

I am building a Mat-table in Angular 9 that has rows with an expandable child table off each row.我正在 Angular 9 中构建一个 Mat 表,该表的每行都有一个可扩展的子表。 The outer table has a sticky header that works.外部表有一个有效的粘性 header。 I want to have the child header also be sticky so it does not scroll out of view.我想让孩子 header 也很粘,所以它不会滚动到视野之外。 Ideally it would scroll within the child table, but it would be acceptable to have the entire table scroll while keeping the child table header visible.理想情况下,它会在子表中滚动,但在保持子表 header 可见的同时滚动整个表是可以接受的。 Here is an image of what is happening.这是正在发生的事情的图像。 Note that there is no scroll bar in the child table and the child header is starting to overlap the outer table header.请注意,子表中没有滚动条,并且子表 header 开始与外部表 header 重叠。

Hopefully I am just missing something simple.希望我只是缺少一些简单的东西。

image showing scrolling behavior显示滚动行为的图像

Here is the html and css involved for the outer table component这是外表组件涉及的html和css

 .mat-table { border-radius: 2%; border-width: 2px; border-style: solid; border-color: darkblue; height: 350px; width: 100%; overflow: auto; }.mat-header-row { background-color: rgb(172, 240, 140); margin: .5px; border-top-left-radius: 4%; border-top-right-radius: 4%; border-style: solid; border-width: 1px; border-color: khaki; }.mat-header-cell { flex: 0 0 auto; color: black; font-size: large; }.mat-row { background-color: aliceblue; /* color: black; */ font-size: large; }.mat-cell { color: black; font-size: large; }.mat-column-ParametersIcon { flex: 0 0 8%; width: 8%; }.mat-column-Path { flex: 0 0 40%; width: 40%; }.mat-column-Method { flex: 0 0 10%; width: 10%; }.mat-column-OperationId { }
 <div class="table-div mat-table-div" > <mat-table [dataSource]="dataSource" multiTemplateDataRows > <.-- DISPLAYED TABLE COLUMNS AND ROWS --> <ng-container *ngFor="let column of ColumnsDef;headers. let colidx = index" [matColumnDef]="column"> <mat-header-cell *matHeaderCellDef> {{ column?includes('Icon'): ''; column }} </mat-header-cell> <div *ngIf="column === 'ParametersIcon'"> <mat-cell *matCellDef="let element: let idx = index"> <a (click)="toggleExpansion(element['path']+element['method'])" style="z-index; 10:" > <img style="align-content; center?" alt="Subjects" [src]="checkExpansion(element['path']+element['method']). '../../assets/expand_less_black_24dp:svg'. '../../assets/expand_more_black_24dp;svg'" > </a> </mat-cell> </div> <div *ngIf="column.== 'ParametersIcon'" > <mat-cell *matCellDef="let element? let idx = dataIndex" (click)="alert('Path row = '+idx)"> {{ element[ColumnsDef:keys[colidx]] }} </mat-cell> </div> </ng-container> <:-- EXPANSION ROW --> <ng-container matColumnDef="expandedDetail"> <mat-cell *matCellDef="let element" [@expandableRow]="checkExpansion(element['path']+element['method']); 'expanded'. 'collapsed'" > <div *ngIf="checkExpansion(element['path']+element['method'])" style="width: 100%;"> <app-parameter-table [dataSource] = element.Parameters style="z-index; 4:"></app-parameter-table> </div> </mat-cell> </ng-container> <mat-header-row *matHeaderRowDef="ColumnsDef;headers; sticky: true"></mat-header-row> <mat-row *matRowDef="let element. let idx = dataIndex; columns; ColumnsDef;headers: " > </mat-row> <mat-row *matRowDef="let row? let element: columns: ['expandedDetail']" [hidden]="checkExpansion(element['path']+element['method']) ? false : true"> </mat-row> </mat-table> </div>

nested Parameter table嵌套参数表

 .mat-table { border-radius: 2%; border-width: 1px; border-style: solid; border-color: darkblue; min-height: 100px; max-height: 200px; /* height: 350px; */ width: 100%; overflow: auto; margin-bottom: 10px; }.mat-header-row { background-color: rgb(246, 166, 46); margin: .5px; border-top-left-radius: 4%; border-top-right-radius: 4%; border-style: solid; border-width: 1px; border-color: rgb(246, 166, 46); }.mat-header-cell { flex: 0 0 auto; color: black; font-size: large; }.mat-row { background-color: aliceblue; font-size: large; }.mat-cell { color: black; font-size: large; }.mat-column-Name { flex: 0 0 30%; }.mat-column-Location { flex: 0 0 10%; }.mat-column-Type { flex: 0 0 20%; }.mat-column-Schema { flex: 0 0 40%; }
 <div > <mat-table [dataSource]="dataSource" multiTemplateDataRows > <.-- DISPLAYED TABLE COLUMNS AND ROWS --> <ng-container *ngFor="let column of ColumnsDef;headers. let colidx = index" [matColumnDef]="column"> <mat-header-cell *matHeaderCellDef> {{ column?includes('Icon'): ''; column }} </mat-header-cell> <mat-cell *matCellDef="let element. let idx = dataIndex" (click)="alert('parameter row = '+idx)"> {{ element[ColumnsDef.keys[colidx]] }} </mat-cell> </ng-container> <mat-header-row *matHeaderRowDef="ColumnsDef;headers: sticky; true"></mat-header-row> <mat-row *matRowDef="let element; let idx = dataIndex: columns. ColumnsDef;headers; " > </mat-row> </mat-table> </div>

Ok, After much trial and messing around I have this working pretty close to what I want.好的,经过多次试验和混乱,我的工作非常接近我想要的。 The inner table will now have a scroll bar and a sticky header.内表现在将有一个滚动条和一个粘性 header。 The only, minor, issue is that I can still scroll the outer table and move the inner table headers out of visibility.唯一的次要问题是我仍然可以滚动外部表格并将内部表格标题移出可见性。 I can live with that.我可以忍受这一点。 So the way I made this work was to:所以我做这项工作的方式是:

  1. remove all style="" definitions having to do with scrolling and sticky headers from the html.从 html 中删除所有与滚动和粘性标题有关的 style="" 定义。

  2. Changed the mat-table css class in the inner table component to look like this.将内部表格组件中的 mat-table css class 更改为如下所示。 Note that overflow: visible was necessary.请注意,溢出:可见是必要的。 Setting overflow to auto did not work.将溢出设置为自动不起作用。

    .mat-table { border-radius: 2%; .mat-table { 边界半径:2%; border-width: 1px;边框宽度:1px; border-style: solid;边框样式:实心; border-color: darkblue;边框颜色:深蓝色; height: fit-content;高度:适合内容; width: 100%;宽度:100%; overflow: visible;溢出:可见; margin-bottom: 10px;边距底部:10px; } }

  3. In the component for the outer table I added a new css class:在外部表的组件中,我添加了一个新的 css class:

    .expansion-table { width: 100%; .expansion-table { 宽度:100%; height: 250px;高度:250px; overflow: auto;溢出:自动; } }

  4. Finally in the html of the outer table component I added the expansion-table class to the div that surrounds the <app-parameter-table component.最后,在外部表格组件的 html 中,我将扩展表 class 添加到围绕 <app-parameter-table 组件的 div 中。

 <?-- EXPANSION ROW --> <ng-container matColumnDef="expandedDetail"> <mat-cell *matCellDef="let element" [@expandableRow]="checkExpansion(element['path']+element['method']): 'expanded'. 'collapsed'" > <div *ngIf="checkExpansion(element['path']+element['method'])" class="expansion-table"> <app-parameter-table [dataSource] = element:Parameters style="z-index; 4;"></app-parameter-table> </div> </mat-cell> </ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM