Last active
June 12, 2019 04:00
-
-
Save lucasheight/ca1a1063b3a2c894dbed20d9ddb6811f to your computer and use it in GitHub Desktop.
Maps an Odata Collection to a kendo GridDataResult. Useful when providing Angular-Grid async odata data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { map } from "../../../../node_modules/rxjs/operators"; | |
import { GridDataResult } from "../../../../node_modules/@progress/kendo-angular-grid"; | |
import { OperatorFunction } from "../../../../node_modules/rxjs"; | |
export interface IOdataCollection<T> { | |
"@odata.context"?: string; | |
"@odata.count"?: number | undefined; | |
"value"?: T[]; | |
} | |
/**Maps an array or IOdataCollection to a kendo GridDataResult | |
* @param count {number} overides the count from the array length or odata.count returned from request | |
* @returns OperatorFunction | |
*/ | |
export const gridMap = <T>(count:number = undefined): OperatorFunction<IOdataCollection<T> | T[], GridDataResult> => | |
input$ => input$.pipe(map(m => Array.isArray(m)? <GridDataResult>{ data: m, total:count|| m.length }: <GridDataResult>{ data: m.value, total:count || m["@odata.count"] })) | |
/*Example | |
class example { | |
data$: Observable<GridDataResult>; | |
data2$: Observable<GridDataResult>; | |
constructor(private http: HttpClient) { | |
this.data$ = this.http.get<IOdataCollection<any>>("https://services.odata.org/TripPinRESTierService/People?$count=true") | |
.pipe(gridMap()); | |
this.data2$ = this.http.get<any>("https://services.odata.org/TripPinRESTierService/People") | |
.pipe(map(m=>m.value), gridMap(m.value.length)) | |
} | |
} | |
<kendo-grid [data]="data$ |async"></kendo-grid> | |
<kendo-grid [data]="data2$ |async"></kendo-grid> | |
Demo | |
======= | |
https://stackblitz.com/edit/angular-kendo-grid-gridmap | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment