是否可以在没有 JQuery 或任何其他附加插件的 Angular 7 中向 mat-table
添加浮动水平滚动条?
我有一个 mat-table
显示 6 列,但也可以通过按下按钮动态添加 100 多列。
但随后布局中断。
HTML部分:
<button (click)="showLess()" mat-stroked-button class="show-button">Show Less</button>
<button (click)="showMore()" mat-stroked-button class="show-button">Show More</button>
<div class="component data component-card">
<mat-card *ngIf="dataSource?.filteredData" class="mat-card">
<mat-paginator [length]="length" [pageSize]="100" [pageSizeOptions]="[100, 250, 500, 1000, 2000]" showFirstLastButtons> </mat-paginator>
<mat-table [dataSource]="dataSource" matSort class="table">
<ng-container class="container" *ngFor="let displayedColumn of displayedColumns" matColumnDef="{{ displayedColumn }}">
<mat-header-cell class="header-cell" *matHeaderCellDef >
<span mat-sort-header class="sort-header">{{ displayedColumn | uppercase }}</span>
<input class="table-input" matInput (keyup)="applyFilter($event.target.value)" (focus)="setupFilter(displayedColumn)" placeholder="Filter"/>
</mat-header-cell>
<mat-cell class="cell" *matCellDef="let item">{{ item[displayedColumn] }}</mat-cell>
</ng-container>
<mat-header-row class="header-row" *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row class="row" *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</mat-card>
CSS部分:
.data {
display: block;
width: 95vw;
overflow: auto;
.table {
width: 100vw;
max-width: 100%;
overflow: auto;
margin-bottom: 10px;
display: table;
border-collapse: collapse;
margin: 0px;
background-color: rgb(238, 238, 238);
}
.row,
.header-row {
display: table-row;
min-height: 36px !important;
}
.cell,
.header-cell {
word-wrap: initial;
display: table-cell;
padding: 0px 5px;
line-break: unset;
width: fit-content;
white-space: nowrap;
overflow: hidden;
vertical-align: middle;
text-align: center;
}
.header-row,
.header-cell {
margin: 0 auto;
min-height: 100px !important;
text-align: center;
vertical-align: middle;
align-self: center;
}
.sort-header {
display: flex;
align-content: center;
text-align: center;
justify-content: center;
font-size: 12pt;
}
.header-cell {
font-weight: bold;
}
}
.mat-card {
min-width: max-content;
max-width: max-content;
}
如果溢出都处于活动状态,那么我必须向下滚动才能水平滚动,但布局保持原样。只有 mat-table 和它周围的 div 会被滚动,并且 mat-table 上方的元素(搜索字段等)会停留在它们应有的位置。一切都停留在屏幕中间。
如果我从“.data”中的 div 中停用溢出,则会出现正常的浏览器滚动条,我不必再向下滚动了。但是 mat-table 在滚动时将屏幕向右扩展,并且在水平滚动时上面的搜索字段将保留在左侧,这对我来说会破坏布局。
我需要的是两个滚动条的组合,这在我眼中将是一个浮动滚动条。我只会滚动垫表,但其余部分保持原位。
有没有办法用 CSS 或 Angular 原生地实现这一点?
演示: https ://stackblitz.com/edit/angular-elm867?file=src%2Fapp%2Fapp.component.scss
如果您单击“显示更多”,则只需在“.data”中注释“溢出:自动”后滚动时查看按钮的行为方式。
原文由 suckerp 发布,翻译遵循 CC BY-SA 4.0 许可协议
这可能会有所帮助: