1

我在搜索时遇到了分页问题。分页工作正常,但是当我使用搜索框并输入一些字母时,结果会有所不同。由于结果不同,分页表现不规则。

我正在PaginationModule 使用'ng2-bootstrap'

我正在获取users带有分页的列表.. 这是我的users.component.ts

export class UserListComponent implements OnInit {
     users: IUser[];
     term : string = '';

     public itemsPerPage: number = 1;
     public totalItems: number = 0;
     public currentPage: number = 1;

  ngOnInit() {
    this.getUsers();
  }

  getUsers(){
    this.dataService.getUsers(this.currentPage, this.itemsPerPage)
        .subscribe((users: PaginatedResult<IUser[]>) => {
                this.users = users;
            this.totalItems = users['pagination']['TotalItems'];       
        });
    }

  pageChanged(event: any, term: string): void {
      this.currentPage = event.page;
       if(term != '')
          this.search(term);
       else
        this.getUsers();
  }

  search(term) {
    this.dataService.searchUserList(term, this.currentPage, this.itemsPerPage)
        .subscribe((items : PaginatedResult<IUser[]>) => {
            this.totalItems = items['pagination']['TotalItems'];
            this.users = items;
      });
   }
}

这是users.component.html

     //Search input box
 <input #term (keyup)="search(term.value)" value="" class="form-control" placeholder="Search" autofocus>
     //pagination
 <pagination [boundaryLinks]="true" [totalItems]="totalItems" [itemsPerPage]="itemsPerPage" [(ngModel)]="currentPage" class="pagination-sm"
                previousText="&lsaquo;" nextText="&rsaquo;" firstText="&laquo;" lastText="&raquo;" (pageChanged)="pageChanged($event, term.value)"></pagination>

问题是当我在page:3搜索某些东西时..搜索后我得到的结果只有一页然后我得到了这个错误

ORIGINAL EXCEPTION: Expression has changed after it was checked. Previous value: '3'. Current value: '1'.

我知道这是因为我page:3在搜索结果之后,我只有1 page这就是分页行为不规则的原因。

有什么解决办法吗?

4

0 回答 0