我正在使用Angular和Angular材质8.0.1。在应用程序中,我有一个使用mat-table的表插槽。我想冻结标题和一列,因此我一直在使用sticky关键字。直到最近,对于大多数用户来说,这种方法一直有效,但是意外停止了。我相信粘性标题和列在开始使用Angular Material的sidenav的同一时间就停止工作了。
我阅读了其他文档,但没有发现任何凸显给我的东西。
我发现使粘性标头起作用的一件事是将表放入具有设定高度和自动溢出的div中。奇怪的是,粘性标头仅在使用该表的滚动条时才起作用,而在浏览器中不起作用。我也不想设置表格的高度,因为页面上有多个表格,希望它们占用所需的空间。如果我将高度设置为100%,则粘性页眉将停止工作;我在想,因为表格的滚动条现在不见了。
以下是我以前无济于事的建议:
https://stackoverflow.com/a/52707760/11665680 https://stackoverflow.com/a/52474361/11665680
export class TeamLeaderboardComponent {
columnsToDisplay = ['player', 'gamesPlayed', 'gamesWon', 'gamesLost', 'winningPercentage', 'kills','deaths','assists'];
sort: MatSort;
dataSource: PlayerServiceRecordDataSource;
@Input('dataSource')
set setDataSource(playerServiceRecords: PlayerServiceRecord[]) {
this.dataSource = new PlayerServiceRecordDataSource(playerServiceRecords);
if (this.sort) {
this.dataSource.sort = this.sort;
}
}
@ViewChild(MatSort, {static: true})
set setSort(sort: MatSort) {
this.sort = sort;
}
}
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="player" sticky>
<th class="text" scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>Player</th>
<td class="text" mat-cell *matCellDef="let playerServiceRecord"><a routerLink="/players/{{playerServiceRecord.player.id}}"><img class="small" src="{{playerServiceRecord.player.emblemUrl}}">{{playerServiceRecord.player.gamertag}}</a></td>
</ng-container>
<ng-container matColumnDef="gamesPlayed">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>GP</th>
<td mat-cell *matCellDef="let playerServiceRecord">{{playerServiceRecord.serviceRecord.gamesPlayed}}</td>
</ng-container>
<ng-container matColumnDef="gamesWon">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>W</th>
<td mat-cell *matCellDef="let playerServiceRecord">{{playerServiceRecord.serviceRecord.gamesWon}}</td>
</ng-container>
<ng-container matColumnDef="gamesLost">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>L</th>
<td mat-cell *matCellDef="let playerServiceRecord"> {{playerServiceRecord.serviceRecord.gamesLost}}</td>
</ng-container>
<ng-container matColumnDef="winningPercentage">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>W%</th>
<td mat-cell *matCellDef="let playerServiceRecord"> {{playerServiceRecord.serviceRecord.winningPercentage | percent: '1.1-1'}}</td>
</ng-container>
<ng-container matColumnDef="kills">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>Kills</th>
<td mat-cell *matCellDef="let playerServiceRecord"> {{playerServiceRecord.serviceRecord.kills}}</td>
</ng-container>
<ng-container matColumnDef="deaths">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>Deaths</th>
<td mat-cell *matCellDef="let playerServiceRecord"> {{playerServiceRecord.serviceRecord.deaths}}</td>
</ng-container>
<ng-container matColumnDef="assists">
<th scope="col" mat-header-cell *matHeaderCellDef mat-sort-header>Assists</th>
<td mat-cell *matCellDef="let playerServiceRecord"> {{playerServiceRecord.serviceRecord.assists}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay; sticky: true"></tr>
<tr mat-row *matRowDef="let rowData; columns: columnsToDisplay"></tr>
</table>
<div class="main-container">
<mat-toolbar class="topnav">
<mat-toolbar-row>
<button fxShow="false" fxShow.xs="true" mat-icon-button (click)="sidenav.toggle()">
<mat-icon>menu</mat-icon>
</button>
<mat-nav-list fxLayout="row">
<a mat-list-item routerLink="/">Website Title</a>
<a mat-list-item fxShow="true" fxShow.xs="false" routerLink="/lans">LANs</a>
<a mat-list-item fxShow="true" fxShow.xs="false" routerLink="/players">Players</a>
<a mat-list-item fxShow="true" fxShow.xs="false" routerLink="/leaderboards">Leaderboards</a>
</mat-nav-list>
</mat-toolbar-row>
</mat-toolbar>
<mat-sidenav-container class="sidenav-container">
<mat-sidenav #sidenav fixedInViewport fixedTopGap="56">
<mat-nav-list>
<a mat-list-item routerLink="/lans">LANs</a>
<a mat-list-item routerLink="/players">Players</a>
<a mat-list-item routerLink="/leaderboards">Leaderboards</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<section>
<router-outlet></router-outlet>
</section>
</mat-sidenav-content>
</mat-sidenav-container>
</div>
.main-container {
display: flex;
flex-direction: column;
flex: 1 0 auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.sidenav-container {
flex: 1 0 auto;
}
我希望标题和第一列具有粘性,但不是。
答案 0 :(得分:0)
您是否为sidenav容器设置了一些伸缩样式?还是只使用默认值?尝试在内容包装器中添加一些flex定义,以查看是否为Sticky标头提供了所需的标记。
我也会研究源代码,以便更好地了解它们的方法并在浏览器端搜索已知的冲突或要求