I am struggling to get the mat-paginator to function correctly as it seems that all the data is being

I'm facing an issue while trying to implement pagination using mat-paginator in Angular for my table. Despite following the instructions from the Angular documentation, the entire table is getting loaded on the first page.

I attempted the following solutions:

setTimeout(() => this.tableDataSource.paginator = this.paginator);

and

ngAfterViewInit() {
    this.tableDataSource.paginator = this.paginator;
}

Below is my component setup:

[...]
displayedColumns: string[] = ['BookTitulo', 'BookAutor', 'BookGenero'];
  data: Book[];
  tableDataSource = new MatTableDataSource<Book>(this.data);
  isLoadingResults = true;

  @ViewChild(MatPaginator) paginator: MatPaginator;

  constructor(private api: ApiService) { }

  ngOnInit() {
    this.api.getBooks()
      .subscribe(res => {
        this.data = res;
        console.log(this.data);
        this.tableDataSource = new MatTableDataSource<Book>(this.data);
        this.isLoadingResults = false;
      }, err => {
        console.log(err);
        this.isLoadingResults = false;
      });

  }
[...]

And here is the table structure:

[...]
<table mat-table [dataSource]="tableDataSource" class="example-table"
           matSort matSortActive="BookTitulo" matSortDisableClear matSortDirection="asc">


      <!-- Book Name Column -->
      <ng-container matColumnDef="BookTitulo">
        <th mat-header-cell *matHeaderCellDef>Titulo</th>
        <td mat-cell *matCellDef="let row">{{row.BookTitulo}}</td>
      </ng-container>

      <!-- Book Author Column -->
      <ng-container matColumnDef="BookAutor">
        <th mat-header-cell *matHeaderCellDef>Autor</th>
        <td mat-cell *matCellDef="let row">{{row.BookAutor}}</td>
      </ng-container>

      <!-- Book Genre Column -->
      <ng-container matColumnDef="BookGenero">
        <th mat-header-cell *matHeaderCellDef>Genero</th>
        <td mat-cell *matCellDef="let row">{{row.BookGenero}}</td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;" [routerLink]="['/book-details/', row._id]"></tr>
    </table>
    <mat-paginator [pageSizeOptions]="[2, 3, 5]" showFirstLastButtons></mat-paginator>
[...]

I have tried various suggestions from other posts without success. Any help or suggestions would be greatly appreciated. Thank you for your time.

Answer №1

Typically, you are unable to include your paginator within the ngAfterViewInit method, as the mat-table has already executed the connect() function. It is recommended to attempt adding it in the ngOnInit if your paginator is static and not impacted by an *ngIf statement.
Best regards

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing an interface in TypeScript and assigning it a value using generics?

I'm struggling to make sense of a type declaration I came across in the react-router library: export interface RouteComponentProps< Params extends { [K in keyof Params]?: string } = {}, C extends StaticContext = StaticContext, S = H.Lo ...

Ways to bypass certificate verification

Every time I make a request from the server to fetch data, I encounter a certificate error. GET https://ip:5001/api/cards/online net::ERR_CERT_COMMON_NAME_INVALID To resolve this issue, I made the decision to bypass certificate verification. Can you provi ...

Creating Instances of Variables Within a Class

Currently, I am working on a project using Ionic and Angular. I have come across various ways of instantiating variables and I'm unsure about the implications of each method. Here are three scenarios that confuse me: export class someClass { myVaria ...

What is the reason behind the lag caused by setTimeout() in my application, while RxJS timer().subscribe(...) does not have the same

I am currently working on a component that "lazy loads" some comments every 100ms. However, I noticed that when I use setTimeout for this task, the performance of my application suffers significantly. Here is a snippet from the component: <div *ngFor ...

Alert: Zone.js has identified that the ZoneAwarePromise '(window|global).Promise' has been replaced with polyfills. Kindly address this issue

