angular - 带有后端分页的 ng2-smart-table (Spring)

标签 angular web pagination angular2-services ng2-smart-table

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

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

如何加载下一个数据 block (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);
}

谢谢!

最佳答案

我用 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));
          this.source.getAll()
          .then(d => this.source.load(d))
      }
        else
          this.source.load(data);
      })
  }
}

关于angular - 带有后端分页的 ng2-smart-table (Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44669968/

相关文章:

web - 有哪些工具可以切片 PSD?

c# - 使用 nhibernate 和 mvc 进行分页

jquery - 如何在jquery数据表中隐藏第一页的上一页和最后一页的下一页

jquery - 如何使用 jQuery 将多个数据变量保存到一个 cookie 中

javascript - 浏览器如何呈现 Canvas 元素的内容

angular - 如何在 Angular Mat-Select 中动态禁用特定选项

javascript - 让选择、可拖动和内容可编辑一起工作

angular - 如何对组件进行单元测试以检查特定组件是否呈现

c# - 从属性中获取属性的名称

angular - 如何在 typescript 类中将 mat-lines-button 更改为 mat-button ?