From 28798b5d949826551740fc893d06e6424b77aa6a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 30 Jan 2017 22:41:14 +0100 Subject: Client: replace simple tables by ng2 smart table component --- client/src/app/shared/rest/index.ts | 1 + client/src/app/shared/rest/rest-data-source.ts | 51 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 client/src/app/shared/rest/rest-data-source.ts (limited to 'client/src/app/shared/rest') diff --git a/client/src/app/shared/rest/index.ts b/client/src/app/shared/rest/index.ts index 3c9509dc7..3cb123c3b 100644 --- a/client/src/app/shared/rest/index.ts +++ b/client/src/app/shared/rest/index.ts @@ -1,3 +1,4 @@ +export * from './rest-data-source'; export * from './rest-extractor.service'; export * from './rest-pagination'; export * from './rest.service'; diff --git a/client/src/app/shared/rest/rest-data-source.ts b/client/src/app/shared/rest/rest-data-source.ts new file mode 100644 index 000000000..847dd7c56 --- /dev/null +++ b/client/src/app/shared/rest/rest-data-source.ts @@ -0,0 +1,51 @@ +import { Http, RequestOptionsArgs, URLSearchParams, } from '@angular/http'; + +import { ServerDataSource } from 'ng2-smart-table'; + +export class RestDataSource extends ServerDataSource { + constructor(http: Http, endpoint: string) { + const options = { + endPoint: endpoint, + sortFieldKey: 'sort', + dataKey: 'data' + } + + super(http, options); + } + + protected extractTotalFromResponse(res) { + const rawData = res.json(); + return rawData ? parseInt(rawData.total): 0; + } + + protected addSortRequestOptions(requestOptions: RequestOptionsArgs) { + let searchParams: URLSearchParams = requestOptions.search; + + if (this.sortConf) { + this.sortConf.forEach((fieldConf) => { + const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''; + + searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field); + }); + } + + return requestOptions; + } + + protected addPagerRequestOptions(requestOptions: RequestOptionsArgs) { + let searchParams: URLSearchParams = requestOptions.search; + + if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) { + const perPage = this.pagingConf['perPage']; + const page = this.pagingConf['page']; + + const start = (page - 1) * perPage; + const count = perPage; + + searchParams.set('start', start.toString()); + searchParams.set('count', count.toString()); + } + + return requestOptions; + } +} -- cgit v1.2.3