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

I am using a server server (Java Spring) with Pager enabled. I load 100 records per page when calling HTTP.

In the angular2 service, it consumes an API call with "? Page = 1 & size = 100" as the initial call, while on the client size pager it displays 10 and moves up to 10 pages, which is good. But I can not download the next piece of data from the server. I checked ServerDataSource and used .setPaging (1,100).

How can I load the next piece of data (2-200) and how can I achieve this. Any hints would 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!

+7
source share
3

-

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

, - :

  public settings: TableSettings = new TableSettings();

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

, , ServerDataSource / /.

enter image description here

+1

I solved this problem with LocalDataSource.

HTML

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

T.S.

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);
      })
  }
}
0
source

Source: https://habr.com/ru/post/1679778/


All Articles