annotate app/query-result-table.component.ts @ 28:1ceea716600f

result table can be shown with checkbox. initially hidden if > 1000 results.
author casties
date Wed, 27 Jan 2016 16:01:06 +0100
parents c2946b7135cd
children 52af480a843e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
1 import {Component, OnInit} from 'angular2/core';
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
2
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
3 import {NG_TABLE_DIRECTIVES} from 'ng2-table/ng2-table';
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
4 import {PAGINATION_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
5
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
6 import {QueryState} from './query-state';
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
7
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
8 @Component({
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
9 selector: 'query-result',
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
10 template: `
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
11 <div *ngIf="queryState && queryState.results">
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
12 <span>Cypher query:</span>
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
13 <pre>{{queryState.resultCypherQuery}}</pre>
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
14 <h2>Query result</h2>
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
15 <pre>{{resultInfo}}</pre>
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
16 <div><input type="checkbox" [(ngModel)]="showTable"/> show results</div>
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
17 <div *ngIf="showTable">
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
18 <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
19 <pagination *ngIf="config.paging"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
20 class="pagination-sm"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
21 [(ngModel)]="page"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
22 [totalItems]="length"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
23 [itemsPerPage]="itemsPerPage"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
24 [maxSize]="maxSize"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
25 [boundaryLinks]="true"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
26 [rotate]="false"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
27 (pageChanged)="onChangeTable(config, $event)"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
28 (numPages)="numPages = $event">
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
29 </pagination>
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
30 <ngTable
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
31 [config]="config.sorting"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
32 (tableChanged)="onChangeTable(config)"
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
33 [rows]="rows" [columns]="columns">
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
34 </ngTable>
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
35 </div>
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
36 </div>
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
37 `,
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
38 inputs: ['queryState', 'resultInfo'],
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
39 directives: [NG_TABLE_DIRECTIVES, PAGINATION_DIRECTIVES]
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
40 })
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
41
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
42 export class QueryResultTableComponent implements OnInit {
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
43
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
44 public queryState: QueryState;
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
45 public resultInfo: string;
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
46
28
1ceea716600f result table can be shown with checkbox. initially hidden if > 1000 results.
casties
parents: 27
diff changeset
47 public showTable = false;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
48 public data: Array<any>;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
49 public columns: Array<any>;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
50
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
51 public rows: Array<any>;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
52
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
53 public page: number = 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
54 public itemsPerPage: number = 10;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
55 public maxSize: number = 5;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
56 public numPages: number = 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
57 public length: number = 0;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
58
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
59 public config: any = {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
60 paging: true,
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
61 sorting: {'columns': this.columns},
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
62 //filtering: {filterString: '', columnName: 'position'}
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
63 };
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
64
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
65 ngOnChanges() {
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
66 console.debug("result table changed! queryState=", this.queryState?this.queryState.resultColumns:'');
27
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
67 this.data = this.queryState.results;
c2946b7135cd working on larger results.
casties
parents: 26
diff changeset
68 this.columns = this.queryState.resultColumns;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
69 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
70 this.showTable = (this.data.length < 1000);
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
71 this.onChangeTable(this.config, null);
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
72 }
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
73
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
74 ngOnInit() {
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
75 console.debug("result table init!");
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
76 }
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
77
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
78 changePage(page:any, data:Array<any> = this.data):Array<any> {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
79 let start = (page.page - 1) * page.itemsPerPage;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
80 let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : data.length;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
81 return data.slice(start, end);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
82 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
83
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
84 changeSort(data: any, config: any) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
85 if (!config.sorting) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
86 return data;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
87 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
88 let columns = this.columns.filter(c => c.sort);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
89 // simple sorting
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
90 let sorted = data.sort((previous: any, current: any) => {
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
91 for (let i = 0; i < columns.length; i++) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
92 let sort = columns[i].sort;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
93 let columnName = columns[i].name;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
94 if (previous[columnName] > current[columnName]) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
95 return sort === 'desc' ? -1 : 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
96 } else if (previous[columnName] < current[columnName]) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
97 return sort === 'asc' ? -1 : 1;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
98 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
99 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
100 return 0;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
101 });
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
102 return sorted;
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
103 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
104
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
105 changeFilter(data: any, config: any): any {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
106 if (!config.filtering) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
107 return data;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
108 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
109
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
110 let filteredData: Array<any> = data.filter((item: any) =>
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
111 item[config.filtering.columnName].match(this.config.filtering.filterString));
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
112
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
113 return filteredData;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
114 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
115
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
116 onChangeTable(config, page: any = config.paging) {
26
991bf349bb04 re-display table more often.
casties
parents: 25
diff changeset
117 console.debug("onChangeTable config=", config);
25
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
118 if (config.filtering) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
119 Object.assign(this.config.filtering, config.filtering);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
120 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
121 if (config.sorting) {
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
122 Object.assign(this.config.sorting, config.sorting);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
123 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
124
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
125 let filteredData = this.changeFilter(this.data, this.config);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
126 let sortedData = this.changeSort(filteredData, this.config);
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
127
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
128 this.rows = (page && config.paging) ? this.changePage(page, sortedData) : sortedData;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
129 this.length = sortedData.length;
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
130 }
0795207f3b90 ng2-table now with pager, sorting and styling.
casties
parents: 24
diff changeset
131
24
f6f4177d0a81 use ng2-table for results.
casties
parents:
diff changeset
132 }