Skip to content

Commit 637490a

Browse files
authored
Merge pull request #1719 from adumesny/master
`addWidget()` now supports recursive grids
2 parents 922c0b1 + d606790 commit 637490a

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

demo/nested.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<h1>Nested grids demo</h1>
2323
<p>This example uses new v3.1 API to load the entire nested grid from JSON, and shows dragging between nested grid items (pink) vs dragging higher grid items (green)</p>
2424
<p>Note: initial v3 HTML5 release doesn't support 'dragOut:false' constrain so use jq version if you need that.</p>
25+
<a class="btn btn-primary" onClick="addNested()" href="#">Add Widget</a>
2526
<a class="btn btn-primary" onClick="addNewWidget('.nested1')" href="#">Add Widget Grid1</a>
2627
<a class="btn btn-primary" onClick="addNewWidget('.nested2')" href="#">Add Widget Grid2</a>
2728
<a class="btn btn-primary" onClick="save()" href="#">Save</a>
@@ -53,6 +54,10 @@ <h1>Nested grids demo</h1>
5354
// create and load it all from JSON above
5455
let grid = GridStack.addGrid(document.querySelector('.container-fluid'), json);
5556

57+
addNested = function() {
58+
grid.addWidget({x:0, y:0, w:3, h:3, content:"nested add", subGrid: {children: sub1, dragOut: true, class: 'nested1', ...subOptions}});
59+
}
60+
5661
addNewWidget = function(selector) {
5762
let subGrid = document.querySelector(selector).gridstack;
5863
let node = {

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Change log
5656
## 4.1.0-dev
5757

5858
- fix [#1704](https://github.com/gridstack/gridstack.js/issues/1704) scrollbar fix broken in 4.x
59+
- add [#1682](https://github.com/gridstack/gridstack.js/issues/1682) `addWidget()` now supports recursive grids like init/addGrid() does.
5960

6061
## 4.1.0 (2021-4-7)
6162

src/gridstack.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export class GridStack {
415415
let domAttr = this._readAttr(el);
416416
options = {...(options || {})}; // make a copy before we modify in case caller re-uses it
417417
Utils.defaults(options, domAttr);
418-
this.engine.prepareNode(options);
418+
let node = this.engine.prepareNode(options);
419419
this._writeAttr(el, options);
420420

421421
if (this._insertNotAppend) {
@@ -427,6 +427,13 @@ export class GridStack {
427427
// similar to makeWidget() that doesn't read attr again and worse re-create a new node and loose any _id
428428
this._prepareElement(el, true, options);
429429
this._updateContainerHeight();
430+
431+
// check if nested grid definition is present
432+
if (node.subGrid && !(node.subGrid as GridStack).el) { // see if there is a sub-grid to create too
433+
let content = node.el.querySelector('.grid-stack-item-content') as HTMLElement;
434+
node.subGrid = GridStack.addGrid(content, node.subGrid as GridStackOptions);
435+
}
436+
430437
this._triggerAddEvent();
431438
this._triggerChangeEvent();
432439

@@ -538,10 +545,6 @@ export class GridStack {
538545
} else {
539546
w = this.addWidget(w).gridstackNode;
540547
}
541-
if (w.subGrid) { // see if there is a sub-grid to create too
542-
let content = w.el.querySelector('.grid-stack-item-content') as HTMLElement;
543-
w.subGrid = GridStack.addGrid(content, w.subGrid as GridStackOptions);
544-
}
545548
}
546549
});
547550

0 commit comments

Comments
 (0)