Skip to content

Commit dd8130b

Browse files
authored
Merge pull request #2126 from adumesny/master
more angular demo tweaks
2 parents c4b5524 + 0199666 commit dd8130b

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

demo/angular/src/app/ngFor.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let ids = 1;
1515
<button (click)="add()">add item</button>
1616
<button (click)="delete()">remove item</button>
1717
<button (click)="change()">modify item</button>
18+
<button (click)="newLayout()">new layout</button>
1819
<div class="grid-stack">
1920
<!-- using angular templating to create DOM, otherwise an easier way is to simply call grid.load(items) -->
2021
<div *ngFor="let n of items; trackBy: identify"
@@ -30,7 +31,7 @@ let ids = 1;
3031
</div>
3132
</div>
3233
`,
33-
// gridstack.min.css and other custom styles should be included in global styles.scss
34+
// gridstack.min.css and other custom styles should be included in global styles.scss or here
3435
})
3536
export class AngularNgForTestComponent implements AfterViewInit {
3637
/** list of HTML items that we track to know when the DOM has been updated to make/remove GS widgets */
@@ -75,7 +76,7 @@ export class AngularNgForTestComponent implements AfterViewInit {
7576
const n = ref.nativeElement.gridstackNode || this.grid.makeWidget(ref.nativeElement).gridstackNode;
7677
if (n) layout.push(n);
7778
});
78-
this.grid.load(layout);
79+
this.grid.load(layout); // efficient that does diffs only
7980
})
8081
}
8182

@@ -95,7 +96,7 @@ export class AngularNgForTestComponent implements AfterViewInit {
9596
* CRUD operations
9697
*/
9798
public add() {
98-
// new array doesn't seem required. Angular seem to detect changes to content...
99+
// new array isn't required as Angular seem to detect changes to content
99100
// this.items = [...this.items, { x: 3, y: 0, w: 3, id: String(ids++) }];
100101
this.items.push({ x: 3, y: 0, w: 3, id: String(ids++) });
101102
}
@@ -105,11 +106,20 @@ export class AngularNgForTestComponent implements AfterViewInit {
105106
}
106107

107108
public change() {
108-
// this.items[0]?.w = 2; // this will not update GS internal data, only DOM values even thought array doesn't grow/shrink. Need to call GS update()
109+
// this.items[0]?.w = 2; // this will not trigger gridstackItems.changes.subscribe, only DOM values are update, so call GS update() instead
109110
const n = this.grid.engine.nodes[0];
110111
if (n) this.grid.update(n.el!, { w: 2 });
111112
}
112113

114+
public newLayout() {
115+
this.items = [ // test updating existing and creating new one
116+
{x: 0, y: 1, id: 1},
117+
{x: 1, y: 1, id: 2},
118+
// {x: 2, y: 1, id: 3}, // delete item
119+
{x: 3, y: 0, w: 3}, // new item
120+
];
121+
}
122+
113123
// ngFor unique node id to have correct match between our items used and GS
114124
identify(index: number, w: GridStackWidget) {
115125
return w.id;

demo/angular/src/app/ngFor_cmd.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* NOTE: see the simpler and better (tracks all changes) angular-ng-for-test
44
*/
55

6-
import { Component, AfterViewInit, Input, ViewChildren, QueryList } from "@angular/core";
6+
import { Component, AfterViewInit, Input, ViewChildren, QueryList, ElementRef } from '@angular/core';
77
import { Subject, zip } from "rxjs";
88

9-
import { GridStack, GridStackWidget } from 'gridstack';
9+
import { GridItemHTMLElement, GridStack, GridStackWidget } from 'gridstack';
1010

1111
@Component({
1212
selector: "angular-ng-for-cmd-test",
@@ -31,30 +31,24 @@ import { GridStack, GridStackWidget } from 'gridstack';
3131
</div>
3232
</div>
3333
`,
34-
styles: [
35-
`
36-
// !!!IMPORTANT!!! Import this through your styles.scss or angular.json! This is just for demo purposes
37-
:host {
38-
::ng-deep {
39-
@import "demo";
40-
}
41-
}
42-
`,
43-
],
34+
// gridstack.min.css and other custom styles should be included in global styles.scss or here
4435
})
4536
export class AngularNgForCmdTestComponent implements AfterViewInit {
46-
@ViewChildren("gridStackItem") gridstackItems!: QueryList<any>;
37+
/** list of HTML items that we track to know when the DOM has been updated to make/remove GS widgets */
38+
@ViewChildren("gridStackItem") gridstackItems!: QueryList<ElementRef<GridItemHTMLElement>>;
39+
40+
/** set the items to display. */
4741
@Input() public items: GridStackWidget[] = [
48-
{ x: 0, y: 0, w: 1, h: 1 },
49-
{ x: 1, y: 1, w: 1, h: 1 },
50-
{ x: 2, y: 2, w: 1, h: 1 },
42+
{x: 0, y: 0},
43+
{x: 1, y: 1},
44+
{x: 2, y: 2},
5145
];
5246

5347
private grid!: GridStack;
5448
private widgetToMake: Subject<{
5549
action: "add" | "remove" | "update";
5650
id: number;
57-
}> = new Subject(); // consider to use a statemanagement like ngrx component store to do this
51+
}> = new Subject(); // consider to use a state management like ngrx component store to do this
5852

5953
constructor() {}
6054

@@ -79,9 +73,11 @@ export class AngularNgForCmdTestComponent implements AfterViewInit {
7973
}
8074
);
8175

82-
// TODO: the problem with this code is that our items list does NOT reflect changes made by GS (user directly changing, or conflict)
83-
// and believe the other ngFor example (which does track changes) is also cleaner as it doesn't require special action commands
84-
// and track both changes to happen using zip().
76+
// TODO: the problem with this code is that our items list does NOT reflect changes made by GS (user directly changing,
77+
// or conflict during initial layout) and believe the other ngFor example (which does track changes) is also cleaner
78+
// as it doesn't require user creating special action commands nor track 'both' changes using zip().
79+
// TODO: identify() uses index which is not guaranteed to match between invocations (insert/delete in
80+
// middle of list instead of end as demo does)
8581
}
8682

8783
/**
@@ -103,6 +99,7 @@ export class AngularNgForCmdTestComponent implements AfterViewInit {
10399
this.grid.update(updateEl!, { w: 2 });
104100
}
105101

102+
// ngFor lookup indexing
106103
identify(index: number) {
107104
return index;
108105
}

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h1>Angular src Demos</h1>
3737
<ol>
3838
<li><a href="angular/src/app/simple.ts">simple.ts</a></li>
3939
<li><a href="angular/src/app/ngFor.ts">ngFor.ts</a></li>
40+
<li><a href="angular/src/app/ngFor_cmd.ts">ngFor with command (not recommended)</a></li>
4041
</ol>
4142
<h1>Old v5.1.1 Jquery Demos</h1>
4243
Note: those are no longer supported, and use an old version of the lib to compare functionality.

0 commit comments

Comments
 (0)