annotate app/query-result-table.component.ts @ 29:52af480a843e

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