Commit 8084e2dded4962ff049620f5f2b576abdd71041e
1 parent
06a997f1
Bus Stop fatality
Showing
3 changed files
with
86 additions
and
57 deletions
Show diff stats
src/app/data/bus-stop/bus-stop.component.html
| ... | ... | @@ -2,11 +2,11 @@ |
| 2 | 2 | <ag-grid-ng2 #agGrid style="width: 100%; height: 100%;" class="ag-blue" [gridOptions]="gridOptions" [columnDefs]="columnDefs" [rowData]="rowData" enableColResize enableSorting enableFilter groupHeaders suppressRowClickSelection toolPanelSuppressGroups |
| 3 | 3 | toolPanelSuppressValues debug rowHeight="22" rowSelection="multiple" (cellClicked)="onCellClicked($event)" (cellDoubleClicked)="onCellDoubleClicked($event)" (cellContextMenu)="onCellContextMenu($event)" (cellValueChanged)="onCellValueChanged($event)" |
| 4 | 4 | (cellFocused)="onCellFocused($event)" (rowSelected)="onRowSelected($event)" (selectionChanged)="onSelectionChanged()" (beforeFilterChanged)="onBeforeFilterChanged()" (afterFilterChanged)="onAfterFilterChanged()" (filterModified)="onFilterModified()" |
| 5 | - (beforeSortChanged)="onBeforeSortChanged()" (afterSortChanged)="onAfterSortChanged()" (virtualRowRemoved)="onVirtualRowRemoved($event)" (rowClicked)="onRowClicked($event)"> | |
| 5 | + (beforeSortChanged)="onBeforeSortChanged()" (afterSortChanged)="onAfterSortChanged($event)" (virtualRowRemoved)="onVirtualRowRemoved($event)" (rowClicked)="onRowClicked($event)"> | |
| 6 | 6 | </ag-grid-ng2> |
| 7 | 7 | <div class="control_button"> |
| 8 | 8 | <div *tdLoading="'loading'; mode:'indeterminate'; type:'circle'; strategy:'replace'; color:'accent'"></div> |
| 9 | - <button md-fab color="accent" (click)="addNewRow()" type="button"><md-icon>add</md-icon></button> | |
| 10 | - <button md-fab color="warn" (click)="deleteRows()" type="button"><md-icon>delete</md-icon></button> | |
| 9 | + <button md-fab color="accent" [disabled]="isNew || isSelected" (click)="addNewRow()" type="button"><md-icon>add</md-icon></button> | |
| 10 | + <button md-fab color="warn" [disabled]="!isSelected" (click)="deleteRows()" type="button"><md-icon>delete</md-icon></button> | |
| 11 | 11 | </div> |
| 12 | 12 | </div> |
| 13 | 13 | \ No newline at end of file | ... | ... |
src/app/data/bus-stop/bus-stop.component.ts
| 1 | -import { Component, ViewEncapsulation } from '@angular/core'; | |
| 1 | +import { Component, ViewEncapsulation, AfterViewInit } from '@angular/core'; | |
| 2 | 2 | import { TdLoadingService } from '@covalent/core'; |
| 3 | -import { GridOptions } from 'ag-grid/main'; | |
| 3 | +import { GridOptions, IGetRowsParams, IRowModel } from 'ag-grid/main'; | |
| 4 | 4 | |
| 5 | 5 | import { BusStopService } from '../../../services/bus-stop.service'; |
| 6 | 6 | import { BusStop } from '../../../models/bus-stop'; |
| ... | ... | @@ -26,12 +26,11 @@ import { routerTransition } from '../../../animations/router.animation'; |
| 26 | 26 | styleUrls: ['bus-stop.scss'], |
| 27 | 27 | encapsulation: ViewEncapsulation.None, |
| 28 | 28 | }) |
| 29 | -export class BusStopComponent { | |
| 29 | +export class BusStopComponent implements AfterViewInit { | |
| 30 | 30 | |
| 31 | 31 | private columnDefs: any[]; |
| 32 | 32 | private gridOptions: GridOptions; |
| 33 | 33 | public showGrid: boolean; |
| 34 | - public rowData: any[]; | |
| 35 | 34 | public rowCount: string; |
| 36 | 35 | public regions: RegionSelectList[]; |
| 37 | 36 | public states: StateCommonSelectList[]; |
| ... | ... | @@ -41,7 +40,8 @@ export class BusStopComponent { |
| 41 | 40 | public boolean: BooleanSelectList[]; |
| 42 | 41 | public isLoading: boolean = false; |
| 43 | 42 | public isBootstrapping: boolean = true; |
| 44 | - public isSelected: boolean = true; | |
| 43 | + public isSelected: boolean = false; | |
| 44 | + public isNew: boolean = false; | |
| 45 | 45 | |
| 46 | 46 | constructor( |
| 47 | 47 | protected service: BusStopService, |
| ... | ... | @@ -51,6 +51,8 @@ export class BusStopComponent { |
| 51 | 51 | ) { |
| 52 | 52 | this.gridOptions = <GridOptions>{}; |
| 53 | 53 | this.gridOptions.enableSorting = true; |
| 54 | + this.gridOptions.suppressMultiSort = true; | |
| 55 | + this.gridOptions.enableServerSideSorting = true; | |
| 54 | 56 | this.showGrid = true; |
| 55 | 57 | this.gridOptions.rowModelType = 'virtual'; |
| 56 | 58 | this.booleanService.getModels().then((models) => this.boolean = models); |
| ... | ... | @@ -64,33 +66,31 @@ export class BusStopComponent { |
| 64 | 66 | this.createColumnDefs(); |
| 65 | 67 | this.isBootstrapping = false; |
| 66 | 68 | }); |
| 67 | - this.service.getData().then((data) => { | |
| 68 | - if (data.length){ | |
| 69 | - this.rowData = data; | |
| 70 | - } else { | |
| 71 | - this.rowData = [new BusStop()]; | |
| 72 | - } | |
| 73 | - }).then(() => { | |
| 74 | - this.gridOptions.api.setDatasource(this.setRowData(this.rowData)); | |
| 75 | - this.gridOptions.api.refreshVirtualPageCache(); | |
| 76 | - }); | |
| 77 | 69 | } |
| 78 | - setRowData(allOfTheData) { | |
| 70 | + | |
| 71 | + ngAfterViewInit() { | |
| 72 | + this.gridOptions.api.setDatasource(this.setRowData()); | |
| 73 | + } | |
| 74 | + | |
| 75 | + setRowData(): any { | |
| 79 | 76 | // tslint:disable-next-line:typedef |
| 80 | 77 | let dataSource = { |
| 81 | 78 | rowCount: null, // behave as infinite scroll |
| 82 | - getRows: function (params) { | |
| 83 | - // At this point in your code, you would call the server, using $http if in AngularJS. | |
| 84 | - // To make the demo look real, wait for 500ms before returning | |
| 85 | - // take a slice of the total rows | |
| 86 | - let rowsThisPage = allOfTheData.slice(params.startRow, params.endRow); | |
| 87 | - // if on or after the last page, work out the last row. | |
| 88 | - let lastRow = -1; | |
| 89 | - if (allOfTheData.length <= params.endRow) { | |
| 90 | - lastRow = allOfTheData.length; | |
| 79 | + getRows: (params: IGetRowsParams) => { | |
| 80 | + let sort = null; | |
| 81 | + if (params.sortModel.length) { | |
| 82 | + sort = this.parseSort(params.sortModel[0]); | |
| 91 | 83 | } |
| 92 | - // call the success callback | |
| 93 | - params.successCallback(rowsThisPage, lastRow); | |
| 84 | + this.service.getData(params.startRow, params.endRow, sort).then((data) => { | |
| 85 | + if (!data.length) { | |
| 86 | + data = [new BusStop()]; | |
| 87 | + } | |
| 88 | + let lastRow = -1; | |
| 89 | + if (data.length <= params.endRow) { | |
| 90 | + lastRow = data.length; | |
| 91 | + } | |
| 92 | + params.successCallback(data, lastRow); | |
| 93 | + }); | |
| 94 | 94 | }, |
| 95 | 95 | }; |
| 96 | 96 | return dataSource; |
| ... | ... | @@ -114,12 +114,31 @@ export class BusStopComponent { |
| 114 | 114 | event.confirm.reject(); |
| 115 | 115 | } |
| 116 | 116 | } |
| 117 | - public addNewRow() { | |
| 118 | - this.rowData.unshift(new BusStop()); | |
| 119 | - this.gridOptions.api.setDatasource(this.setRowData(this.rowData)); | |
| 120 | - this.gridOptions.api.refreshVirtualPageCache(); | |
| 117 | + public addNewRow(): void { | |
| 118 | + if (this.getFirstRowID()) { | |
| 119 | + this.gridOptions.api.insertItemsAtIndex(0, [new BusStop()]); | |
| 120 | + this.isNew = true; | |
| 121 | + } | |
| 122 | + } | |
| 123 | + private parseSort(sort: any): string { | |
| 124 | + if (sort.colId && sort.sort) { | |
| 125 | + let result = sort.colId; | |
| 126 | + if (sort.sort === 'desc') { | |
| 127 | + result = '-' + result; | |
| 128 | + } | |
| 129 | + return result; | |
| 130 | + } | |
| 131 | + return null; | |
| 132 | + } | |
| 133 | + private getFirstRowID(): number { | |
| 134 | + let model = this.gridOptions.api.getModel().getRow(0); | |
| 135 | + let id: number = model.data.busStopId; | |
| 136 | + if (id) { | |
| 137 | + return id; | |
| 138 | + } else { | |
| 139 | + return null; | |
| 140 | + } | |
| 121 | 141 | } |
| 122 | - | |
| 123 | 142 | private createColumnDefs() { |
| 124 | 143 | this.columnDefs = [ |
| 125 | 144 | { |
| ... | ... | @@ -273,10 +292,10 @@ export class BusStopComponent { |
| 273 | 292 | } |
| 274 | 293 | this.enableLoader(); |
| 275 | 294 | result = this.service.create(data).then((busStop) => { |
| 276 | - this.rowData[$event.node.rowIndex] = busStop; | |
| 277 | - this.gridOptions.api.setDatasource(this.setRowData(this.rowData)); | |
| 295 | + this.gridOptions.api.setDatasource(this.setRowData()); | |
| 278 | 296 | this.gridOptions.api.refreshVirtualPageCache(); |
| 279 | 297 | this.disableLoader(); |
| 298 | + this.isNew = false; | |
| 280 | 299 | }); |
| 281 | 300 | } |
| 282 | 301 | } |
| ... | ... | @@ -285,28 +304,24 @@ export class BusStopComponent { |
| 285 | 304 | private deleteRows() { |
| 286 | 305 | let rows = this.gridOptions.api.getSelectedNodes(); |
| 287 | 306 | if (!rows.length) { |
| 307 | + this.isSelected = false; | |
| 288 | 308 | return ; |
| 289 | 309 | } |
| 290 | - rows.forEach((element) => { | |
| 310 | + rows.forEach((element, index, array) => { | |
| 291 | 311 | let id = element.data.busStopId; |
| 292 | 312 | if (id) { |
| 293 | 313 | this.enableLoader(); |
| 294 | - this.service.delete(id).then(() => this.disableLoader()); | |
| 314 | + this.service.delete(id).then(() => { | |
| 315 | + if (index === (array.length - 1)) { | |
| 316 | + this.disableLoader(); | |
| 317 | + this.gridOptions.api.setDatasource(this.setRowData()); | |
| 318 | + this.gridOptions.api.refreshVirtualPageCache(); | |
| 319 | + this.gridOptions.api.deselectAll(); | |
| 320 | + this.isNew = false; | |
| 321 | + } | |
| 322 | + }); | |
| 295 | 323 | } |
| 296 | 324 | }); |
| 297 | - // Sort in order to protect array from reindexing (remove rear elements first) | |
| 298 | - let sorted = rows.sort((a, b) => { | |
| 299 | - if (a > b) { | |
| 300 | - return -1; | |
| 301 | - } else { | |
| 302 | - return 1; | |
| 303 | - } | |
| 304 | - }); | |
| 305 | - sorted.forEach((item) => { | |
| 306 | - this.rowData.splice(item.rowIndex, 1); | |
| 307 | - }); | |
| 308 | - this.gridOptions.api.setDatasource(this.setRowData(this.rowData)); | |
| 309 | - this.gridOptions.api.refreshVirtualPageCache(); | |
| 310 | 325 | } |
| 311 | 326 | |
| 312 | 327 | private onCellDoubleClicked($event) { |
| ... | ... | @@ -327,7 +342,16 @@ export class BusStopComponent { |
| 327 | 342 | } |
| 328 | 343 | |
| 329 | 344 | private onSelectionChanged() { |
| 330 | - console.log('selectionChanged'); | |
| 345 | + let selection = this.gridOptions.api.getSelectedNodes(); | |
| 346 | + if (selection.length) { | |
| 347 | + if (selection.length === 1) { | |
| 348 | + this.gridOptions.api.refreshVirtualPageCache(); | |
| 349 | + this.isNew = false; | |
| 350 | + } | |
| 351 | + this.isSelected = true; | |
| 352 | + } else { | |
| 353 | + this.isSelected = false; | |
| 354 | + } | |
| 331 | 355 | } |
| 332 | 356 | |
| 333 | 357 | private onBeforeFilterChanged() { |
| ... | ... | @@ -342,11 +366,11 @@ export class BusStopComponent { |
| 342 | 366 | console.log('onFilterModified'); |
| 343 | 367 | } |
| 344 | 368 | |
| 345 | - private onBeforeSortChanged() { | |
| 369 | + private onBeforeSortChanged(event) { | |
| 346 | 370 | console.log('onBeforeSortChanged'); |
| 347 | 371 | } |
| 348 | 372 | |
| 349 | - private onAfterSortChanged() { | |
| 373 | + private onAfterSortChanged($event) { | |
| 350 | 374 | console.log('onAfterSortChanged'); |
| 351 | 375 | } |
| 352 | 376 | ... | ... |
src/services/bus-stop.service.ts
| ... | ... | @@ -10,8 +10,13 @@ export class BusStopService { |
| 10 | 10 | private url = 'http://localhost:5000/busstop'; |
| 11 | 11 | private headers = new Headers({'Content-Type': 'application/json'}); |
| 12 | 12 | constructor(private http: Http) { } |
| 13 | - getData(): Promise<BusStop[]> { | |
| 14 | - return this.http.get(this.url) | |
| 13 | + getData(from: number = 0, to: number = 100, sort: string = null): Promise<BusStop[]> { | |
| 14 | + let url = this.url; | |
| 15 | + url += '?from=' + from + '&to=' + to; | |
| 16 | + if (sort) { | |
| 17 | + url += '&sort=' + sort; | |
| 18 | + } | |
| 19 | + return this.http.get(url) | |
| 15 | 20 | .toPromise() |
| 16 | 21 | .then(response => response.json().busStopEditDsM as BusStop[]) |
| 17 | 22 | .catch(this.handleError); | ... | ... |