Recently, I updated my Angular application from version 8 to 9. After updating the packages and successfully compiling the application, I encountered an error message in the Chrome browser console: Error: Zone.js has detected that ZoneAwarePromise `(wind ...

Unable to use 'ngFor' as it is not recognized as a property of ... in a mixed AngularJS and Angular environment

My AngularJS and Angular hybrid application is giving me trouble when I try to downgrade my Angular test.component. Every time I attempt it, I encounter the following error messages: Can't bind to 'ngFor' since it isn't a known pro ...

Angular Migration - Unable to locate a suitable version

I need to upgrade my application from Angular 10 to Angular 11. However, during the migration process, I encountered the following error: npm ERR! code ETARGET npm ERR! notarget No matching version found for @ruf/<a href="/cdn-cgi/l/email-protection" cl ...

Is there a way to apply the same technique to a dynamic select option in Angular?

My array is dynamic and has an onChange method in the select option. The issue arises when I add a new array and select the new option, as it causes the first array to reset. Here's a snippet of my array structure: <ng-container formGroupName=&qu ...

Can anyone help me with coloring Devanagiri diacritics in ReactJS?

I am currently working on a ReactJS project and I have come across an issue. I would like for the diacritic of a Devanagiri letter to be displayed in a different color than the letter it is attached to. For example: क + ी make की I was wondering ...

The Protractor element appears to be detached from the page's document

Currently, I am facing a challenge while using protractor in my Angular6 Project. The issue revolves around clicking elements within the project. Situation: Within a table row, there is a list of elements that I need to click on individually. Challenge: ...

Tips on ensuring the callback function is properly called when it is passed as an argument to another function

I am facing a challenge with my typescript method that needs to call another method, on(), which requires a callback method. I want the myConnect() method to wait until the callback is executed. I believe this involves using a promise, but I'm struggl ...

Error with SwitchMap on ActivatedRoute.paramMap

When I try to run the ngOnInit method of my component, I encountered an error with the following line of code. this.products$ = this.route.paramMap.switchMap((params: ParamMap) => this.getProductsForType(params.get('type'))); The error mes ...

Is the relevance of Angular 1.x still prevalent in today's development landscape

Considering I have several projects in angular 1.x, I'm contemplating whether it's truly essential and efficient to upgrade them to angular 4 or a later version. The smaller dashboards do not necessarily require an update since they are only use ...

Angular 2: Sending an HTTP GET request with custom headers and parameters

I have been encountering difficulties while attempting to retrieve data from a Stardog triple store into Angular. Despite successfully accessing the service using curl with matching headers and parameters, I am unable to replicate this functionality with ...

Is there a way to trigger a custom event from a Web Component and then intercept it within a React Functional Component for further processing?

I'm facing an issue with dispatching a custom event called "select-date" from a custom web component date picker to a React functional component. Despite testing, the event doesn't seem to be reaching the intended component as expected. Below is ...

The compiler-cli encounters an error when it comes across dynamic declarations

I have encountered a significant project that was designed with the anticipation of transitioning to Angular 2.0 and enabling AoT compilation. After obtaining all the necessary API from Angular, I found that the project worked smoothly in JiT mode with Ang ...

The error message TS2339 in Angular service.component indicates that the property 'subscribe' is not recognized on the type 'Promise<Object>'

Currently, I am in the process of learning Angular by developing a web application for my parish. I have implemented a method for deleting items in the products-list.component.ts file which appears to be technically correct. However, when I attempt to run ...

Signaling the completion of a stream to a BehaviorSubject

When working with Angular 2, I encountered an issue with the mySubject (see code) function. It compiles a complete() function, but an error occurs during execution stating that there is no such function. I tried using onComplete() instead, but it didn&apos ...

Deploying assets in Angular using a specified path address

When deploying Angular to a path other than the root, there is an issue with asset paths: any '/assets' path in templates or style sheets does not get properly prefixed with the deployment path. I am looking to create an IIS rewrite rule that ca ...

Guide on integrating jQuery list filtering in Ionic 2

I stumbled upon a jQuery function that filters data in HTML, found it online. Now, I want to incorporate a similar feature into my Ionic app. However, I am unsure about the modifications required in list-search-min.js. Here is a snippet of my index.html: ...