typescript Mat-Paginator无法在基于API的mat-table上运行

tquggr8v  于 2023-03-13  发布在  TypeScript
关注(0)|答案(6)|浏览(162)

我正在实现一个从API检索信息的mat-table。
垫表显示的数据符合预期。
这台分页机不工作了。
下面是我使用的代码:
组件:

import {DataService} from '../../services/data.service';
import {Component, ViewChild, AfterViewInit, OnInit} from '@angular/core';
import {MatPaginator, MatTableDataSource} from '@angular/material';


@Component({
  selector: 'app-projects',
  templateUrl: './projects.component.html',
  styleUrls: ['./projects.component.css'],

})
export class ProjectsComponent implements OnInit,AfterViewInit {
  projects: any;
  dataSource = new MatTableDataSource();
  displayedColumns: any;
  length: number;
  pageSize: number=1;
  pageSizeOptions = [1, 5, 10, 50];
  @ViewChild(MatPaginator) paginator: MatPaginator;
  constructor(private dataService: DataService) { 

  }


  ngOnInit() {
    console.log("Retrieving projects");
    this.getProjects().then(
      projects => {
        console.log("Projects retrieved", projects);
        this.projects=projects;
        this.dataSource = this.projects;
        console.log(this.dataSource);
        this.displayedColumns = [
          'prj_id',
          'title',
          'priority_id'
        ];
        this.length=this.projects.length;

      }
    ).catch(function (data){
      console.log("Rejected", data);
    });

  }

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

  getProjects(){
    let promise = new Promise((resolve, reject) => {
      this.dataService.getProjects().subscribe(
        projects => {
          resolve(projects);
        },
        error => {
          console.log("error retrieving projects");
          reject("error retrieving projects");
        }
      )
    });
    return promise;
  }

}

export interface Projects {
  prj_id: string;
  title: string;
  priority_id: number;
}

HTML视图:

<div fxLayout="column" fxLayoutAlign="center center" >

    <button mat-raised-button color="primary" ><mat-icon>add</mat-icon>Project</button>

</div>
<div fxLayout="row" fxLayoutWrap="wrap">

  <div fxFlex.gt-sm="100" fxFlex.gt-xs="100" fxFlex="100">
      <mat-card>
          <mat-card-content> 
              <mat-card-title backgroundColor="primary">Projects</mat-card-title>
              <div class="table-rasponsive">
                  <mat-table #table [dataSource]="dataSource">

                  <!-- Position Column -->
                  <ng-container matColumnDef="prj_id">
                    <mat-header-cell *matHeaderCellDef> ID </mat-header-cell>
                    <mat-cell *matCellDef="let element"> {{element.prj_id}} </mat-cell>
                  </ng-container>

                  <!-- Name Column -->
                  <ng-container matColumnDef="title">
                    <mat-header-cell *matHeaderCellDef> TITLE </mat-header-cell>
                    <mat-cell *matCellDef="let element"> {{element.title}} </mat-cell>
                  </ng-container>

                   <!-- Name Column -->
                   <ng-container matColumnDef="priority_id">
                      <mat-header-cell *matHeaderCellDef> PRIORITY </mat-header-cell>
                      <mat-cell *matCellDef="let element"> {{element.priority_id}} </mat-cell>
                    </ng-container>


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

                <mat-paginator  #paginator
                                [length]="length"
                                [pageSize]="pageSize"
                                [pageSizeOptions]="pageSizeOptions">
                </mat-paginator>
              </div>    
          </mat-card-content>
      </mat-card>
  </div>
</div>
bzzcjhmw

bzzcjhmw1#

在经历了很多头痛之后,我确实弄清楚了问题。检查这条线

this.dataSource = this.projects;

并将其更改为

this.dataSource.data = this.projects;

它应该可以解决您的问题。另外,ngAfterViewInit 是可以的,不要更改它。

pepwfjgg

pepwfjgg2#

使用下面的行

this.dataSource = new MatTableDataSource(this.projects);

如果分页器仍然不工作,对于动态加载的表数据,请使用MatPaginator的read参数

@ViewChild(MatPaginator, {read: true}) paginator: MatPaginator;

这个就行了。

deikduxw

deikduxw3#

如果您正在从API获取数据,则该任务是异步的表示即使未完成API请求,它也将调用后续步骤,如果您在没有数据的情况下应用分页器,数据源中将没有数据分页器肯定无法工作。如果您使用console.log(此.paginator.paginator),则将得到undefined,表示您的数据源未初始化,因此您无法将分页器应用于数据源
要使其正常工作,请始终在完成API请求后分别分配数据和分页程序。

dataSource = new MatTableDataSource();
    @ViewChild(MatPaginator) paginator: MatPaginator;
    
    ngOnInit(){
    yourService.getData().subscribe(
        (data)=>{
           this.datasource.data = data;
           this.dataSource.paginator = this.paginator;
                },
        (error)=>{}
       );
    }

//注意始终保持<mat-paginator> after <mat-table>

u5i3ibmn

u5i3ibmn4#

尝试以下操作:删除ngAfterViewInit()中的代码;

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

并添加到ngOnInit()中;

vsikbqxv

vsikbqxv5#

请尝试初始化MATPAGINATOR和MATSORT如下,这为我工作。

public dataSource = new MatTableDataSource([]);
@ViewChild(MatSort) matSort: MatSort;

private paginator: MatPaginator;
private sort: any;

@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
    this.paginator = mp;
    this.setDataSourceAttributes();
  }

  @ViewChild(MatSort) set content(content: ElementRef) {
    this.sort = content;
    if (this.sort) {
      this.dataSource.sort = this.sort;
    }
  }

  setDataSourceAttributes() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

上面的代码检查paginator和sort变量,当它们未定义时将被设置。同样不需要在HTML文件中添加#paginator。
谢谢并问候Kishore Kumar. K

cfh9epnr

cfh9epnr6#

尝试以下操作并从ngAfterInit()中删除代码

@ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
          this.paginator = mp;
          this.dataSource.paginator = this.paginator;
   }

相关问题