【问题标题】:mat-pagination without mat-table没有垫子的垫子分页
【发布时间】:2020-07-16 09:20:07
【问题描述】:

我一直在尝试对不使用 mat-table 的结果列表实施 mat-pagination。但是,我在这里找到的所有示例都使用 mat-table 和分页。

感谢您对此提供的帮助。

我目前正在 onInit 上从解析器获取所有数据,并且不需要在每次用户与分页交互时从后端获取数据。

component.html

<div class="axi-results-wrapper">
    <mat-accordion>
      <ng-container *ngFor="let process of Processes; let i = index;">
        <mat-expansion-panel (opened)="setOpened(i)"
                             (closed)="setClosed(i)" hideToggle="true">
          <mat-expansion-panel-header [collapsedHeight]="'66px'" [expandedHeight]="'60px'">
            <div *ngIf="currentlyOpenedItemIndex !== i" class="row axi-results-row">
              <span class="col-md-4 axi-results-row__txt">
                <div class = "axi-result-circle axi-result-circle--bg-color">
                  <i class="far fa-copy"></i>
                </div>
                <div class="axi-results-row__txt axi-results-row__txt--primary">{{process.name}}</div>
              </span>
              <span class="col-md-3 axi-results-row__txt axi-results-row__txt--secondary">{{process.repoUrl}}</span>
              <span class="col-md-3 axi-results-row__txt axi-results-row__txt--secondary">{{process.processType}}</span>
              <span class="col-md-2 axi-results-row__txt axi-results-row__icon" *appHasAccess="roleTitle">
                <span (click)="editProcess(process, i); $event.stopPropagation();">
                  <i class="material-icons">edit</i>
                </span>
                <span (click)="deleteProcess(process.id, i); $event.stopPropagation()">
                  <i class="material-icons">delete_outline</i>
                </span>
              </span>
            </div>
            <div *ngIf="currentlyOpenedItemIndex === i">
              <app-result-header [iconType]="type" [headerName]="process.name" (editData)="editProcess(process, i)" (deleteFunc)="deleteProcess(process.id, i)"></app-result-header>
            </div>
          </mat-expansion-panel-header>
          <app-process-detail-view [process]="process"></app-process-detail-view>
        </mat-expansion-panel>
      </ng-container>
    </mat-accordion>

    <mat-paginator [length]="length"
    [pageIndex]="pageIndex"
    [pageSize]="pageSize"
    [pageSizeOptions]="[5, 10, 25, 100]">
    </mat-paginator>
  </div>

组件.ts

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

  constructor(private actr: ActivatedRoute) { }

  ngOnInit() {
    this.getProcessService();
  }

  getProcessService() {
    // Getting all the data fetched from the active route
    this.actr.data.subscribe((res) => {
      this.Processes = res.process;
    });
  }

【问题讨论】:

    标签: angular angular-material mat-pagination


    【解决方案1】:

    mat-paginator 与其他组件一样,具有inputsoutputs。您使用[name of property] 控制输入和输出(name of output)="yourFunction($event)"

    docs

    所以你可以使用,例如

    <mat-paginator [length]="length"
                   [pageSize]="pageSize"
                   [pageSizeOptions]="pageSizeOptions"
                   (page)="change($event)">
    </mat-paginator>
    
    change(event:PageEvent)
    {
        //make something
        console.log(event.length);
        console.log(event.pageIndex);
        console.log(event.pageSize);
        console.log(event.previousPageIndex)
        //possible call to a function in a service to get data from one index to another one
        this.obs$=this.data.service.getItems(event.previousPageIndex*event.pageSize,
               event.PageIndex*event.pageSize               
        )
    }
    

    【讨论】:

    • 这让我大开眼界。我不想在更改函数中使用服务调用,因为我已经拥有了所有数据。相反,我使用 array.slice 函数来获取我想要的特定页面的数据。
    • 没错,如果您已经拥有所有数据,请使用切片-我读得太快了您的问题-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2021-11-09
    • 2023-03-05
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多