Angular MatPaginator not working












8















I have 2 components. Both have mat-table and paginators and pagination is working for one component and not working for the other component though the code is similar. Below is my html:



<div class="mat-elevation-z8">

<mat-table [dataSource]="dataSource" matSort>

<ng-container matColumnDef="col1">
<mat-header-cell *matHeaderCellDef mat-sort-header> Column1 </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.col1}} </mat-cell>
</ng-container>

<ng-container matColumnDef="col2">
<mat-header-cell *matHeaderCellDef mat-sort-header> Column2 </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.col2}} </mat-cell>
</ng-container>

<!-- Different columns goes here -->

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;">
</mat-row>
</mat-table>

<mat-paginator #scheduledOrdersPaginator [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
</div>


And below is my code in component.ts:



dataSource: MatTableDataSource<any>;
displayedColumns = ['col1', 'col2', ... ];
@ViewChild('scheduledOrdersPaginator') paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
ngOnInit(): void {
// Load data
this.dataSource = new MatTableDataSource(somearray);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}


Similar code is working for the other component and table is getting rendered with the pagination properly, no clue what's wrong with this code.



Any help would be really appreciated










share|improve this question



























    8















    I have 2 components. Both have mat-table and paginators and pagination is working for one component and not working for the other component though the code is similar. Below is my html:



    <div class="mat-elevation-z8">

    <mat-table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="col1">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Column1 </mat-header-cell>
    <mat-cell *matCellDef="let row"> {{row.col1}} </mat-cell>
    </ng-container>

    <ng-container matColumnDef="col2">
    <mat-header-cell *matHeaderCellDef mat-sort-header> Column2 </mat-header-cell>
    <mat-cell *matCellDef="let row"> {{row.col2}} </mat-cell>
    </ng-container>

    <!-- Different columns goes here -->

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;">
    </mat-row>
    </mat-table>

    <mat-paginator #scheduledOrdersPaginator [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
    </div>


    And below is my code in component.ts:



    dataSource: MatTableDataSource<any>;
    displayedColumns = ['col1', 'col2', ... ];
    @ViewChild('scheduledOrdersPaginator') paginator: MatPaginator;
    @ViewChild(MatSort) sort: MatSort;
    ngOnInit(): void {
    // Load data
    this.dataSource = new MatTableDataSource(somearray);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
    }


    Similar code is working for the other component and table is getting rendered with the pagination properly, no clue what's wrong with this code.



    Any help would be really appreciated










    share|improve this question

























      8












      8








      8


      1






      I have 2 components. Both have mat-table and paginators and pagination is working for one component and not working for the other component though the code is similar. Below is my html:



      <div class="mat-elevation-z8">

      <mat-table [dataSource]="dataSource" matSort>

      <ng-container matColumnDef="col1">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Column1 </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.col1}} </mat-cell>
      </ng-container>

      <ng-container matColumnDef="col2">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Column2 </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.col2}} </mat-cell>
      </ng-container>

      <!-- Different columns goes here -->

      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns;">
      </mat-row>
      </mat-table>

      <mat-paginator #scheduledOrdersPaginator [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
      </div>


      And below is my code in component.ts:



      dataSource: MatTableDataSource<any>;
      displayedColumns = ['col1', 'col2', ... ];
      @ViewChild('scheduledOrdersPaginator') paginator: MatPaginator;
      @ViewChild(MatSort) sort: MatSort;
      ngOnInit(): void {
      // Load data
      this.dataSource = new MatTableDataSource(somearray);
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
      }


      Similar code is working for the other component and table is getting rendered with the pagination properly, no clue what's wrong with this code.



      Any help would be really appreciated










      share|improve this question














      I have 2 components. Both have mat-table and paginators and pagination is working for one component and not working for the other component though the code is similar. Below is my html:



      <div class="mat-elevation-z8">

      <mat-table [dataSource]="dataSource" matSort>

      <ng-container matColumnDef="col1">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Column1 </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.col1}} </mat-cell>
      </ng-container>

      <ng-container matColumnDef="col2">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Column2 </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row.col2}} </mat-cell>
      </ng-container>

      <!-- Different columns goes here -->

      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns;">
      </mat-row>
      </mat-table>

      <mat-paginator #scheduledOrdersPaginator [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
      </div>


      And below is my code in component.ts:



      dataSource: MatTableDataSource<any>;
      displayedColumns = ['col1', 'col2', ... ];
      @ViewChild('scheduledOrdersPaginator') paginator: MatPaginator;
      @ViewChild(MatSort) sort: MatSort;
      ngOnInit(): void {
      // Load data
      this.dataSource = new MatTableDataSource(somearray);
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
      }


      Similar code is working for the other component and table is getting rendered with the pagination properly, no clue what's wrong with this code.



      Any help would be really appreciated







      angular pagination angular-material






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 14 '18 at 11:21









      Nagendra VarmaNagendra Varma

      4611621




      4611621
























          6 Answers
          6






          active

          oldest

          votes


















          21














          I resolved a similar issue by surrounding the instantiation with a timeout. Try this :



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





          share|improve this answer
























          • Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

            – Nagendra Varma
            Feb 14 '18 at 11:34








          • 1





            It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

            – Nagendra Varma
            Feb 14 '18 at 11:47








          • 2





            Not a solution but a workaround, keep that in mind

            – trichetriche
            Jul 16 '18 at 13:43






          • 1





            angular provides ngAfterViewInit for this

            – pcnate
            Aug 23 '18 at 15:17






          • 2





            Not when you refresh your data source.

            – trichetriche
            Aug 23 '18 at 15:30



















          8














          In my case, the <mat-paginator> element was inside a container that had an *ngIf on it, which did not render until the data loaded asynchronously. This causes this.paginator to be undefined even in ngAfterViewInit. This causes it to silently fail as MatTableDataSource has no problem with you setting paginator to undefined.



          The solution was to move the <mat-paginator> outside of the *ngIf'd container



          Hopefully, this helps someone who is in the same situation as me.






          share|improve this answer
























          • Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

            – TabsNotSpaces
            Dec 7 '18 at 19:33











          • This was the problem in my case. Smart!

            – Mitchapp
            Dec 18 '18 at 14:35



















          7














          Although selected answer works and solves the problem it is still a workaround. This is proper and more elegant way to deal with the problem.



          Try adding AfterViewInit interface to your class, then put this.dataSource.paginator = this.paginator inside ngAfterViewInit() method



              ngAfterViewInit() {
          this.dataSource.paginator = this.paginator
          }


          then you would not have to call workaround setTimeout.






          share|improve this answer





















          • 1





            this is the more appropriate method

            – pcnate
            Aug 23 '18 at 15:16






          • 1





            This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

            – Stack Underflow
            Oct 17 '18 at 17:13



















          0














          To make it work I had to set the paginator after the data was fetched from source



          getVariables() {
          this.activeRoute.params.subscribe(params => {
          if (params['id'] && (params['type'] === Type.CodeList)) {
          this.dataService
          .getItems(this.currentLanguage, params['id'])
          .subscribe((items: any) => {
          this.dataSource.data = this.items;
          this.dataSource.paginator = this.paginator;
          })
          }
          })
          }





          share|improve this answer































            0














              <mat-paginator
            #scheduledOrdersPaginator
            (page)="pageEvent($event)">
            </mat-paginator>
            pageEvent(event){
            //it will run everytime you change paginator
            this.dataSource.paginator = this.paginator;
            }





            share|improve this answer



















            • 2





              You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

              – MikeW
              Nov 7 '18 at 15:21



















            0














            I am quite a beginner in angular and typescript, but after having the same problem (except, that for me sorting didn't work either), what helped was creating a function 'refreshDataScource()' and having it called from ngAfterViewInit() as well as after each time the server responds with new data. In this function I simply refresh the dataSource with the paginator and the sort. Like this:



            refreshDataSource() {
            this.dataSource = new MatTableDataSource(myDataArray);
            this.dataSource.paginator = this.paginator;
            this.dataSource.sort = this.sort;
            }


            It fixed the paginator and the sort. Everything works now perfectly. However I am not sure if it is just a workaround or a real fix.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48785965%2fangular-matpaginator-not-working%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              21














              I resolved a similar issue by surrounding the instantiation with a timeout. Try this :



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





              share|improve this answer
























              • Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

                – Nagendra Varma
                Feb 14 '18 at 11:34








              • 1





                It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

                – Nagendra Varma
                Feb 14 '18 at 11:47








              • 2





                Not a solution but a workaround, keep that in mind

                – trichetriche
                Jul 16 '18 at 13:43






              • 1





                angular provides ngAfterViewInit for this

                – pcnate
                Aug 23 '18 at 15:17






              • 2





                Not when you refresh your data source.

                – trichetriche
                Aug 23 '18 at 15:30
















              21














              I resolved a similar issue by surrounding the instantiation with a timeout. Try this :



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





              share|improve this answer
























              • Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

                – Nagendra Varma
                Feb 14 '18 at 11:34








              • 1





                It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

                – Nagendra Varma
                Feb 14 '18 at 11:47








              • 2





                Not a solution but a workaround, keep that in mind

                – trichetriche
                Jul 16 '18 at 13:43






              • 1





                angular provides ngAfterViewInit for this

                – pcnate
                Aug 23 '18 at 15:17






              • 2





                Not when you refresh your data source.

                – trichetriche
                Aug 23 '18 at 15:30














              21












              21








              21







              I resolved a similar issue by surrounding the instantiation with a timeout. Try this :



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





              share|improve this answer













              I resolved a similar issue by surrounding the instantiation with a timeout. Try this :



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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 14 '18 at 11:29









              trichetrichetrichetriche

              26.2k42153




              26.2k42153













              • Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

                – Nagendra Varma
                Feb 14 '18 at 11:34








              • 1





                It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

                – Nagendra Varma
                Feb 14 '18 at 11:47








              • 2





                Not a solution but a workaround, keep that in mind

                – trichetriche
                Jul 16 '18 at 13:43






              • 1





                angular provides ngAfterViewInit for this

                – pcnate
                Aug 23 '18 at 15:17






              • 2





                Not when you refresh your data source.

                – trichetriche
                Aug 23 '18 at 15:30



















              • Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

                – Nagendra Varma
                Feb 14 '18 at 11:34








              • 1





                It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

                – Nagendra Varma
                Feb 14 '18 at 11:47








              • 2





                Not a solution but a workaround, keep that in mind

                – trichetriche
                Jul 16 '18 at 13:43






              • 1





                angular provides ngAfterViewInit for this

                – pcnate
                Aug 23 '18 at 15:17






              • 2





                Not when you refresh your data source.

                – trichetriche
                Aug 23 '18 at 15:30

















              Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

              – Nagendra Varma
              Feb 14 '18 at 11:34







              Ideally, I'm loading getting data from a server and this code is already wrapped into setTimeout function: this.http.get(url).subscribe(data => { setTimeout(() => { this.data= data as any; this.dataSource = new MatTableDataSource(this.data); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; }); }

              – Nagendra Varma
              Feb 14 '18 at 11:34






              1




              1





              It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

              – Nagendra Varma
              Feb 14 '18 at 11:47







              It did :) Crazy!! Thanks @trichetriche, but may I please know why is this strange behavior

              – Nagendra Varma
              Feb 14 '18 at 11:47






              2




              2





              Not a solution but a workaround, keep that in mind

              – trichetriche
              Jul 16 '18 at 13:43





              Not a solution but a workaround, keep that in mind

              – trichetriche
              Jul 16 '18 at 13:43




              1




              1





              angular provides ngAfterViewInit for this

              – pcnate
              Aug 23 '18 at 15:17





              angular provides ngAfterViewInit for this

              – pcnate
              Aug 23 '18 at 15:17




              2




              2





              Not when you refresh your data source.

              – trichetriche
              Aug 23 '18 at 15:30





              Not when you refresh your data source.

              – trichetriche
              Aug 23 '18 at 15:30













              8














              In my case, the <mat-paginator> element was inside a container that had an *ngIf on it, which did not render until the data loaded asynchronously. This causes this.paginator to be undefined even in ngAfterViewInit. This causes it to silently fail as MatTableDataSource has no problem with you setting paginator to undefined.



              The solution was to move the <mat-paginator> outside of the *ngIf'd container



              Hopefully, this helps someone who is in the same situation as me.






              share|improve this answer
























              • Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

                – TabsNotSpaces
                Dec 7 '18 at 19:33











              • This was the problem in my case. Smart!

                – Mitchapp
                Dec 18 '18 at 14:35
















              8














              In my case, the <mat-paginator> element was inside a container that had an *ngIf on it, which did not render until the data loaded asynchronously. This causes this.paginator to be undefined even in ngAfterViewInit. This causes it to silently fail as MatTableDataSource has no problem with you setting paginator to undefined.



              The solution was to move the <mat-paginator> outside of the *ngIf'd container



              Hopefully, this helps someone who is in the same situation as me.






              share|improve this answer
























              • Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

                – TabsNotSpaces
                Dec 7 '18 at 19:33











              • This was the problem in my case. Smart!

                – Mitchapp
                Dec 18 '18 at 14:35














              8












              8








              8







              In my case, the <mat-paginator> element was inside a container that had an *ngIf on it, which did not render until the data loaded asynchronously. This causes this.paginator to be undefined even in ngAfterViewInit. This causes it to silently fail as MatTableDataSource has no problem with you setting paginator to undefined.



              The solution was to move the <mat-paginator> outside of the *ngIf'd container



              Hopefully, this helps someone who is in the same situation as me.






              share|improve this answer













              In my case, the <mat-paginator> element was inside a container that had an *ngIf on it, which did not render until the data loaded asynchronously. This causes this.paginator to be undefined even in ngAfterViewInit. This causes it to silently fail as MatTableDataSource has no problem with you setting paginator to undefined.



              The solution was to move the <mat-paginator> outside of the *ngIf'd container



              Hopefully, this helps someone who is in the same situation as me.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 26 '18 at 20:08









              Skylar BrownSkylar Brown

              48254




              48254













              • Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

                – TabsNotSpaces
                Dec 7 '18 at 19:33











              • This was the problem in my case. Smart!

                – Mitchapp
                Dec 18 '18 at 14:35



















              • Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

                – TabsNotSpaces
                Dec 7 '18 at 19:33











              • This was the problem in my case. Smart!

                – Mitchapp
                Dec 18 '18 at 14:35

















              Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

              – TabsNotSpaces
              Dec 7 '18 at 19:33





              Thank you - this was the solution for me. I opted to use [hidden] instead of ngIf so that the paginator would render even if there wasn't any data, but wouldn't show up to the user

              – TabsNotSpaces
              Dec 7 '18 at 19:33













              This was the problem in my case. Smart!

              – Mitchapp
              Dec 18 '18 at 14:35





              This was the problem in my case. Smart!

              – Mitchapp
              Dec 18 '18 at 14:35











              7














              Although selected answer works and solves the problem it is still a workaround. This is proper and more elegant way to deal with the problem.



              Try adding AfterViewInit interface to your class, then put this.dataSource.paginator = this.paginator inside ngAfterViewInit() method



                  ngAfterViewInit() {
              this.dataSource.paginator = this.paginator
              }


              then you would not have to call workaround setTimeout.






              share|improve this answer





















              • 1





                this is the more appropriate method

                – pcnate
                Aug 23 '18 at 15:16






              • 1





                This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

                – Stack Underflow
                Oct 17 '18 at 17:13
















              7














              Although selected answer works and solves the problem it is still a workaround. This is proper and more elegant way to deal with the problem.



              Try adding AfterViewInit interface to your class, then put this.dataSource.paginator = this.paginator inside ngAfterViewInit() method



                  ngAfterViewInit() {
              this.dataSource.paginator = this.paginator
              }


              then you would not have to call workaround setTimeout.






              share|improve this answer





















              • 1





                this is the more appropriate method

                – pcnate
                Aug 23 '18 at 15:16






              • 1





                This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

                – Stack Underflow
                Oct 17 '18 at 17:13














              7












              7








              7







              Although selected answer works and solves the problem it is still a workaround. This is proper and more elegant way to deal with the problem.



              Try adding AfterViewInit interface to your class, then put this.dataSource.paginator = this.paginator inside ngAfterViewInit() method



                  ngAfterViewInit() {
              this.dataSource.paginator = this.paginator
              }


              then you would not have to call workaround setTimeout.






              share|improve this answer















              Although selected answer works and solves the problem it is still a workaround. This is proper and more elegant way to deal with the problem.



              Try adding AfterViewInit interface to your class, then put this.dataSource.paginator = this.paginator inside ngAfterViewInit() method



                  ngAfterViewInit() {
              this.dataSource.paginator = this.paginator
              }


              then you would not have to call workaround setTimeout.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 24 '18 at 22:24

























              answered May 24 '18 at 11:48









              hopseyhopsey

              315210




              315210








              • 1





                this is the more appropriate method

                – pcnate
                Aug 23 '18 at 15:16






              • 1





                This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

                – Stack Underflow
                Oct 17 '18 at 17:13














              • 1





                this is the more appropriate method

                – pcnate
                Aug 23 '18 at 15:16






              • 1





                This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

                – Stack Underflow
                Oct 17 '18 at 17:13








              1




              1





              this is the more appropriate method

              – pcnate
              Aug 23 '18 at 15:16





              this is the more appropriate method

              – pcnate
              Aug 23 '18 at 15:16




              1




              1





              This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

              – Stack Underflow
              Oct 17 '18 at 17:13





              This does not work for me. My data source is being loaded after ngAfterViewInit which may be the problem. I tried initializing the data source first and then using ngAfterViewInit to set the paginator but it did not work. The only thing I found that worked was the workaround in the accepted answer.

              – Stack Underflow
              Oct 17 '18 at 17:13











              0














              To make it work I had to set the paginator after the data was fetched from source



              getVariables() {
              this.activeRoute.params.subscribe(params => {
              if (params['id'] && (params['type'] === Type.CodeList)) {
              this.dataService
              .getItems(this.currentLanguage, params['id'])
              .subscribe((items: any) => {
              this.dataSource.data = this.items;
              this.dataSource.paginator = this.paginator;
              })
              }
              })
              }





              share|improve this answer




























                0














                To make it work I had to set the paginator after the data was fetched from source



                getVariables() {
                this.activeRoute.params.subscribe(params => {
                if (params['id'] && (params['type'] === Type.CodeList)) {
                this.dataService
                .getItems(this.currentLanguage, params['id'])
                .subscribe((items: any) => {
                this.dataSource.data = this.items;
                this.dataSource.paginator = this.paginator;
                })
                }
                })
                }





                share|improve this answer


























                  0












                  0








                  0







                  To make it work I had to set the paginator after the data was fetched from source



                  getVariables() {
                  this.activeRoute.params.subscribe(params => {
                  if (params['id'] && (params['type'] === Type.CodeList)) {
                  this.dataService
                  .getItems(this.currentLanguage, params['id'])
                  .subscribe((items: any) => {
                  this.dataSource.data = this.items;
                  this.dataSource.paginator = this.paginator;
                  })
                  }
                  })
                  }





                  share|improve this answer













                  To make it work I had to set the paginator after the data was fetched from source



                  getVariables() {
                  this.activeRoute.params.subscribe(params => {
                  if (params['id'] && (params['type'] === Type.CodeList)) {
                  this.dataService
                  .getItems(this.currentLanguage, params['id'])
                  .subscribe((items: any) => {
                  this.dataSource.data = this.items;
                  this.dataSource.paginator = this.paginator;
                  })
                  }
                  })
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 2 '18 at 9:25









                  HasseHasse

                  763




                  763























                      0














                        <mat-paginator
                      #scheduledOrdersPaginator
                      (page)="pageEvent($event)">
                      </mat-paginator>
                      pageEvent(event){
                      //it will run everytime you change paginator
                      this.dataSource.paginator = this.paginator;
                      }





                      share|improve this answer



















                      • 2





                        You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

                        – MikeW
                        Nov 7 '18 at 15:21
















                      0














                        <mat-paginator
                      #scheduledOrdersPaginator
                      (page)="pageEvent($event)">
                      </mat-paginator>
                      pageEvent(event){
                      //it will run everytime you change paginator
                      this.dataSource.paginator = this.paginator;
                      }





                      share|improve this answer



















                      • 2





                        You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

                        – MikeW
                        Nov 7 '18 at 15:21














                      0












                      0








                      0







                        <mat-paginator
                      #scheduledOrdersPaginator
                      (page)="pageEvent($event)">
                      </mat-paginator>
                      pageEvent(event){
                      //it will run everytime you change paginator
                      this.dataSource.paginator = this.paginator;
                      }





                      share|improve this answer













                        <mat-paginator
                      #scheduledOrdersPaginator
                      (page)="pageEvent($event)">
                      </mat-paginator>
                      pageEvent(event){
                      //it will run everytime you change paginator
                      this.dataSource.paginator = this.paginator;
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 7 '18 at 14:33









                      Muhammad ShaheemMuhammad Shaheem

                      213110




                      213110








                      • 2





                        You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

                        – MikeW
                        Nov 7 '18 at 15:21














                      • 2





                        You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

                        – MikeW
                        Nov 7 '18 at 15:21








                      2




                      2





                      You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

                      – MikeW
                      Nov 7 '18 at 15:21





                      You need some descriptive text about why this is a good solution, and perhaps some comment(s) on the original source.

                      – MikeW
                      Nov 7 '18 at 15:21











                      0














                      I am quite a beginner in angular and typescript, but after having the same problem (except, that for me sorting didn't work either), what helped was creating a function 'refreshDataScource()' and having it called from ngAfterViewInit() as well as after each time the server responds with new data. In this function I simply refresh the dataSource with the paginator and the sort. Like this:



                      refreshDataSource() {
                      this.dataSource = new MatTableDataSource(myDataArray);
                      this.dataSource.paginator = this.paginator;
                      this.dataSource.sort = this.sort;
                      }


                      It fixed the paginator and the sort. Everything works now perfectly. However I am not sure if it is just a workaround or a real fix.






                      share|improve this answer




























                        0














                        I am quite a beginner in angular and typescript, but after having the same problem (except, that for me sorting didn't work either), what helped was creating a function 'refreshDataScource()' and having it called from ngAfterViewInit() as well as after each time the server responds with new data. In this function I simply refresh the dataSource with the paginator and the sort. Like this:



                        refreshDataSource() {
                        this.dataSource = new MatTableDataSource(myDataArray);
                        this.dataSource.paginator = this.paginator;
                        this.dataSource.sort = this.sort;
                        }


                        It fixed the paginator and the sort. Everything works now perfectly. However I am not sure if it is just a workaround or a real fix.






                        share|improve this answer


























                          0












                          0








                          0







                          I am quite a beginner in angular and typescript, but after having the same problem (except, that for me sorting didn't work either), what helped was creating a function 'refreshDataScource()' and having it called from ngAfterViewInit() as well as after each time the server responds with new data. In this function I simply refresh the dataSource with the paginator and the sort. Like this:



                          refreshDataSource() {
                          this.dataSource = new MatTableDataSource(myDataArray);
                          this.dataSource.paginator = this.paginator;
                          this.dataSource.sort = this.sort;
                          }


                          It fixed the paginator and the sort. Everything works now perfectly. However I am not sure if it is just a workaround or a real fix.






                          share|improve this answer













                          I am quite a beginner in angular and typescript, but after having the same problem (except, that for me sorting didn't work either), what helped was creating a function 'refreshDataScource()' and having it called from ngAfterViewInit() as well as after each time the server responds with new data. In this function I simply refresh the dataSource with the paginator and the sort. Like this:



                          refreshDataSource() {
                          this.dataSource = new MatTableDataSource(myDataArray);
                          this.dataSource.paginator = this.paginator;
                          this.dataSource.sort = this.sort;
                          }


                          It fixed the paginator and the sort. Everything works now perfectly. However I am not sure if it is just a workaround or a real fix.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 13:11









                          Gefilte FishGefilte Fish

                          43859




                          43859






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48785965%2fangular-matpaginator-not-working%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Tonle Sap (See)

                              I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                              Guatemaltekische Davis-Cup-Mannschaft