简体   繁体   English

Angular 材料分页器未按预期工作

[英]Angular material paginator not working as intended

Happy new year新年快乐

I am learning angular and I used angular material paginator on my table which gets data from an API. It displays the number of rows and entries correctly on bottom, but all the data is still displayed in single page.我正在学习 angular,我在我的表上使用了 angular 材料分页器,它从 API 获取数据。它在底部正确显示行数和条目数,但所有数据仍显示在单页中。 I have searched and tried a lot of solutions on the inte.net but in vain.我在 inte.net 上搜索并尝试了很多解决方案,但都是徒劳的。

I am using a view password component which is displayed in router-outlet.我正在使用显示在路由器插座中的查看密码组件。

Component HTML组件 HTML

<div class="mat-elevation-z8">
  <table mat-table [dataSource]="passwords">

    <!-- Position Column -->
    <ng-container matColumnDef="S.No">
      <th mat-header-cell *matHeaderCellDef> S.No. </th>
      <td mat-cell *matCellDef="let password"> {{password.website}} </td>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="Website">
      <th mat-header-cell *matHeaderCellDef> Website </th>
      <td mat-cell *matCellDef="let password"> {{password.website}} </td>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="URL">
      <th mat-header-cell *matHeaderCellDef> URL </th>
      <td mat-cell *matCellDef="let password"> {{password.url}} </td>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="Password">
      <th mat-header-cell *matHeaderCellDef> Password </th>
      <td mat-cell *matCellDef="let password"> {{password.password}} </td>
    </ng-container>

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

  <mat-paginator showFirstLastButtons [pageSize]="5" [pageSizeOptions]="[5, 10, 25, 50]">
  </mat-paginator>
</div>

Component ts组件 ts

import { HttpClient } from '@angular/common/http';
import { AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource, MatTableDataSourcePaginator } from '@angular/material/table';
import {MatPaginator} from '@angular/material/paginator';

@Component({
    selector: 'view-passwords',
    templateUrl: './view-passwords.component.html',
    styleUrls: ['./view-passwords.component.css']
})
export class ViewPasswordsComponent{

    url:any= 'http://localhost:8080/showPass';

    passwords:any;

    displayedColumns: string[] = ['S.No', 'Website', 'URL', 'Password'];
    dataSource: any;

    @ViewChild(MatPaginator, {static:false}) paginator !: MatPaginator;

    constructor(private http: HttpClient, private cdr:ChangeDetectorRef){
      this.http.get(this.url)
        .subscribe(Response=>{
          (this.passwords=Response);
          this.cdr.detectChanges();
          this.dataSource= new MatTableDataSource(this.passwords);
          this.dataSource.paginator = this.paginator;
          //console.log(this.dataSource.paginator)
        })
    }

My localhost my webpage我的本地主机我的网页

The bottom paginator is working fine as I can see, but the table displays all data in single page, despite the paginator showing 1-5.如我所见,底部分页器工作正常,但该表在单页中显示所有数据,尽管分页器显示 1-5。 Am I doing anything wrong?我做错了什么吗?

Angular material paginator displaying data in multiple pages, but it displays all data in single page Angular material paginator 在多页中显示数据,但在单页中显示所有数据

I refactored and simplified your typescript-code a bit.我重构并简化了您的打字稿代码。 I think one main-problem was that you pass passwords instead of dataSource to the HTML. Can you try if the following code works?我认为一个主要问题是您将passwords而不是数据源传递给dataSource如果以下代码有效,您可以尝试一下吗?

Component TS :组件 TS

@ViewChild(MatPaginator)
paginator!: MatPaginator;

url = 'http://localhost:8080/showPass';

displayedColumns: string[] = ['S.No', 'Website', 'URL', 'Password'];
dataSource!: MatTableDataSource<User>;

constructor(private http: HttpClient){
    this.http.get(this.url).subscribe(res=>{
        this.dataSource = new MatTableDataSource(res);
        this.dataSource.paginator = this.paginator;
    })
}

Modification needed in Component HTML :组件 HTML需要修改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM