Add edit button

pull/1/head
Max Ehrlicher-Schmidt 4 years ago
parent f974312467
commit 2922345a60

@ -78,9 +78,10 @@
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let element">
<div class="button-wrapper">
<button mat-icon-button (click)="edit(element.id)">
<button mat-icon-button (click)="edit(element)" [hidden]="isGettingEdited || waitingForEditPermissions">
<mat-icon>edit</mat-icon>
</button>
<mat-spinner [diameter]="32" *ngIf="waitingForEditPermissions"></mat-spinner>
<button mat-icon-button>
<mat-icon>delete</mat-icon>
</button>

@ -19,6 +19,7 @@
.mat-cell {
min-width: 80px;
box-sizing: border-box;
padding-left: 0.5em;
}
.mat-table-sticky {

@ -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<CargoBikeResult>(true, []);
displayedColumns: string[] = [
'select',
'name',
'id',
'frameNumber',
'numberOfChildren',
'buttons',
];
constructor(private bikesService: BikesService) {
bikesService.bikes.subscribe(bikes => this.bikes = bikes);
bikes = new MatTableDataSource(<Array<CargoBikeDataRow>>[]);
selection = new SelectionModel<CargoBikeDataRow>(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<string[]>) {
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));
}
}

Loading…
Cancel
Save