Blame view

src/app/components/grid/rich-grid.component.ts 10.1 KB
91d89214   Administrator   add grid
1
2
  import {Component, ViewEncapsulation} from "@angular/core";
  import {GridOptions} from "ag-grid/main";
6c0454b9   Yarik   Table
3
  
91d89214   Administrator   add grid
4
5
6
  import ProficiencyFilter from "./proficiencyFilter";
  import SkillFilter from "./skillFilter";
  import RefData from "./refData";
6c0454b9   Yarik   Table
7
8
9
10
11
12
13
14
15
16
17
18
19
  import { BusStopService } from './busStop.service';
  import { busStop } from './busStop';
  import { EditorComponent } from '../../helpers/editor.component';
  import { RendererComponent } from '../../helpers/renderer.component';
  import { BusStopCreateService } from '../../services/busstopcreate.service';
  import { BooleanSelectListService } from '../../services/booleanselectlist.service';
  import { RegionSelectList } from '../../models/regionselectlist';
  import { StateCommonSelectList } from '../../models/statecommonselectlist';
  import { RoadSelectList } from '../../models/roadselectlist';
  import { BooleanSelectList } from '../../models/booleanselectlist';
  import { SettlementSelectList } from '../../models/settlementselectlist';
  import { SurfaceTypeSelectList } from '../../models/surfacetypeselectlist';
  
91d89214   Administrator   add grid
20
21
22
23
24
25
26
27
28
29
30
  
  // only import this if you are using the ag-Grid-Enterprise
  
  @Component({
      selector: 'rich-grid',
      templateUrl: 'rich-grid.component.html',
      styleUrls: ['rich-grid.css', 'proficiency-renderer.css'],
      encapsulation: ViewEncapsulation.None
  })
  export class RichGridComponent {
  
91d89214   Administrator   add grid
31
32
      public showGrid: boolean;
      public rowData: any[];
91d89214   Administrator   add grid
33
      public rowCount: string;
6c0454b9   Yarik   Table
34
35
36
37
38
39
40
41
      public regions: RegionSelectList[];
      public states: StateCommonSelectList[];
      public surfaceTypes: SurfaceTypeSelectList[];
      public settlements: SettlementSelectList[];
      public roads: RoadSelectList[];
      public boolean: BooleanSelectList[];
      public isLoading: boolean = false;
      public isBootstrapping: boolean = true;
440e2e86   Yarik   fixes
42
      public isSelected: boolean = true;
6c0454b9   Yarik   Table
43
44
      private columnDefs: any[];
      private gridOptions: GridOptions;
91d89214   Administrator   add grid
45
  
6c0454b9   Yarik   Table
46
47
48
49
50
      constructor(
          protected service: BusStopService,
          private dataService: BusStopCreateService,
          private booleanService: BooleanSelectListService
      ) {
91d89214   Administrator   add grid
51
          this.gridOptions = <GridOptions>{};
6c0454b9   Yarik   Table
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        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.surfaceTypes = models.surfaceTypeSelectListDsM as SurfaceTypeSelectList[];
          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 busStop];
            }
        }).then(() => {
            this.gridOptions.api.setDatasource(this.setRowData(this.rowData));
            this.gridOptions.api.refreshVirtualPageCache();
        });
91d89214   Administrator   add grid
76
      }
6c0454b9   Yarik   Table
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
      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();
91d89214   Administrator   add grid
103
      }
6c0454b9   Yarik   Table
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    }
    public addNewRow() {
          this.rowData.unshift(new busStop());
          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: 'busStopId',
6c0454b9   Yarik   Table
124
125
126
127
128
        },
        {
          headerName: 'Назва дороги',
          field: 'roadId',
          editable: true,
6c0454b9   Yarik   Table
129
130
131
132
133
134
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.roads,
            valueCol: 'roadId',
            labelCol: 'name'
91d89214   Administrator   add grid
135
          }
6c0454b9   Yarik   Table
136
137
138
139
140
        },
        {
          headerName: 'Область',
          field: 'regionId',
          editable: true,
6c0454b9   Yarik   Table
141
142
143
144
145
146
147
148
149
150
151
152
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.regions,
            valueCol: 'regionId',
            labelCol: 'name'
          }
        },
        {
          headerName: 'Місцезнаходження, км+ справа',
          field: 'locationRight',
          editable: true,
6c0454b9   Yarik   Table
153
154
155
156
157
        },
        {
          headerName: 'Місцезнаходження, км+ зліва',
          field: 'locationLeft',
          editable: true,
6c0454b9   Yarik   Table
158
159
160
161
162
        },
        {
          headerName: 'Технічний стан',
          field: 'stateCommonId',
          editable: true,
6c0454b9   Yarik   Table
163
164
165
166
167
168
169
170
171
172
173
174
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.states,
            valueCol: 'stateCommonId',
            labelCol: 'value'
          }
        },
        {
          headerName: 'Наявність туалету',
          field: 'toiletAvailability',
          editable: true,
6c0454b9   Yarik   Table
175
176
177
178
179
180
181
182
183
184
          cellEditorFramework: EditorComponent,
          cellRendererFramework: RendererComponent,
          cellEditorParams: {
            data: this.boolean,
            valueCol: 'value',
            labelCol: 'label'
          }
        }
      ];
    }
91d89214   Administrator   add grid
185
186
187
188
189
190
  
      private onCellClicked($event) {
          console.log('onCellClicked: ' + $event.rowIndex + ' ' + $event.colDef.field);
      }
  
      private onCellValueChanged($event) {
6c0454b9   Yarik   Table
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
          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();
91d89214   Administrator   add grid
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
      }
  
      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);
      }
91d89214   Administrator   add grid
301
  }