annotate app/query-result-table.component.ts @ 26:991bf349bb04

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