service-object.component.ts
9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import {Component, ViewEncapsulation} from '@angular/core';
import {GridOptions} from 'ag-grid/main';
import { ServiceObjectService } from '../../../services/service-object.service';
import { ServiceObject } from '../../../models/service-object';
import { EditorComponent } from '../../../helpers/editor.component';
import { RendererComponent } from '../../../helpers/renderer.component';
import { ServiceObjectCreateService } from '../../../services/service-object-create.service';
import { BooleanSelectListService } from '../../../services/boolean-select-list.service';
import { RegionSelectList } from '../../../models/region-select-list';
import { StateCommonSelectList } from '../../../models/state-common-select-list';
import { RoadSelectList } from '../../../models/road-select-list';
import { DepartmentAffiliationList } from '../../../models/department-affiliation-list';
import { BooleanSelectList } from '../../../models/boolean-select-list';
import { SettlementSelectList } from '../../../models/settlement-select-list';
import { routerTransition } from '../../../animations/router.animation';
// only import this if you are using the ag-Grid-Enterprise
@Component({
selector: 'service-object',
templateUrl: 'service-object.component.html',
styleUrls: ['service-object.scss'],
encapsulation: ViewEncapsulation.None,
})
export class ServiceObjectComponent {
public showGrid: boolean;
public rowData: any[];
public rowCount: string;
public regions: RegionSelectList[];
public states: StateCommonSelectList[];
public departmentAffiliation: DepartmentAffiliationList[];
public settlements: SettlementSelectList[];
public roads: RoadSelectList[];
public boolean: BooleanSelectList[];
public isLoading: boolean = false;
public isBootstrapping: boolean = true;
public isSelected: boolean = true;
private columnDefs: any[];
private gridOptions: GridOptions;
constructor(
protected service: ServiceObjectService,
private dataService: ServiceObjectCreateService,
private booleanService: BooleanSelectListService,
) {
this.gridOptions = <GridOptions>{};
this.gridOptions.enableSorting = true;
this.showGrid = true;
this.gridOptions.rowModelType = 'virtual';
this.booleanService.getModels().then((models) => this.boolean = models);
this.dataService.getModels().then((models) => {
this.regions = models.regionSelectListDsM as RegionSelectList[];
this.states = models.stateCommonSelectListDsM as StateCommonSelectList[];
this.departmentAffiliation = models.departmentAffiliationListDsM as DepartmentAffiliationList[];
this.settlements = models.settlementSelectListDsM as SettlementSelectList[];
this.roads = models.roadSelectListDsM as RoadSelectList[];
}).then(() => {
this.createColumnDefs();
this.isBootstrapping = false;
});
this.service.getData().then((data) => {
if (data.length){
this.rowData = data;
} else {
this.rowData = [new ServiceObject];
}
}).then(() => {
this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
this.gridOptions.api.refreshVirtualPageCache();
});
}
setRowData(allOfTheData) {
let dataSource = {
rowCount: null, // behave as infinite scroll
getRows: function (params) {
console.log('asking for ' + params.startRow + ' to ' + params.endRow);
// At this point in your code, you would call the server, using $http if in AngularJS.
// To make the demo look real, wait for 500ms before returning
// take a slice of the total rows
let rowsThisPage = allOfTheData.slice(params.startRow, params.endRow);
// if on or after the last page, work out the last row.
let lastRow = -1;
if (allOfTheData.length <= params.endRow) {
lastRow = allOfTheData.length;
}
// call the success callback
params.successCallback(rowsThisPage, lastRow);
},
};
return dataSource;
}
onDeleteConfirm(event): void {
if (window.confirm('Вы уверены что хотите удалить??')) {
event.confirm.resolve();
} else {
event.confirm.reject();
}
}
public addNewRow() {
this.rowData.unshift(new ServiceObject());
this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
this.gridOptions.api.refreshVirtualPageCache();
}
private createColumnDefs() {
this.columnDefs = [
{
headerName: '#',
width: 30,
checkboxSelection: true,
suppressSorting: true,
suppressMenu: true,
pinned: true,
},
{
headerName: 'ID',
field: 'serviceObjectId',
},
{
headerName: 'Назва дороги',
field: 'roadId',
editable: true,
cellEditorFramework: EditorComponent,
cellRendererFramework: RendererComponent,
cellEditorParams: {
data: this.roads,
valueCol: 'roadId',
labelCol: 'name',
},
},
{
headerName: 'Область',
field: 'regionId',
editable: true,
cellEditorFramework: EditorComponent,
cellRendererFramework: RendererComponent,
cellEditorParams: {
data: this.regions,
valueCol: 'regionId',
labelCol: 'name',
},
},
{
headerName: 'Місцезнаходження, км+ справа',
field: 'locationRight',
editable: true,
},
{
headerName: 'Місцезнаходження, км+ зліва',
field: 'locationLeft',
editable: true,
},
];
}
private onCellClicked($event) {
console.log('onCellClicked: ' + $event.rowIndex + ' ' + $event.colDef.field);
}
private onCellValueChanged($event) {
if ($event.oldValue !== $event.newValue) {
let data = JSON.stringify($event.data);
let id = $event.data.busStopId;
let result = null;
if (id) {
this.isLoading = true;
result = this.service.update(id, data).then(() => this.isLoading = false);
} else {
// Protection of posting new row being already sent.
if (this.isLoading) {
return ;
}
this.isLoading = true;
result = this.service.create(data).then((busStop) => {
this.rowData[$event.node.rowIndex] = busStop;
this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
this.gridOptions.api.refreshVirtualPageCache();
this.isLoading = false;
});
}
}
}
private deleteRows() {
let rows = this.gridOptions.api.getSelectedNodes();
if (!rows.length) {
return ;
}
rows.forEach((element) => {
let id = element.data.busStopId;
if (id) {
this.isLoading = true;
this.service.delete(id).then(() => this.isLoading = false);
}
});
// Sort in order to protect array from reindexing (remove rear elements first)
let sorted = rows.sort((a, b) => {
if (a > b) {
return -1;
} else {
return 1;
}
});
sorted.forEach((item) => {
this.rowData.splice(item.rowIndex, 1);
});
this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
this.gridOptions.api.refreshVirtualPageCache();
}
private onCellDoubleClicked($event) {
console.log('onCellDoubleClicked: ' + $event.rowIndex + ' ' + $event.colDef.field);
}
private onCellContextMenu($event) {
console.log('onCellContextMenu: ' + $event.rowIndex + ' ' + $event.colDef.field);
}
private onCellFocused($event) {
console.log('onCellFocused: (' + $event.rowIndex + ',' + $event.colIndex + ')');
}
private onRowSelected($event) {
// taking out, as when we 'select all', it prints to much to the console!!
// console.log('onRowSelected: ' + $event.node.data.name);
}
private onSelectionChanged() {
console.log('selectionChanged');
}
private onBeforeFilterChanged() {
console.log('beforeFilterChanged');
}
private onAfterFilterChanged() {
console.log('afterFilterChanged');
}
private onFilterModified() {
console.log('onFilterModified');
}
private onBeforeSortChanged() {
console.log('onBeforeSortChanged');
}
private onAfterSortChanged() {
console.log('onAfterSortChanged');
}
private onVirtualRowRemoved($event) {
// because this event gets fired LOTS of times, we don't print it to the
// console. if you want to see it, just uncomment out this line
// console.log('onVirtualRowRemoved: ' + $event.rowIndex);
}
private onRowClicked($event) {
console.log('onRowClicked: ' + $event.node.data.name);
}
public onQuickFilterChanged($event) {
this.gridOptions.api.setQuickFilter($event.target.value);
}
// here we use one generic event to handle all the column type events.
// the method just prints the event name
private onColumnEvent($event) {
console.log('onColumnEvent: ' + $event);
}
}