Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular material pagination and sorting issue

I have been looking for a solution to this issue but unfortunately couldn't make it work. I followed the official angular material guide and wrote the following code, which is supposed to support pagination and sorting of the material table:

export class RecentAnnouncementsComponent implements OnInit, AfterViewInit {

  displayedColumns: string[] = ['created_utc', 'name', 'title'];
  dataSource = new MatTableDataSource<any>();
  items: ItemData[] = [];

  constructor(private firebaseService: FirebaseService) { }

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  ngOnInit(): void {
    
  }

  ngAfterViewInit() {
    this.firebaseService.getItems().subscribe(result => {
      this.items = result;
      this.dataSource.data = this.items;
      this.dataSource.sort = this.sort;
      this.dataSource.paginator = this.paginator;
    });
  }
}

The following error persists:

Property 'paginator' has no initializer and is not definitely assigned in the constructor.

I tried to modify the code according to these answers but nothing worked. At most, I got rid of errors but the pagination and sorting buttons didn't affect the table items.

I would be very thankful for a suggestion to solve this issue.

like image 900
Niko Gamulin Avatar asked Aug 13 '26 00:08

Niko Gamulin


1 Answers

I assume you're working on the latest version of Angular. I do, and I got this working with:

  @ViewChild(MatSort) sort: MatSort = new MatSort();
  @ViewChild(MatPaginator) paginator: MatPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
like image 194
Christine Avatar answered Aug 14 '26 15:08

Christine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!