export class OrderService {
. . .
public execute(state: DataStateChangeEventArgs): Promise<DataResult> {
return this.getData(state);
}
private getData(state: DataStateChangeEventArgs): Promise<DataResult> {
const pageQuery = `$skip=${state.skip}&$top=${state.take}`;
let sortQuery: string = '';
let filterQuery: String = '';
if (state.where) { // here you can prepare the filter query
filterQuery = `&$filter=` + state.where.map((obj:any) => {
return (obj).predicates.map((predicate:any) => {
return predicate.operator === 'equal' ? `${predicate.field} eq ${predicate.value}` : `${predicate.operator}(tolower(${predicate.field}),'${predicate.value}')`;
}).reverse().join(' and ');
});
}
this.ajax.url = `${this.BASE_URL}?${pageQuery}${sortQuery}${filterQuery}&$inlinecount=allpages&$format=json`;
return this.ajax.send().then((response: any) => {
const data: any = JSON.parse(response);
return {
count: parseInt(getValue('d.__count', data), 10),
result: getValue('d.results', data)
};
});
}
};
|
import { Injectable } from '@angular/core';
@Injectable()
export class OrdersService extends Subject<DataStateChangeEventArgs> {
public getData(state: DataStateChangeEventArgs): Observable<DataStateChangeEventArgs> {
. . .
if (state.where) {
// here you can prepare the filter query as per your requirment
}
return this.http
.get(`${this.BASE_URL}?${pageQuery}${sortQuery}${filterQuery}&$count=true`)
.pipe(map((response: any) => response.json()))
.pipe(map((response: any) => {
return state.dataSource === undefined ? (<DataResult>{
result: response['value'],
count: parseInt(response['@odata.count'], 10),
}) : response['value'];
}))
.pipe(map((data: any) => data));
}
}
|
public dataStateChange(state: DataStateChangeEventArgs): void {
if ((<any>state.action).requestType === 'filterchoicerequest' ||(<any>state.action).requestType=== 'filtersearchbegin') {
this.service.getData(state).subscribe((e) => state.dataSource(e));
} else {
this.service.execute(state);
}
}
|