У меня нет проблем с 2 таблицами с одним и тем же paginator, я следую руководству по angular документам и вижу, как он работает:
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;
}
}
html шаблон :
<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 выполнить функции для получения информации, а затем установить paginator:
ngOnInit() {
this.userData();
this.dataSourceUsers.paginator = this.paginator;
this.organizationData()
this.dataSourceOrganizations.paginator = this.paginator;
}