【问题标题】:Mat-Paginator not working on api based mat-tableMat-Paginator 不适用于基于 api 的 mat-table
【发布时间】:2018-05-25 15:14:54
【问题描述】:

我正在实现一个从 API 检索信息的 mat-table。

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>

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    如果你从 API 获取数据,那么任务是异步的意味着即使没有完成 API 请求它也会调用下一步,如果你应用分页器没有数据,数据源中没有数据绝对分页器不会工作。如果你 console.log(this.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)=>{}
           );
        }
    

    // 注意始终保持&lt;mat-paginator&gt; after &lt;mat-table&gt;

    【讨论】:

    • 谢谢,这是唯一对我有用的东西
    【解决方案2】:

    使用下面一行

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

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

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

    这样就可以了。

    【讨论】:

    • 这确实有效!非常感谢,这意味着我们必须告诉分页我们正在使用动态数据?
    【解决方案3】:

    在经历了很多头痛之后,我确实找出了问题所在。 检查这一行

    this.dataSource = this.projects;
    

    并改变它

    this.dataSource.data = this.projects;
    

    它应该可以解决您的问题。还有,ngAfterViewInit是可以的,不用改了。

    【讨论】:

      【解决方案4】:

      试试这个:从 ngAfterViewInit() 中删除代码;

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

      并将其添加到 ngOnInit();

      【讨论】:

        【解决方案5】:

        请尝试如下初始化 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;
          }
        

        上面的代码检查分页器和排序变量,并在它们未定义时设置。 也无需在 HTML 文件中添加#paginator。

        感谢和问候, 基肖尔·库马尔。克

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-19
          • 1970-01-01
          • 2019-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-11
          • 2020-02-23
          相关资源
          最近更新 更多