]>
Commit | Line | Data |
---|---|---|
28798b5d C |
1 | import { Http, RequestOptionsArgs, URLSearchParams, } from '@angular/http'; |
2 | ||
3 | import { ServerDataSource } from 'ng2-smart-table'; | |
4 | ||
5 | export class RestDataSource extends ServerDataSource { | |
6 | constructor(http: Http, endpoint: string) { | |
7 | const options = { | |
8 | endPoint: endpoint, | |
9 | sortFieldKey: 'sort', | |
10 | dataKey: 'data' | |
11 | } | |
12 | ||
13 | super(http, options); | |
14 | } | |
15 | ||
16 | protected extractTotalFromResponse(res) { | |
17 | const rawData = res.json(); | |
18 | return rawData ? parseInt(rawData.total): 0; | |
19 | } | |
20 | ||
21 | protected addSortRequestOptions(requestOptions: RequestOptionsArgs) { | |
22 | let searchParams: URLSearchParams = <URLSearchParams> requestOptions.search; | |
23 | ||
24 | if (this.sortConf) { | |
25 | this.sortConf.forEach((fieldConf) => { | |
26 | const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''; | |
27 | ||
28 | searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field); | |
29 | }); | |
30 | } | |
31 | ||
32 | return requestOptions; | |
33 | } | |
34 | ||
35 | protected addPagerRequestOptions(requestOptions: RequestOptionsArgs) { | |
36 | let searchParams: URLSearchParams = <URLSearchParams> requestOptions.search; | |
37 | ||
38 | if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) { | |
39 | const perPage = this.pagingConf['perPage']; | |
40 | const page = this.pagingConf['page']; | |
41 | ||
42 | const start = (page - 1) * perPage; | |
43 | const count = perPage; | |
44 | ||
45 | searchParams.set('start', start.toString()); | |
46 | searchParams.set('count', count.toString()); | |
47 | } | |
48 | ||
49 | return requestOptions; | |
50 | } | |
51 | } |