我可以根据屏幕尺寸更改默认的 mat-paginator 尺寸吗?
Can I change the default mat-paginator size depending on screensize?
我有一个带有 mat-table 和 mat-paginator 的 angular 项目,类似于以下内容:
<div class="mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
<td mat-cell *matCellDef="let row"> {{row.id}} </td>
</ng-container>
<ng-container matColumnDef="progress">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Progress </th>
<td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<ng-container matColumnDef="color">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Color </th>
<td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;">
</tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 15, 20, 25, dataSource.data.length]"></mat-paginator>
</div>
也可以在这里找到:Stackblitz Example
如您所见,pageSizeOptions
设置为 [5, 10, 15, 20, 25, dataSource.data.length]
我希望能够根据屏幕大小将此数组中的第一个值(默认值)设置为不同。所以在较小的屏幕上,我将 10 作为默认值,而在较大的屏幕上,我将设置为 15。
这甚至是一个很好的 idea/practice 吗?
您可以使用 onresize 事件:(window:resize)="onResize($event)"
<div class="mat-elevation-z8" (window:resize)="onResize($event)">
<table mat-table [dataSource]="dataSource" matSort>
<!-- ID Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
<td mat-cell *matCellDef="let row"> {{row.id}} </td>
</ng-container>
<!-- Progress Column -->
<ng-container matColumnDef="progress">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Progress </th>
<td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<!-- Color Column -->
<ng-container matColumnDef="color">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Color </th>
<td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;">
</tr>
</table>
<mat-paginator [pageSize]="pageSize" [pageSizeOptions]="[5, 10, 15, 20, 25, dataSource.data.length]"></mat-paginator>
</div>
//你的组件
onResize(event) {
console.log(event.target.innerWidth);
if (event.target.innerWidth > 600 && event.target.innerWidth < 800) {
this.pageSize = 10;
}
else if (event.target.innerWidth > 800 && event.target.innerWidth < 1000) {
this.pageSize = 15;
} else {
this.pageSize = 5;
}
}
默认页面大小应为 5
pageSize = 5;
You can check demo here and bind it according to your requirement of screen.
如果屏幕尺寸小于1440px,最简单的方法如下:
pageSize = window.innerWidth < 1440 ? 9 : 13; //show 9 records or 13 records as default
我有一个带有 mat-table 和 mat-paginator 的 angular 项目,类似于以下内容:
<div class="mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
<td mat-cell *matCellDef="let row"> {{row.id}} </td>
</ng-container>
<ng-container matColumnDef="progress">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Progress </th>
<td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<ng-container matColumnDef="color">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Color </th>
<td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;">
</tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 15, 20, 25, dataSource.data.length]"></mat-paginator>
</div>
也可以在这里找到:Stackblitz Example
如您所见,pageSizeOptions
设置为 [5, 10, 15, 20, 25, dataSource.data.length]
我希望能够根据屏幕大小将此数组中的第一个值(默认值)设置为不同。所以在较小的屏幕上,我将 10 作为默认值,而在较大的屏幕上,我将设置为 15。
这甚至是一个很好的 idea/practice 吗?
您可以使用 onresize 事件:(window:resize)="onResize($event)"
<div class="mat-elevation-z8" (window:resize)="onResize($event)">
<table mat-table [dataSource]="dataSource" matSort>
<!-- ID Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th>
<td mat-cell *matCellDef="let row"> {{row.id}} </td>
</ng-container>
<!-- Progress Column -->
<ng-container matColumnDef="progress">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Progress </th>
<td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<!-- Color Column -->
<ng-container matColumnDef="color">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Color </th>
<td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;">
</tr>
</table>
<mat-paginator [pageSize]="pageSize" [pageSizeOptions]="[5, 10, 15, 20, 25, dataSource.data.length]"></mat-paginator>
</div>
//你的组件
onResize(event) {
console.log(event.target.innerWidth);
if (event.target.innerWidth > 600 && event.target.innerWidth < 800) {
this.pageSize = 10;
}
else if (event.target.innerWidth > 800 && event.target.innerWidth < 1000) {
this.pageSize = 15;
} else {
this.pageSize = 5;
}
}
默认页面大小应为 5
pageSize = 5;
You can check demo here and bind it according to your requirement of screen.
如果屏幕尺寸小于1440px,最简单的方法如下:
pageSize = window.innerWidth < 1440 ? 9 : 13; //show 9 records or 13 records as default