Skip to content

Commit 09d19a0

Browse files
committed
Nested grid support with content update
* fix #2384 * you can now update the widget content while having a nested grid * added an example showing the issue
1 parent 3aeef45 commit 09d19a0

File tree

3 files changed

+66
-12
lines changed

3 files changed

+66
-12
lines changed

spec/e2e/html/2357_rem.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<head>
33
<title>REM demo</title>
4-
<link rel="stylesheet" href="../../../demo.css"/>
4+
<link rel="stylesheet" href="../../../demo/demo.css"/>
55
<script src="../../../dist/gridstack-all.js"></script>
66
</head>
77
<body>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<title>Nested grid update content</title>
4+
<link rel="stylesheet" href="../../../demo/demo.css"/>
5+
<link rel="stylesheet" href="../../../dist/gridstack-extra.min.css"/>
6+
<style type="text/css">
7+
.grid-stack {
8+
background: lightgoldenrodyellow;
9+
}
10+
.grid-stack-item-content {
11+
color: #2c3e50;
12+
text-align: center;
13+
background-color: #18bc9c;
14+
}
15+
.grid-stack.grid-stack-nested {
16+
position: absolute;
17+
inset: 20px 0 0 0;
18+
border: 1px solid black;
19+
}
20+
</style>
21+
<script src="../../../dist/gridstack-all.js"></script>
22+
</head>
23+
<body>
24+
<h1>Nested grid when updating content</h1>
25+
<div>
26+
<button onClick="updateContent()">Update Subgrid</button>
27+
<button onClick="move()">move Subgrid</button>
28+
</div>
29+
<br/>
30+
<div class="grid-stack"></div>
31+
<script type="text/javascript">
32+
var options = {
33+
cellHeight: 70,
34+
acceptWidgets: true,
35+
margin: 5,
36+
children: [
37+
{x:0, y:0, w:2, h:2, content: 'original content', subGridOpts: {
38+
children: [{x:0, y:0, content: '1'}, {x:1, y:0, content: '2'}], cellHeight: 50, column: 2, margin: 5, acceptWidgets: true,
39+
}},
40+
]
41+
};
42+
43+
var grid = GridStack.init(options);
44+
45+
updateContent = function() {
46+
const n = grid.engine.nodes[0];
47+
grid.update(n.el, {content: "updated content"});
48+
}
49+
50+
move = function() {
51+
const n = grid.engine.nodes[0];;
52+
grid.update(n.el, {x: n.x+1});
53+
}
54+
</script>
55+
</body>
56+
</html>

src/gridstack.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,8 @@ export class GridStack {
11701170
}
11711171

11721172
GridStack.getElements(els).forEach(el => {
1173-
if (!el || !el.gridstackNode) return;
1174-
let n = el.gridstackNode;
1173+
let n = el?.gridstackNode;
1174+
if (!n) return;
11751175
let w = Utils.cloneDeep(opt); // make a copy we can modify in case they re-use it or multiple items
11761176
delete w.autoPosition;
11771177

@@ -1191,17 +1191,15 @@ export class GridStack {
11911191
}
11921192

11931193
// check for content changing
1194-
if (w.content) {
1195-
let toRemove = el.querySelectorAll('.grid-stack-item-content > :not(.grid-stack-nested, style)');
1196-
const subGrid = el.querySelector('.grid-stack-item-content > .grid-stack-nested')
1194+
if (w.content !== undefined) {
11971195
const itemContent = el.querySelector('.grid-stack-item-content');
1198-
if (toRemove) {
1199-
toRemove.forEach(child => itemContent.removeChild(child));
1196+
if (!itemContent || itemContent.innerHTML === w.content) return;
1197+
itemContent.innerHTML = w.content;
1198+
// restore any sub-grid back
1199+
if (n.subGrid?.el) {
1200+
itemContent.appendChild(n.subGrid.el);
1201+
if (!n.subGrid.opts.styleInHead) n.subGrid._updateStyles(true); // force create
12001202
}
1201-
const tempEl = document.createElement("div");
1202-
tempEl.innerHTML = w.content;
1203-
tempEl.childNodes.forEach(childNode => itemContent.insertBefore(childNode, subGrid));
1204-
12051203
delete w.content;
12061204
}
12071205

0 commit comments

Comments
 (0)