annotate src/app/query-result-table.component.ts @ 54:4341c1b4e3ae ng2-table

cleanup.
author casties
date Thu, 23 Mar 2017 18:57:01 +0100
parents ac4bfbd99638
children d7c947909ab8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45
dc4f0541f04d update to angular2-rc1. mostly working ;-(
casties
parents: 42
diff changeset
1 import {Component, OnInit} from '@angular/core';
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
2
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
3 import {QueryState} from './query-state';
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
4
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
5 @Component({
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
6 selector: 'query-result',
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
7 template: `
33
739eb38ec2a2 more elvis operator :-)
casties
parents: 31
diff changeset
8 <div *ngIf="queryState?.results">
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
9 <span>Cypher query:</span>
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
10 <pre>{{queryState.getQueryText()}}</pre>
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
11 <h2>Query result</h2>
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
12 <pre>{{resultInfo}}</pre>
29
52af480a843e show/hide results buttons.
casties
parents: 28
diff changeset
13 <div *ngIf="showTable"><button (click)="showTable=false">hide results</button></div>
52af480a843e show/hide results buttons.
casties
parents: 28
diff changeset
14 <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
15 <div *ngIf="showTable">
30
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
16 <div>Show
52
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
17 <select name="numItems" (change)="onSelectNumItems($event)">
30
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
18 <option value="10">10</option>
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
19 <option value="100">100</option>
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
20 <option value="0">all</option>
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
21 </select>
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
22 results per page.
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
23 </div>
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
24 <div>
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
25 <form (ngSubmit)="onSelectCols($event)">
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
26 Columns:
54
4341c1b4e3ae cleanup.
casties
parents: 53
diff changeset
27 <span *ngFor="let col of allColumns">
4341c1b4e3ae cleanup.
casties
parents: 53
diff changeset
28 <input type="checkbox" name="col{{col.name}}" value="{{col.name}}" [(ngModel)]="col.show">{{col.name}}
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
29 </span>
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
30 <button type="submit">change columns</button>
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
31 </form>
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
32 </div>
52
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
33 <pagination *ngIf="config.paging" class="pagination-sm"
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
34 [(ngModel)]="currentPage"
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
35 [totalItems]="length"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
36 [itemsPerPage]="itemsPerPage"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
37 [maxSize]="maxSize"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
38 [boundaryLinks]="true"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
39 [rotate]="false"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
40 (pageChanged)="onChangeTable(config, $event)"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
41 (numPages)="numPages = $event">
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
42 </pagination>
45
dc4f0541f04d update to angular2-rc1. mostly working ;-(
casties
parents: 42
diff changeset
43 <ng-table
53
ac4bfbd99638 small change.
casties
parents: 52
diff changeset
44 [config]="config"
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
45 (tableChanged)="onChangeTable($event)"
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
46 [rows]="rows" [columns]="columns">
45
dc4f0541f04d update to angular2-rc1. mostly working ;-(
casties
parents: 42
diff changeset
47 </ng-table>
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
48 </div>
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
49 </div>
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
50 `,
54
4341c1b4e3ae cleanup.
casties
parents: 53
diff changeset
51 inputs: ['queryState', 'resultInfo']
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
52 })
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
53
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
54 export class QueryResultTableComponent implements OnInit {
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
55
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
56 public queryState: QueryState;
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
57 public resultInfo: string;
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
58
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
59 public showTable = false;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
60 public data: Array<any>;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
61 public columns: Array<any>;
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
62 public allColumns: Array<any>;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
63
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
64 public rows: Array<any>;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
65
52
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
66 public currentPage: number = 1;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
67 public itemsPerPage: number = 10;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
68 public maxSize: number = 5;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
69 public numPages: number = 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
70 public length: number = 0;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
71
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
72 public config: any = {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
73 paging: true,
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
74 sorting: {'columns': this.columns},
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
75 //filtering: {filterString: '', columnName: 'position'}
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
76 };
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
77
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
78 ngOnChanges(changes: any) {
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
79 console.debug("result table changed! changes=", changes);
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
80 this.data = this.queryState.results;
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
81 this.allColumns = this.queryState.resultColumns;
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
82 this.columns = this.allColumns.filter(c => c.show);
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
83 this.config.sorting = this.columns;
52
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
84 this.config.paging = {'page': this.currentPage, 'itemsPerPage': this.itemsPerPage};
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
85 this.showTable = (this.data.length < 1000);
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
86 this.onChangeTable(this.config);
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
87 }
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
88
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
89 ngOnInit() {
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
90 console.debug("result table init!");
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
91 }
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
92
30
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
93 onSelectNumItems(event: any) {
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
94 let selected = event.target.value;
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
95 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
96 let num = parseInt(selected);
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
97 if (num == 0) {
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
98 this.itemsPerPage = this.length;
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
99 } else {
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
100 this.itemsPerPage = num;
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
101 }
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
102 // update something
52
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
103 this.config.paging = {'page': this.currentPage, 'itemsPerPage': this.itemsPerPage};
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
104 this.onChangeTable(this.config);
30
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
105 }
193271b6b9d2 configure attributes per result type. select number of items per result page.
casties
parents: 29
diff changeset
106
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
107 onSelectCols(event: any) {
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
108 console.debug("select cols:", this.allColumns);
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
109 this.columns = this.allColumns.filter(c => c.show);
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
110 this.config.sorting = this.columns;
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
111 }
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
112
53
ac4bfbd99638 small change.
casties
parents: 52
diff changeset
113 changePage(page: any, data: Array<any> = this.data): Array<any> {
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
114 let start = (page.page - 1) * page.itemsPerPage;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
115 let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : data.length;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
116 return data.slice(start, end);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
117 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
118
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
119 changeSort(data: any, config: any) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
120 if (!config.sorting) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
121 return data;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
122 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
123 let columns = this.columns.filter(c => c.sort);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
124 // simple sorting
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
125 let sorted = data.sort((previous: any, current: any) => {
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
126 for (let i = 0; i < columns.length; i++) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
127 let sort = columns[i].sort;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
128 let columnName = columns[i].name;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
129 if (previous[columnName] > current[columnName]) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
130 return sort === 'desc' ? -1 : 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
131 } else if (previous[columnName] < current[columnName]) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
132 return sort === 'asc' ? -1 : 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
133 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
134 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
135 return 0;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
136 });
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
137 return sorted;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
138 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
139
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
140 changeFilter(data: any, config: any): any {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
141 if (!config.filtering) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
142 return data;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
143 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
144
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
145 let filteredData: Array<any> = data.filter((item: any) =>
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
146 item[config.filtering.columnName].match(this.config.filtering.filterString));
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
147
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
148 return filteredData;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
149 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
150
47
b65a031c4967 first step to angular2-final (2.4) version of the query browser.
casties
parents: 45
diff changeset
151 onChangeTable(config: any, page: any = config.paging) {
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
152 console.debug("onChangeTable config=", config, " page=", page);
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
153 if (config.filtering) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
154 Object.assign(this.config.filtering, config.filtering);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
155 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
156 if (config.sorting) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
157 Object.assign(this.config.sorting, config.sorting);
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
158 // changing sorting resets page
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
159 if (page == null) {
52
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
160 this.currentPage = 1;
738e90238443 ng2-table version mostly works (without page-n-of-m, problems with columns).
casties
parents: 47
diff changeset
161 page = {'page': this.currentPage, 'itemsPerPage': this.itemsPerPage};
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
162 }
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
163 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
164
31
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
165 //let filteredData = this.changeFilter(this.data, this.config);
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
166 //let sortedData = this.changeSort(filteredData, this.config);
4926885f8a99 selectable result columns. nicer cypher query output.
casties
parents: 30
diff changeset
167 let sortedData = this.changeSort(this.data, this.config);
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
168
42
99fb5a953a40 fixed issues with paging and sorting.
casties
parents: 33
diff changeset
169 this.rows = (page && this.config.paging) ? this.changePage(page, sortedData) : sortedData;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
170 this.length = sortedData.length;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
171 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
172
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
173 }