annotate app/query-result-table.component.ts @ 27:c2946b7135cd

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