ng2-smart-table,后端分页(Spring)

问题描述 投票:7回答:3

我正在使用启用了Pager的后端服务器(Java Spring)。我在HTTP调用上每页加载100条记录。

在angular2服务上,它使用“?page = 1&size = 100”作为初始调用的API调用,而在客户端大小的寻呼机上,它显示10并且最多移动10页,这很好。但我无法从服务器加载下一块数据。我检查了ServerDataSource并使用了.setPaging(1,100)。

如何加载下一个数据块(2-200)以及如何实现此目的。任何提示都会有所帮助。

@Injectable()
export class AmazonService extends ServerDataSource {

constructor(protected http: Http) {
    super(http);
}

public getAmazonInformation(page, size): Observable<Amazon[]> {

    let url = 'http://localhost:8080/plg-amazon?page=1&size=100';
    this.setPaging(1, 100, true);
    if (this.pagingConf && this.pagingConf['page'] && 
       this.pagingConf['perPage']) {
          url += 
       `page=${this.pagingConf['page']}&size=${this.pagingConf['perPage']}`;
}

return this.http.get(url).map(this.extractData).catch(this.handleError);
}

谢谢!

angular web pagination angular2-services ng2-smart-table
3个回答
2
投票

尝试将设置设置为智能表,如下所示

<ng2-smart-table #grid [settings]="settings" ... >

在您的组件中,定义设置,如下所示:

  public settings: TableSettings = new TableSettings();

  ngOnInit(): void {
      ...
    this.settings.pager.display = true;
    this.settings.pager.perPage = 100;
    ...
  }

1
投票

此外,如果您需要自定义后端请求,则会为ServerDataSource配置参数,用于数据检索/分页/排序。

enter image description here


0
投票

我用LocalDataSource解决了这个问题。

HTML

<ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>

TS

source: LocalDataSource = new LocalDataSource();
pageSize = 25;

ngOnInit() {
  this.source.onChanged().subscribe((change) => {
    if (change.action === 'page') {
      this.pageChange(change.paging.page);
    }
  });
}

pageChange(pageIndex) {
  const loadedRecordCount = this.source.count();
  const lastRequestedRecordIndex = pageIndex * this.pageSize;

  if (loadedRecordCount <= lastRequestedRecordIndex) {    
    let myFilter; //This is your filter.
    myFilter.startIndex = loadedRecordCount + 1;
    myFilter.recordCount = this.pageSize + 100; //extra 100 records improves UX.

    this.myService.getData(myFilter) //.toPromise()
      .then(data => {
        if (this.source.count() > 0)
          data.forEach(d => this.source.add(d));
        else
          this.source.load(data);
      })
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.