更新pageIndex

时间:2018-06-05 15:48:44

标签: angular pagination angular-material2

我使用Angular Material数据表来显示可添加到的对象列表。将对象添加到数据源时,页面索引会中断并显示负数。我发现修复此问题的唯一方法是将材质分页器的页面索引重置为0.但是,对分页器的任何进一步更改都会导致pageIndex为-1或破坏的分页符。这是一个例子:

我首先在Angular Material数据源中设置新数据,如下所示:

private refreshTable() {
    // first we update the length of the paginator with the new length of the datasource data to ensure parity
    this.paginator.length = this.dataSource.data.length;


    // next we get the current page number so we can return to it later.
    let currentPage = this.paginator.pageIndex;

    this.tempSubscription2 = this.rrService.getSearchResults(this.searchEnv.outputPayload(true, true)).subscribe(
        (data: any) => {
            this.extractSBRs(data);
            this.dataSource.data = [];
            // Here is where the dataSource is reset...
            this.dataSource.data = this.sbrs;

            this.paginator.length = this.dataSource.data.length;
            this.paginator.pageIndex = 0;
            this.dataSource.paginator = this.paginator;
            console.log('made it this far');

            if (this.dataSource.paginator.pageIndex < currentPage) {
                console.log('need to return to the current page');
                this.paginator.pageIndex = currentPage;
                this.paginator._pageIndex = currentPage;
                this.dataSource.paginator.pageIndex = currentPage;
                this.dataSource.paginator._pageIndex = currentPage;

            }
        },
        (err: any) => {
        },
        () => {
        }
    );
}

运行此代码时,第一页的更新很好,因为this.paginator.pageIndex被设置为0.一旦在大于零的页面上添加元素,该表将显示第一页,即分页符pageIndex为-1,UI中的paginator如下所示:

enter image description here

注意负面索引-9 - 0。似乎很少有更新Angular材料数据表的示例,甚至更少有关于如何更新它们影响Angular Material分页器的进一步说明。如何让paginator转到0以外的pageIndex而不会出现负面索引问题?

我已经尝试按照建议here仅更新通过this.paginator抓取的ViewChild()。我还尝试更新您在上面的代码中看到的dataSource.paginator

以下是我的导入:

import {
AfterContentInit, AfterViewInit, ApplicationRef, ChangeDetectorRef, 
Component, OnDestroy, OnInit,
    ViewChild
} from '@angular/core';
import { SearchEnvironment } from "../classes/search-environment";
import { SearchFilterDropdown } from "../classes/search-filter-dropdown";
import { ActivatedRoute, Router } from "@angular/router";
import {MatTableDataSource, MatPaginator, MatDialog, MatTable, MatSnackBar} from "@angular/material";
import { RemoteRetrievalService } from "../core/services/remote-retrieval.service";
import { Observable } from "rxjs/Observable";

如何在不导致paginator.pageIndex值中的负索引或项目范围内负值的错误用户体验的情况下获得返回currentPage索引的所需分页符行为?

1 个答案:

答案 0 :(得分:2)

他们如何构建这些组件是很奇怪的。我不确定我的解决方案是否有效,但根据我在该问题中所阅读的内容,他们似乎无法在同一摘要周期中处理length更改和pageIndex更改。

因此,我建议先更改length,然后再更改pageIndex

 this.paginator.length = this.dataSource.data.length;
 this.paginator.pageIndex = 0;
 this.dataSource.paginator = this.paginator;
 console.log('made it this far');

上面设置了我们知道将对组件起作用的值,下一部分使用window.setTimeout在组件有机会自我更新后运行下一组更改。

 if (this.dataSource.paginator.pageIndex < currentPage) {
     window.setTimeout(()=>{
         this.zone.run(()=>{
            console.log('need to return to the current page');
            this.paginator.pageIndex = currentPage;
            this.paginator._pageIndex = currentPage;
            this.dataSource.paginator.pageIndex = currentPage;
            this.dataSource.paginator._pageIndex = currentPage;
            // EDIT: You must then set the datasource paginator back to the newly edited paginator. 
            this.dataSource.paginator = this.paginator;
         });
     });
 }

您需要注入NgZone以在Angular的区域内运行代码(用于错误处理)。使用window.setTimeout时,这只是一个很好的做法。

请注意,您希望稍后进行服务器端呈现时,window在Angular Universal中不存在,但这是一个不同的问题。