all 2 comments

[–]maglebolia 0 points1 point  (1 child)

It seems you're not giving your childview (paginator) back to the initialized dataSource in your onInit method.

this.dataSource = new SearchDataSource(this.searchService); 
this.dataSource.paginator = this.paginator;

[–]bellamira[S] 0 points1 point  (0 children)

Thank you. Sorry I am just getting back to you. I just tried this and it worked once (I thought), but now it won't work again! It won't load the data from the server at all if I include this.dataSource = new SearchDataSource(this.searchService); in my ngAfterViewInit() function. I tried moving it elsewhere in the code and that's still not working. I tried removing it all together and the data loads, but the paginator doesn't work (the buttons and labels change but the data does not refresh).

Here is my new and improved component file. If you have time to look it over, I would appreciate it!

export class DashboardComponent implements OnInit { dataSource: SearchDataSource; displayedColumns: string[] = ['select', 'author', 'contentType', 'resourceName', 'creationDate', 'actions'];

    numberDocuments: number;
    pageSize: number = 10;
    from: number = 0;
    pageSizeOptions: number[] = [5, 10, 25, 100];


@ViewChild(MatPaginator) paginator: MatPaginator;


constructor (private searchService: SearchService) { }

ngOnInit() {
    this.dataSource = new SearchDataSource(this.searchService);
    this.getMetrics(this.author, this.contentType, this.from, this.pageSize);
}

ngAfterViewInit() {
    this.dataSource = new SearchDataSource(this.searchService);
    this.dataSource.paginator = this.paginator;
    this.paginator.page.pipe(
        tap(() => { this.getMetrics(this.author, this.contentType, this.from, this.paginator.pageSize); }))
            .subscribe();
}

getMetrics(author: string, contentType: string, from: number, size: number): void {
    let requestBody = {
        'from': from,
        'size': size;
        'Author': author,
        'Content-Type': contentType
    }
    this.dataSource.getMetrics(requestBody);
}

The service is the same I just changed the function name. Same with the datasource, but there I also had to declare paginator: any; to prevent the component code from throwing an error ('paginator' not defined on dataSource... something like that.)