24
|
1 import {Component, OnInit} from 'angular2/core';
|
|
2
|
|
3 import {NG_TABLE_DIRECTIVES} from 'ng2-table/ng2-table';
|
25
|
4 import {PAGINATION_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';
|
24
|
5
|
|
6 import {QueryState} from './query-state';
|
|
7
|
|
8 @Component({
|
|
9 selector: 'query-result-table',
|
|
10 template: `
|
|
11 <div *ngIf="queryState && queryState.results">
|
26
|
12 <span>Cypher query:</span>
|
24
|
13 <pre>{{queryState.resultCypherQuery}}</pre>
|
25
|
14 <h2>Query result</h2>
|
|
15 <pre *ngIf="config.paging">{{queryState.resultInfo}}: page {{page}} of {{numPages}}</pre>
|
|
16 <pagination *ngIf="config.paging"
|
|
17 class="pagination-sm"
|
|
18 [(ngModel)]="page"
|
|
19 [totalItems]="length"
|
|
20 [itemsPerPage]="itemsPerPage"
|
|
21 [maxSize]="maxSize"
|
|
22 [boundaryLinks]="true"
|
|
23 [rotate]="false"
|
|
24 (pageChanged)="onChangeTable(config, $event)"
|
|
25 (numPages)="numPages = $event">
|
|
26 </pagination>
|
|
27 <ngTable [config]="config.sorting"
|
|
28 (tableChanged)="onChangeTable(config)"
|
|
29 [rows]="rows" [columns]="columns">
|
24
|
30 </ngTable>
|
|
31 </div>
|
|
32 `,
|
25
|
33 inputs: ['queryState', 'data', 'columns'],
|
|
34 directives: [NG_TABLE_DIRECTIVES, PAGINATION_DIRECTIVES]
|
24
|
35 })
|
|
36
|
|
37 export class QueryResultTableComponent implements OnInit {
|
|
38
|
|
39 public queryState: QueryState;
|
25
|
40 public data: Array<any>;
|
|
41 public columns: Array<any>;
|
|
42
|
|
43 public rows: Array<any>;
|
|
44
|
|
45 public page: number = 1;
|
|
46 public itemsPerPage: number = 10;
|
|
47 public maxSize: number = 5;
|
|
48 public numPages: number = 1;
|
|
49 public length: number = 0;
|
|
50
|
|
51 public config: any = {
|
|
52 paging: true,
|
|
53 sorting: {'columns': this.columns},
|
|
54 //filtering: {filterString: '', columnName: 'position'}
|
|
55 };
|
24
|
56
|
|
57 ngOnChanges() {
|
|
58 console.debug("result table changed! queryState=", this.queryState?this.queryState.resultColumns:'');
|
25
|
59 this.config.sorting = this.queryState.resultColumns;
|
26
|
60 this.onChangeTable(this.config, null);
|
24
|
61 }
|
|
62
|
|
63 ngOnInit() {
|
|
64 console.debug("result table init!");
|
|
65 }
|
|
66
|
25
|
67 changePage(page:any, data:Array<any> = this.data):Array<any> {
|
|
68 let start = (page.page - 1) * page.itemsPerPage;
|
|
69 let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : data.length;
|
|
70 return data.slice(start, end);
|
|
71 }
|
|
72
|
|
73 changeSort(data: any, config: any) {
|
26
|
74 console.debug("changeSort start");
|
25
|
75 if (!config.sorting) {
|
|
76 return data;
|
|
77 }
|
|
78 let columns = this.columns.filter(c => c.sort);
|
|
79 // simple sorting
|
26
|
80 let sorted = data.sort((previous: any, current: any) => {
|
25
|
81 for (let i = 0; i < columns.length; i++) {
|
|
82 let sort = columns[i].sort;
|
|
83 let columnName = columns[i].name;
|
|
84 if (previous[columnName] > current[columnName]) {
|
|
85 return sort === 'desc' ? -1 : 1;
|
|
86 } else if (previous[columnName] < current[columnName]) {
|
|
87 return sort === 'asc' ? -1 : 1;
|
|
88 }
|
|
89 }
|
|
90 return 0;
|
|
91 });
|
26
|
92 console.debug("changeSort done");
|
|
93 return sorted;
|
25
|
94 }
|
|
95
|
|
96 changeFilter(data: any, config: any): any {
|
|
97 if (!config.filtering) {
|
|
98 return data;
|
|
99 }
|
|
100
|
|
101 let filteredData: Array<any> = data.filter((item: any) =>
|
|
102 item[config.filtering.columnName].match(this.config.filtering.filterString));
|
|
103
|
|
104 return filteredData;
|
|
105 }
|
|
106
|
|
107 onChangeTable(config, page: any = config.paging) {
|
26
|
108 console.debug("onChangeTable config=", config);
|
25
|
109 if (config.filtering) {
|
|
110 Object.assign(this.config.filtering, config.filtering);
|
|
111 }
|
|
112 if (config.sorting) {
|
|
113 Object.assign(this.config.sorting, config.sorting);
|
|
114 }
|
|
115
|
|
116 let filteredData = this.changeFilter(this.data, this.config);
|
|
117 let sortedData = this.changeSort(filteredData, this.config);
|
|
118
|
|
119 this.rows = (page && config.paging) ? this.changePage(page, sortedData) : sortedData;
|
|
120 this.length = sortedData.length;
|
|
121 }
|
|
122
|
24
|
123 }
|