Skip to content

Commit

Permalink
improve trackBy demo
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuzuld committed Jul 21, 2020
1 parent fc6e50d commit a2bc4b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class GridsterItemComponent implements OnDestroy, OnChanges, GridsterItem
}

ngOnChanges(changes: SimpleChanges): void {
if (changes['item']) {
if (changes.item) {
this.updateOptions();

if (!this.init) {
Expand Down
9 changes: 8 additions & 1 deletion src/app/sections/trackBy/trackBy.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
</a>
<markdown [src]="'assets/trackBy.md'"></markdown>
</div>
<div *ngIf="dashboard[0]" class="options-header">
<div class="options-header">
<button (click)="reset()" class="cols-2" color="accent" mat-raised-button>
Reset
</button>
<button (click)="addItem()" class="add-button cols-2" mat-mini-fab>
<mat-icon>add</mat-icon>
</button>
</div>


<gridster [options]="options">
<gridster-item *ngFor="let item of dashboard; trackBy: trackBy" [item]="item">
<button (mousedown)="removeItem($event, item)" (touchstart)="removeItem($event, item)"
mat-mini-fab style="position: absolute; right: 10px; top: 10px">
<mat-icon>delete</mat-icon>
</button>
<app-trackby-item [id]="item.id"></app-trackby-item>
</gridster-item>
</gridster>
Expand Down
20 changes: 15 additions & 5 deletions src/app/sections/trackBy/trackBy.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export class TrackByComponent implements OnInit {
dragEnabled: true,
resizeEnabled: true,
compactEnabled: true,
id: '0',
id: 0,
},
{cols: 2, rows: 2, y: 0, x: 2, id: '1'},
{cols: 1, rows: 1, y: 0, x: 4, id: '2'},
{cols: 3, rows: 2, y: 1, x: 4, id: '3'},
{cols: 1, rows: 1, y: 2, x: 1, id: '4'}
{cols: 2, rows: 2, y: 0, x: 2, id: 1},
{cols: 1, rows: 1, y: 0, x: 4, id: 2},
{cols: 3, rows: 2, y: 1, x: 4, id: 3},
{cols: 1, rows: 1, y: 2, x: 1, id: 4}
];
this.dashboardOriginal = this.dashboard.map(x => ({...x}));
}
Expand All @@ -83,4 +83,14 @@ export class TrackByComponent implements OnInit {
trackBy(index: number, item: GridsterItem): number {
return item.id;
}

addItem(): void {
this.dashboard.push({x: 0, y: 0, cols: 1, rows: 1, id: this.dashboard.length});
}

removeItem($event: MouseEvent | TouchEvent, item): void {
$event.preventDefault();
$event.stopPropagation();
this.dashboard.splice(this.dashboard.indexOf(item), 1);
}
}

0 comments on commit a2bc4b4

Please sign in to comment.