From 2922345a60dc933a00343072849905b62761e75d Mon Sep 17 00:00:00 2001 From: Max Ehrlicher-Schmidt Date: Tue, 22 Sep 2020 22:37:24 +0200 Subject: [PATCH] Add edit button --- .../pages/tables/bikes/bikes.component.html | 3 +- .../pages/tables/bikes/bikes.component.scss | 1 + src/app/pages/tables/bikes/bikes.component.ts | 67 ++++++++++++++----- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/app/pages/tables/bikes/bikes.component.html b/src/app/pages/tables/bikes/bikes.component.html index 3a8e9c4..f63b353 100644 --- a/src/app/pages/tables/bikes/bikes.component.html +++ b/src/app/pages/tables/bikes/bikes.component.html @@ -78,9 +78,10 @@
- + diff --git a/src/app/pages/tables/bikes/bikes.component.scss b/src/app/pages/tables/bikes/bikes.component.scss index 0a80432..23cb5a9 100644 --- a/src/app/pages/tables/bikes/bikes.component.scss +++ b/src/app/pages/tables/bikes/bikes.component.scss @@ -19,6 +19,7 @@ .mat-cell { min-width: 80px; box-sizing: border-box; + padding-left: 0.5em; } .mat-table-sticky { diff --git a/src/app/pages/tables/bikes/bikes.component.ts b/src/app/pages/tables/bikes/bikes.component.ts index 522bfca..72337dd 100644 --- a/src/app/pages/tables/bikes/bikes.component.ts +++ b/src/app/pages/tables/bikes/bikes.component.ts @@ -1,7 +1,13 @@ import { SelectionModel } from '@angular/cdk/collections'; -import { Component, OnInit } from '@angular/core'; -import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop'; +import { Component, ChangeDetectorRef } from '@angular/core'; +import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop'; import { BikesService, CargoBikeResult } from 'src/app/services/bikes.service'; +import { MatTableDataSource } from '@angular/material/table'; + +type CargoBikeDataRow = CargoBikeResult & { + waitingForEditPermissions: boolean; + isGettingEdited: boolean; +}; @Component({ selector: 'app-bikes', @@ -9,37 +15,66 @@ import { BikesService, CargoBikeResult } from 'src/app/services/bikes.service'; styleUrls: ['./bikes.component.scss'], }) export class BikesComponent { - displayedColumns: string[] = ['select', 'name', 'id', 'frameNumber', 'numberOfChildren', 'buttons']; - - bikes: CargoBikeResult[]; - selection = new SelectionModel(true, []); - + displayedColumns: string[] = [ + 'select', + 'name', + 'id', + 'frameNumber', + 'numberOfChildren', + 'buttons', + ]; - constructor(private bikesService: BikesService) { - bikesService.bikes.subscribe(bikes => this.bikes = bikes); + bikes = new MatTableDataSource(>[]); + selection = new SelectionModel(true, []); + constructor( + private bikesService: BikesService, + private changeDetectorRefs: ChangeDetectorRef + ) { + bikesService.bikes.subscribe((bikes) => { + this.bikes = new MatTableDataSource( + bikes.map((bike) => { + return Object.assign({}, bike, { + waitingForEditPermissions: false, + isGettingEdited: false, + }); + }) + ); + }); bikesService.loadBikes(); } - edit(id: string) { - console.log(id); + edit(row: CargoBikeDataRow) { + console.log('isGettingEdited: ' + row.isGettingEdited); + row.waitingForEditPermissions = true; + console.log('edit'); + console.log(row.waitingForEditPermissions); + setTimeout(() => { + row.waitingForEditPermissions = false; + row.isGettingEdited = true; + console.log('gets edited'); + }, 100); } drop(event: CdkDragDrop) { - moveItemInArray(this.displayedColumns, event.previousIndex + 2, event.currentIndex + 2); // +2 because the first 2 (selection + name) columns are not dragable + moveItemInArray( + this.displayedColumns, + event.previousIndex + 2, + event.currentIndex + 2 + ); // +2 because the first 2 (selection + name) columns are not dragable } /** Whether the number of selected elements matches the total number of rows. */ isAllSelected() { const numSelected = this.selection.selected.length; - const numRows = this.bikes.length; + const numRows = this.bikes.data.length; return numSelected === numRows; } /** Selects all rows if they are not all selected; otherwise clear selection. */ masterToggle() { - this.isAllSelected() ? - this.selection.clear() : - this.bikes.forEach(row => this.selection.select(row)); + this.isAllSelected() + ? this.selection.clear() + : this.bikes.data.forEach((row) => this.selection.select(row)); } }