Angular 如何在一个组件中有多个Mat Paginator实例?
我不得不重新调整存放Mat Table/Mat Paginator的组件,因为我的HTML文件有一个ngIf。熟悉的人会知道Mat Paginator不符合处理Paginator的标准约定,因为数据到达需要时间。在下面,您可以找到当一个表使用mat分页时工作的更新类型脚本 现在,我需要在同一个组件中有两个具有Mat分页的表。然而,我只能得到一个工作。由于我是个新手,我希望有人能帮我指出正确的方向。随着我继续做更多的研究,我将更新我的问题。但据我所知,每个组件只能有一个Mat分页实例 您可以在下面找到工作的Typescript代码:Angular 如何在一个组件中有多个Mat Paginator实例?,angular,typescript,pagination,mat-table,Angular,Typescript,Pagination,Mat Table,我不得不重新调整存放Mat Table/Mat Paginator的组件,因为我的HTML文件有一个ngIf。熟悉的人会知道Mat Paginator不符合处理Paginator的标准约定,因为数据到达需要时间。在下面,您可以找到当一个表使用mat分页时工作的更新类型脚本 现在,我需要在同一个组件中有两个具有Mat分页的表。然而,我只能得到一个工作。由于我是个新手,我希望有人能帮我指出正确的方向。随着我继续做更多的研究,我将更新我的问题。但据我所知,每个组件只能有一个Mat分页实例 您可以在下面
dataSource1 = new MatTableDataSource<any>(this.userData());
dataSource2 = new MatTableDataSource<any>(this.userData2());
private paginator: MatPaginator;
@ViewChild(MatPaginator, { static: false }) set matPaginator(mp: MatPaginator) {
this.paginator = mp;
this.dataSource1.paginator = this.paginator;
}
ngOnInit() {
this.dataSource1.paginator = this.paginator;
}
dataSource1=newmattabledatasource(this.userData());
dataSource2=新MatTableDataSource(this.userData2());
私人分页者:MatPaginator;
@ViewChild(MatPaginator,{static:false})集合MatPaginator(mp:MatPaginator){
this.paginator=mp;
this.dataSource1.paginator=this.paginator;
}
恩戈尼尼特(){
this.dataSource1.paginator=this.paginator;
}
最后,我希望分页对dataSource1和dataSource2起作用。显然,当我在ngOnInit和ViewChild中声明this.dataSource2.paginator=this.paginator时,它会中断这两者的分页。我已经验证了我的HTML是否正常。我对使用相同分页器的两个表没有任何问题,我遵循angular docs指南并看到了它的工作原理: ts文件:
import { Component, OnInit, ViewChild } from '@angular/core';
import {PageEvent, MatPaginator} from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
const ELEMENT_DATA2: PeriodicElement[] = [
{position: 1, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
{position: 2, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 3, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 4, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 5, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 6, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 7, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 8, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 9, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 10, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
];
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'bootstrapDivTextInside';
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
dataSource2 = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA2);
length = 100;
pageSize = 2;
pageSizeOptions: number[] = [5, 10, 25, 100];
pageEvent: PageEvent;
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
constructor() {}
ngOnInit() {
this.dataSource.paginator = this.paginator;
this.dataSource2.paginator = this.paginator;
}
}
请提供您尝试的代码。我发现对多个MatDatasource实例使用相同的分页器没有问题,即使是在同一个组件中?当我尝试声明它时,两个表的分页都会中断。我将在几分钟内提供代码,即使在同一个组件中也是可行的,但是我必须为MatPaginator定义VIewChild的方式又如何呢?那么,def有什么问题吗?数据源不会加载到表中,除非我以这种方式定义VIewChild。我将直接抛出代码,您可以使用该结构创建一个空对象,然后当您承诺解析更新对象P.D:stackblitz被默认抛出到admin文件夹中的唯一组件中
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>
<!-- Symbol Column -->
<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="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<!---*******************************************************************************-->
<table mat-table [dataSource]="dataSource2" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>
<!-- Symbol Column -->
<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="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
(page)="pageEvent = $event">
</mat-paginator>
export class AdminComponent implements OnInit, AfterContentInit {
USER_DATA: UserData[] = [];
ORGANIZATION_DATA: OrganizationData[] = [];
// Table Variables
dataSourceUsers = new MatTableDataSource<UserData[]>(this.USER_DATA);
dataSourceOrganizations = new MatTableDataSource<OrganizationData[]>(this.ORGANIZATION_DATA);
@ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;
ngOnInit() {
this.userData();
this.dataSourceUsers.paginator = this.paginator;
this.organizationData()
this.dataSourceOrganizations.paginator = this.paginator;
}