Skip to content

Commit

Permalink
Add, update and remove method have been implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Giaffaglione committed Nov 27, 2016
1 parent b14b678 commit 46ac9a4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<ul>
<li *ngFor="let item of items | async">
<span (click)=selectItem(item) >{{ item.item.name }}</span>
</li>
<li *ngFor="let item of items | async">
<span (click)=selectItem(item)>{{ item.name }}</span>
</li>
</ul>
<input [(ngModel)]="test" />


<span class="app-action">
<button md-fab (click)="openDialog()" ><md-icon>playlist_add</md-icon></button>
</span>
</span>
42 changes: 33 additions & 9 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ItemDialogComponent } from './item-dialog/item-dialog.component';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
template: require('./app.component.html'),
styles: [require('./app.component.css')]
})
export class AppComponent {
dialogRef: MdDialogRef<ItemDialogComponent>;
Expand All @@ -16,17 +16,18 @@ export class AppComponent {
constructor(private af: AngularFire, public dialog: MdDialog) {
this.items = af.database.list('/items');
console.log(this.items);
this.selectedItem = 'prova';
this.selectedItem = { name: 'Prova' };
}

click(item) {
selectItem(item) {
console.log(item);
this.selectedItem = item;
this.openDialog();
}

add() {
console.log('add new item');
const newItem = {item: { name: 'Test'}};
// const newItem = {item: { name: 'Test'}};
// this.items.push(newItem);
}

Expand All @@ -35,15 +36,38 @@ export class AppComponent {
disableClose: false,
});

this.dialogRef.componentInstance.newItem = this.selectedItem;

this.dialogRef.afterClosed().subscribe(result => {
console.log('result: ' + result);
this.dialogRef = null;
if(result) {
const newItem = {item: { name: result}};
console.log(newItem);
this.items.push(newItem)
if (result) {
if (result.delete) {
if (result.item && result.item.$key) {
this.items.remove(result.item.$key);
}
} else {
const key = result.$key;
if (key) {
delete result['$key'];
delete result['$exists'];
this.items.update(key, result);
} else {
console.log(result);
this.items.push(result);
}
}
}
});
}

}



// WEBPACK FOOTER //
// /home/fgiaffaglione/angular2/demo/~/angular2-template-loader!/home/fgiaffaglione/angular2/demo/src/app/app.component.ts


// WEBPACK FOOTER //
// /home/fgiaffaglione/angular2/demo/~/angular2-template-loader!/home/fgiaffaglione/angular2/demo/src/app/app.component.ts
7 changes: 4 additions & 3 deletions src/app/item-dialog/item-dialog.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<md-card class="app-input-section">
<md-input [(ngModel)]="firstname" placeholder="First name"></md-input>
<md-input [(ngModel)]="newItem.name" placeholder="First name"></md-input>
<md-input placeholder="Last name"></md-input>
</md-card>



<button md-raised-button type="button" color="primary"
(click)="dialogRef.close(firstname)" md-tooltip="This is a tooltip!">Yes</button>
<button md-raised-button type="button" (click)="dialogRef.close()">No</button>
(click)="dialogRef.close(newItem)" md-tooltip="This is a tooltip!">Salva</button>
<button md-raised-button type="button" (click)="dialogRef.close()">Annulla</button>
<button md-raised-button type="button" (click)="dialogRef.close({item: newItem, delete:true})">Rimuovi</button>
12 changes: 7 additions & 5 deletions src/app/item-dialog/item-dialog.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { MdDialogRef } from '@angular/material';

@Component({
selector: 'app-item-dialog',
templateUrl: './item-dialog.component.html',
styleUrls: ['./item-dialog.component.css']
})
export class ItemDialogComponent {
firstname: string;
export class ItemDialogComponent implements OnInit {
newItem: any;

constructor(public dialogRef: MdDialogRef<ItemDialogComponent>) {
this.firstname = '';
constructor(public dialogRef: MdDialogRef<ItemDialogComponent>) {
}

ngOnInit() {
}


Expand Down

0 comments on commit 46ac9a4

Please sign in to comment.