Commit 7252ae3a5df05ab664429536a1982745357a9c48

Authored by Yarik
1 parent a04ea953

Filters

src/models/statement.base.ts
@@ -2,6 +2,8 @@ import { AfterViewInit, OnInit } from '@angular/core'; @@ -2,6 +2,8 @@ import { AfterViewInit, OnInit } from '@angular/core';
2 import { TdLoadingService } from '@covalent/core'; 2 import { TdLoadingService } from '@covalent/core';
3 import { GridOptions, IGetRowsParams, IRowModel, RowNode } from 'ag-grid/main'; 3 import { GridOptions, IGetRowsParams, IRowModel, RowNode } from 'ag-grid/main';
4 import { StatementBaseService } from '../services/statement.base.service'; 4 import { StatementBaseService } from '../services/statement.base.service';
  5 +import { Subject } from 'rxjs/Subject';
  6 +import { Observable } from 'rxjs/Observable';
5 7
6 export abstract class StatementBase implements AfterViewInit, OnInit { 8 export abstract class StatementBase implements AfterViewInit, OnInit {
7 protected columnDefs: any[]; 9 protected columnDefs: any[];
@@ -62,16 +64,34 @@ export abstract class StatementBase implements AfterViewInit, OnInit { @@ -62,16 +64,34 @@ export abstract class StatementBase implements AfterViewInit, OnInit {
62 this.isBootstrapping = false; 64 this.isBootstrapping = false;
63 } 65 }
64 66
65 - protected setRowData(): {} { 67 +// protected setRowData2(): {} {
  68 +// let dataSource: {} = {
  69 +// rowCount: null,
  70 +// getRows: (params: IGetRowsParams) => {
  71 +// let sort: string = null;
  72 +// if (params.sortModel.length) {
  73 +// sort = this.parseSort(params.sortModel[0]);
  74 +// }
  75 +// this.service.getData(params.startRow, params.endRow, sort).then((data: any) => {
  76 +// if (!data.length) {
  77 +// data = [this.createModel()];
  78 +// }
  79 +// let lastRow: number = -1;
  80 +// if (data.length <= params.endRow) {
  81 +// lastRow = data.length;
  82 +// }
  83 +// params.successCallback(data, lastRow);
  84 +// });
  85 +// },
  86 +// };
  87 +// return dataSource;
  88 +// }
  89 +
  90 + protected setRowData(): {} {
66 let dataSource: {} = { 91 let dataSource: {} = {
67 rowCount: null, 92 rowCount: null,
68 getRows: (params: IGetRowsParams) => { 93 getRows: (params: IGetRowsParams) => {
69 - console.log(params);  
70 - let sort: string = null;  
71 - if (params.sortModel.length) {  
72 - sort = this.parseSort(params.sortModel[0]);  
73 - }  
74 - this.service.getData(params.startRow, params.endRow, sort).then((data: any) => { 94 + this.service.getData(params).subscribe((data: any[]) => {
75 if (!data.length) { 95 if (!data.length) {
76 data = [this.createModel()]; 96 data = [this.createModel()];
77 } 97 }
src/services/statement.base.service.ts
1 import { Headers, Http, Response } from '@angular/http'; 1 import { Headers, Http, Response } from '@angular/http';
  2 +import { IGetRowsParams } from 'ag-grid/main';
2 3
3 import { Observable } from 'rxjs/Observable'; 4 import { Observable } from 'rxjs/Observable';
4 import 'rxjs/add/operator/toPromise'; 5 import 'rxjs/add/operator/toPromise';
@@ -10,24 +11,36 @@ export abstract class StatementBaseService { @@ -10,24 +11,36 @@ export abstract class StatementBaseService {
10 11
11 constructor(protected http: Http) { } 12 constructor(protected http: Http) { }
12 13
13 - getData(from: number = 0, to: number = 100, sort: string = null): Promise<any[]> {  
14 - let url: string = this.url;  
15 - url += '?from=' + from + '&to=' + to;  
16 - if (sort) { 14 + // getData2(from: number = 0, to: number = 100, sort: string = null): Promise<any[]> {
  15 + // let url: string = this.url;
  16 + // url += '?from=' + from + '&to=' + to;
  17 + // if (sort) {
  18 + // url += '&sort=' + sort;
  19 + // }
  20 + // return this.http.get(url)
  21 + // .toPromise()
  22 + // .then((response: Response) => this.parseModels(response.json()))
  23 + // .catch(this.handleError);
  24 + // }
  25 +
  26 + getData(params: IGetRowsParams): Observable<any[]> {
  27 + let sort: string = null;
  28 + if (params.sortModel.length) {
  29 + sort = this.parseSort(params.sortModel[0]);
  30 + }
  31 + let url: string = this.url;
  32 + url += '?from=' + params.startRow + '&to=' + params.endRow;
  33 + if (sort) {
17 url += '&sort=' + sort; 34 url += '&sort=' + sort;
18 } 35 }
19 - return this.http.get(url)  
20 - .toPromise()  
21 - .then((response: Response) => this.parseModels(response.json()))  
22 - .catch(this.handleError); 36 + let filter: string = this.parseFilter(params.filterModel);
  37 + if (filter) {
  38 + url += '&filter=' + filter;
  39 + }
  40 + return this.http.get(url)
  41 + .map((response: Response) => this.parseModels(response.json()));
23 } 42 }
24 43
25 - // search(term: string): Observable<any[]> {  
26 - // return this.http  
27 - // .get(`app/heroes/?name=${term}`)  
28 - // .map(response => response.json().data as any[]);  
29 - // }  
30 -  
31 update(id: number, data: string): Promise<any> { 44 update(id: number, data: string): Promise<any> {
32 return this.http.post(this.url + '/update?id=' + id, data, { headers: this.headers }) 45 return this.http.post(this.url + '/update?id=' + id, data, { headers: this.headers })
33 .toPromise() 46 .toPromise()
@@ -56,6 +69,52 @@ export abstract class StatementBaseService { @@ -56,6 +69,52 @@ export abstract class StatementBaseService {
56 return Promise.reject(error.message || error); 69 return Promise.reject(error.message || error);
57 } 70 }
58 71
  72 + protected parseSort(sort: any): string {
  73 + if (sort.colId && sort.sort) {
  74 + let result = sort.colId;
  75 + if (sort.sort === 'desc') {
  76 + result = '-' + result;
  77 + }
  78 + return result;
  79 + }
  80 + return null;
  81 + }
  82 +
  83 + protected parseFilter(filter: Object): string {
  84 + let result: string = '';
  85 + for (let property in filter) {
  86 + let value: string = filter[property].filter;
  87 + let operator: string = filter[property].type;
  88 + let symbol: string;
  89 + switch (operator) {
  90 + case 'contains':
  91 + symbol = '*';
  92 + break;
  93 + case 'equals':
  94 + symbol = '=';
  95 + break;
  96 + case 'not equals':
  97 + symbol = '!';
  98 + break;
  99 + case 'starts with':
  100 + symbol = '^';
  101 + break;
  102 + case 'ends with':
  103 + symbol = '$';
  104 + break;
  105 + default:
  106 + }
  107 + if (symbol.length) {
  108 + result += property + symbol + value + ';';
  109 + }
  110 + }
  111 + if (result.length) {
  112 + return result;
  113 + } else {
  114 + return null;
  115 + }
  116 + }
  117 +
59 protected abstract parseModels(json: any): any[]; 118 protected abstract parseModels(json: any): any[];
60 protected abstract parseModel(json: any): any; 119 protected abstract parseModel(json: any): any;
61 } 120 }