I have an Angular Material Data Table and I am trying to add pagination to it.
I have come across this problem that if the data is an async source then Mat-Paginator doesn't work.
I tried with Sync source and Pagination works as expected but as I have an observable the pagination does not get initialized when the data finally arrives.
However, I have tried searching for this problem and so far no one seems to be having this problem so it may be down to my setup.
Following is my ts file for my component.
import { Component, Input, Output, EventEmitter, style, Inject, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { Router } from '@angular/router';
import { Country} from '../../core/models/country';
import { OnChanges, AfterViewInit } from '@angular/core';
import { MatTableDataSource, MatPaginator } from '@angular/material';
@Component({
// tslint:disable-next-line:component-selector
selector: 'app-country-list',
templateUrl: './country-preview-list.html',
styleUrls : ['./country-preview-list.css']
})
export class CountryListComponent implements OnChanges, AfterViewInit {
@Input() public countries: Country[];
displayedColumns = ['Name', 'Code'];
dataSource: MatTableDataSource<Country>;
@ViewChild(MatPaginator) paginator: MatPaginator;
ngOnChanges(): void {
this.dataSource = new MatTableDataSource<Country>(this.countries);
}
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
}
and Parent component html has this line to add my component
<app-country-list [countries]="countries$ | async"></app-country-list>
Edit
Parent Component TS file
import { Component, ChangeDetectionStrategy, OnChanges } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { take } from 'rxjs/operators';
import * as fromCountries from '../../state/countries/reducers';
import * as fromConfiguration from '../../state/configuration/reducers';
import * as country from '../../state/countries/actions/countries';
import * as configuration from '../../state/configuration/actions/configuration';
import { Country} from '../../core/models/country';
import { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';
@Component({
selector: 'app-countries-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<app-country-search (searchCountries)="search($event)"></app-country-search>
<app-country-list [countries]="countries$ | async"></app-country-list>
`,
})
export class CountriesPageComponent {
countries$: Observable<Country[]>;
constructor(private store: Store<fromCountries.State>) {
this.countries$ = store.pipe(select(fromCountries.getAllCountries));
}
search(query: any) {
this.store.dispatch(new country.Search(query));
}
}
if change countries$ | async to a synchronous list of countries; paginator works.
Can someone please help. Thanks
countries$in the component. Is this all the code you have? - R. Richardsthis.dataSource.paginator = this.paginator;to the end ofngOnChanges()? Since you create a new datasource, itspaginatorproperty is reset. You could also try simply setting the datasource's data (usingthis.dataSource.data) directly. - Jetocountries$is defined in the parent component's script. Having the complete code (or even better, a StackBlitz) would definitely help though. - Jeto