Ast*_*ius 16 angular-material angular
如何将数据表组件的组件固定在顶部,并将Paginator固定在底部?
这是我的HTML:
<div class="example-container mat-elevation-z8">
<table mat-table #itemsTable [dataSource]="dataSource" class="items-list">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef> Symbol </th>
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="this.componentsDataService.displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: this.componentsDataService.displayedColumns;" (click)="onRowClicked(row)"></tr>
</table>
<mat-paginator [pageSizeOptions]="[50, 100, 250]" showFirstLastButtons></mat-paginator>
</div>
Run Code Online (Sandbox Code Playgroud)
我尝试添加sticky到<tr mat-header-row>从文档,但它不工作.
我的.ts文件中的导入:
import { Component, OnInit, OnChanges, Input, ViewChild } from '@angular/core';
import { TabsDataService } from 'src/app/services/tabs-data.service';
import { ComponentsDataService } from 'src/app/services/components-data.service';
import { MatPaginator, MatTableDataSource, MatTable, MatTableModule } from '@angular/material'
Run Code Online (Sandbox Code Playgroud)
;
小智 44
我在材料网站的示例中发现可以通过向matHeaderRowDef添加sticky来修复标头:
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true">
Run Code Online (Sandbox Code Playgroud)
对于paginator,我向mat-paginator添加了一个类:
<mat-paginator ...
class="mat-paginator-sticky">
Run Code Online (Sandbox Code Playgroud)
基于评论中提供的@frederik 链接中的答案的类
.mat-paginator-sticky {
bottom: 0px;
position: sticky;
z-index: 10;
}
Run Code Online (Sandbox Code Playgroud)
Mor*_*kop 17
根据材料 8.1
对于粘性标题,我们需要给出一个粘性值,如此处的许多答案所示
用于粘性分页器或底部固定分页器。
我们可以使用 max-height 或 height CSS 属性将表格包裹在 div 中,并将 paninator 放在该 div 之外。
Html 代码示例。
<div class="mat-elevation-z2">
<div class="table-container">
<!--- List of column definitions here --->
<table mat-table [dataSource]="dataSource" matSort>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
<mat-paginator [pageSize]="25" [pageSizeOptions]="[10, 15, 25, 100]"></mat-paginator>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
table {
width: 100%;
}
.table-container {
max-height: calc(100vh - 155px); // or height: calc(100vh - 155px); depending on your need change
overflow: auto;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
下面的CSS对我有用:
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
.mat-header-row{
position: sticky;
top: 0;
z-index: 100;
background: white;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21262 次 |
| 最近记录: |