Mercurial > hg > ng2-query-ismi
annotate app/query-result-table.component.ts @ 30:193271b6b9d2
configure attributes per result type. select number of items per result page.
author | casties |
---|---|
date | Mon, 01 Feb 2016 17:29:04 +0100 |
parents | 52af480a843e |
children | 4926885f8a99 |
rev | line source |
---|---|
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({ | |
27 | 9 selector: 'query-result', |
24 | 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> |
27 | 15 <pre>{{resultInfo}}</pre> |
29 | 16 <div *ngIf="showTable"><button (click)="showTable=false">hide results</button></div> |
17 <div *ngIf="!showTable"><button (click)="showTable=true">show results</button></div> | |
28
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
18 <div *ngIf="showTable"> |
30
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
19 <div>Show |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
20 <select (change)="onSelectNumItems($event)"> |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
21 <option value="10">10</option> |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
22 <option value="100">100</option> |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
23 <option value="0">all</option> |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
24 </select> |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
25 results per page. |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
26 </div> |
28
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
27 <div *ngIf="config.paging">Page {{page}} of {{numPages}}</div> |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
28 <pagination *ngIf="config.paging" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
29 class="pagination-sm" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
30 [(ngModel)]="page" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
31 [totalItems]="length" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
32 [itemsPerPage]="itemsPerPage" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
33 [maxSize]="maxSize" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
34 [boundaryLinks]="true" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
35 [rotate]="false" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
36 (pageChanged)="onChangeTable(config, $event)" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
37 (numPages)="numPages = $event"> |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
38 </pagination> |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
39 <ngTable |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
40 [config]="config.sorting" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
41 (tableChanged)="onChangeTable(config)" |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
42 [rows]="rows" [columns]="columns"> |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
43 </ngTable> |
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
44 </div> |
24 | 45 </div> |
46 `, | |
27 | 47 inputs: ['queryState', 'resultInfo'], |
25 | 48 directives: [NG_TABLE_DIRECTIVES, PAGINATION_DIRECTIVES] |
24 | 49 }) |
50 | |
51 export class QueryResultTableComponent implements OnInit { | |
52 | |
53 public queryState: QueryState; | |
27 | 54 public resultInfo: string; |
55 | |
28
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
56 public showTable = false; |
25 | 57 public data: Array<any>; |
58 public columns: Array<any>; | |
59 | |
60 public rows: Array<any>; | |
61 | |
62 public page: number = 1; | |
63 public itemsPerPage: number = 10; | |
64 public maxSize: number = 5; | |
65 public numPages: number = 1; | |
66 public length: number = 0; | |
67 | |
68 public config: any = { | |
69 paging: true, | |
70 sorting: {'columns': this.columns}, | |
71 //filtering: {filterString: '', columnName: 'position'} | |
72 }; | |
24 | 73 |
74 ngOnChanges() { | |
75 console.debug("result table changed! queryState=", this.queryState?this.queryState.resultColumns:''); | |
27 | 76 this.data = this.queryState.results; |
77 this.columns = this.queryState.resultColumns; | |
25 | 78 this.config.sorting = this.queryState.resultColumns; |
28
1ceea716600f
result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents:
27
diff
changeset
|
79 this.showTable = (this.data.length < 1000); |
26 | 80 this.onChangeTable(this.config, null); |
24 | 81 } |
82 | |
83 ngOnInit() { | |
84 console.debug("result table init!"); | |
85 } | |
86 | |
30
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
87 onSelectNumItems(event: any) { |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
88 let selected = event.target.value; |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
89 console.debug("selected number of items:", selected); |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
90 let num = parseInt(selected); |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
91 if (num == 0) { |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
92 this.itemsPerPage = this.length; |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
93 } else { |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
94 this.itemsPerPage = num; |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
95 } |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
96 } |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
29
diff
changeset
|
97 |
25 | 98 changePage(page:any, data:Array<any> = this.data):Array<any> { |
99 let start = (page.page - 1) * page.itemsPerPage; | |
100 let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : data.length; | |
101 return data.slice(start, end); | |
102 } | |
103 | |
104 changeSort(data: any, config: any) { | |
105 if (!config.sorting) { | |
106 return data; | |
107 } | |
108 let columns = this.columns.filter(c => c.sort); | |
109 // simple sorting | |
26 | 110 let sorted = data.sort((previous: any, current: any) => { |
25 | 111 for (let i = 0; i < columns.length; i++) { |
112 let sort = columns[i].sort; | |
113 let columnName = columns[i].name; | |
114 if (previous[columnName] > current[columnName]) { | |
115 return sort === 'desc' ? -1 : 1; | |
116 } else if (previous[columnName] < current[columnName]) { | |
117 return sort === 'asc' ? -1 : 1; | |
118 } | |
119 } | |
120 return 0; | |
121 }); | |
26 | 122 return sorted; |
25 | 123 } |
124 | |
125 changeFilter(data: any, config: any): any { | |
126 if (!config.filtering) { | |
127 return data; | |
128 } | |
129 | |
130 let filteredData: Array<any> = data.filter((item: any) => | |
131 item[config.filtering.columnName].match(this.config.filtering.filterString)); | |
132 | |
133 return filteredData; | |
134 } | |
135 | |
136 onChangeTable(config, page: any = config.paging) { | |
26 | 137 console.debug("onChangeTable config=", config); |
25 | 138 if (config.filtering) { |
139 Object.assign(this.config.filtering, config.filtering); | |
140 } | |
141 if (config.sorting) { | |
142 Object.assign(this.config.sorting, config.sorting); | |
143 } | |
144 | |
145 let filteredData = this.changeFilter(this.data, this.config); | |
146 let sortedData = this.changeSort(filteredData, this.config); | |
147 | |
148 this.rows = (page && config.paging) ? this.changePage(page, sortedData) : sortedData; | |
149 this.length = sortedData.length; | |
150 } | |
151 | |
24 | 152 } |