Skip to content

Commit 232fddb

Browse files
authored
Merge pull request #1780 from adumesny/master
Angular demos
2 parents c0db179 + 6dfba94 commit 232fddb

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

demo/angular-ngFor.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Example using Angular ngFor to loop through items and create DOM items
3+
*/
4+
import { Component, AfterViewInit, OnChanges, SimpleChanges, Input, ChangeDetectionStrategy } from '@angular/core';
5+
6+
import { GridStack, GridStackWidget } from 'gridstack';
7+
import 'gridstack/dist/h5/gridstack-dd-native';
8+
9+
@Component({
10+
selector: 'app-angular-ng-for-test',
11+
template: `
12+
<button (click)="add()">add item</button><button (click)="delete()">remove item</button><button (click)="change()">modify item</button>
13+
<div class="grid-stack">
14+
<!-- using angular templating to create DOM, otherwise an easier way is to simply call grid.load(items) -->
15+
<div *ngFor="let n of items; let i = index" class="grid-stack-item" [attr.gs-w]="n.w" [attr.gs-h]="n.h" [attr.gs-x]="n.x" [attr.gs-y]="n.y">
16+
<div class="grid-stack-item-content">item {{i}}</div>
17+
</div>
18+
</div>
19+
`,
20+
// gridstack.min.css and other custom styles should be included in global styles.scss
21+
22+
// tell Angular only @Input will change - doesn't help
23+
// changeDetection: ChangeDetectionStrategy.OnPush
24+
})
25+
export class AngularNgForTestComponent implements AfterViewInit, OnChanges {
26+
@Input() public items: GridStackWidget[] = [
27+
{ x: 0, y: 0, w: 9, h: 6},
28+
{ x: 9, y: 0, w: 3, h: 3},
29+
{ x: 9, y: 3, w: 3, h: 3},
30+
];
31+
private grid: GridStack;
32+
33+
constructor() {}
34+
35+
// wait until after DOM is ready to init gridstack - can't be ngOnInit() as angular ngFor needs to run first!
36+
public ngAfterViewInit() {
37+
this.grid = GridStack.init({
38+
cellHeight: 70,
39+
});
40+
}
41+
42+
/**
43+
* this would be easier with addWidget(), removeWidget(), update() but simulating angular change detection instead...
44+
*/
45+
public add() {
46+
// this SHOULD trigger ngOnChanges() but not seeing it... and doing ngDoCheck() seem extreme ?
47+
// https://www.reddit.com/r/angular/comments/azjefs/change_detection_for_arraysobjects/
48+
// https://angular.io/guide/lifecycle-hooks#docheck
49+
this.items = [...this.items, {x: 1, y: 6, w: 3}];
50+
// this.items.push({x: 1, y: 6, w: 3});
51+
}
52+
public delete() {
53+
this.items.pop(); // todo
54+
}
55+
public change() {
56+
this.items[0].w = 1; // todo
57+
}
58+
59+
public ngOnChanges(changes: SimpleChanges) {
60+
if (changes.items) {
61+
// TODO: ... figure what is new and call makeWidget(), old -> removeWidget(el,false) and changed -> update()
62+
console.log('items changed');
63+
}
64+
}
65+
}
66+

demo/angular.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Simple Angular Example using GridStack API directly
3+
*/
4+
import { Component, OnInit } from '@angular/core';
5+
6+
import { GridStack, GridStackWidget } from 'gridstack';
7+
import 'gridstack/dist/h5/gridstack-dd-native';
8+
9+
@Component({
10+
selector: 'app-angular-test',
11+
template: `
12+
<button (click)="add()">add item</button><button (click)="delete()">remove item</button><button (click)="change()">modify item</button>
13+
<div class="grid-stack"></div>
14+
`,
15+
// gridstack.min.css and other custom styles should be included in global styles.scss
16+
})
17+
export class AngularTestComponent implements OnInit {
18+
private items: GridStackWidget[] = [
19+
{ x: 0, y: 0, w: 9, h: 6, content: '0' },
20+
{ x: 9, y: 0, w: 3, h: 3, content: '1' },
21+
{ x: 9, y: 3, w: 3, h: 3, content: '2' },
22+
];
23+
private grid: GridStack;
24+
25+
constructor() {}
26+
27+
// simple div above doesn't require Angular to run, so init gridstack here
28+
public ngOnInit() {
29+
this.grid = GridStack.init({
30+
cellHeight: 70,
31+
})
32+
.load(this.items); // and load our content directly (will create DOM)
33+
}
34+
35+
public add() {
36+
this.grid.addWidget({w: 3, content: 'new content'});
37+
}
38+
public delete() {
39+
this.grid.removeWidget(this.grid.engine.nodes[0].el);
40+
}
41+
public change() {
42+
this.grid.update(this.grid.engine.nodes[0].el, {w: 1});
43+
}
44+
}
45+

demo/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<h1>Demos</h1>
99
<ul>
1010
<li><a href="anijs.html">AniJS</a></li>
11+
<li><a href="angular.ts">Angular (simple)</a></li>
12+
<li><a href="angular-ngFor.ts">Angular (ngFor partial)</a></li>
1113
<li><a href="cell-height.html">Cell Height</a></li>
1214
<li><a href="column.html">Column</a></li>
1315
<li><a href="float.html">Float grid</a></li>

0 commit comments

Comments
 (0)