ng2-smart-table with paging from back-end (Spring)

拥有回忆 提交于 2019-12-19 17:36:16

问题


I am using a back-end server (Java Spring) that has Pager enabled. I am loading 100 records per page on a HTTP call.

On angular2 service, it is consuming the API call with "?page=1&size=100" as the initial call whereas on the client size pager, it shows 10 and moves up to 10 page which is fine. But I am unable to load the next chunk of data from server. I have checked ServerDataSource and used .setPaging(1,100).

How can I load the next chunk of data (2-200) and how can I achieve this. Any hints will be helpful.

@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);
}

Thank you!


回答1:


I solved this problem with 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);
      })
  }
}



回答2:


Try to set the settings to the smart-table like this

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

And at your component, define the settings, something like this:

  public settings: TableSettings = new TableSettings();

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



回答3:


Also if you need to customize request to back-end there is configuration parameters for ServerDataSource that is used for data retrieving/pagination/sorting.



来源:https://stackoverflow.com/questions/44669968/ng2-smart-table-with-paging-from-back-end-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!