1 import { Http, RequestOptionsArgs, URLSearchParams, } from '@angular/http';
3 import { ServerDataSource } from 'ng2-smart-table';
5 export class RestDataSource extends ServerDataSource {
6 constructor(http: Http, endpoint: string) {
16 protected extractTotalFromResponse(res) {
17 const rawData = res.json();
18 return rawData ? parseInt(rawData.total): 0;
21 protected addSortRequestOptions(requestOptions: RequestOptionsArgs) {
22 let searchParams: URLSearchParams = <URLSearchParams> requestOptions.search;
25 this.sortConf.forEach((fieldConf) => {
26 const sortPrefix = fieldConf.direction === 'desc' ? '-' : '';
28 searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field);
32 return requestOptions;
35 protected addPagerRequestOptions(requestOptions: RequestOptionsArgs) {
36 let searchParams: URLSearchParams = <URLSearchParams> requestOptions.search;
38 if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
39 const perPage = this.pagingConf['perPage'];
40 const page = this.pagingConf['page'];
42 const start = (page - 1) * perPage;
43 const count = perPage;
45 searchParams.set('start', start.toString());
46 searchParams.set('count', count.toString());
49 return requestOptions;