Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87b3517

Browse files
authored
Merge pull request gridstack#1287 from adumesny/develop
gridstack#1286 updated serialize demo to use Update()
2 parents caf7440 + eb73d4c commit 87b3517

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

demo/serialization.html

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,30 @@ <h1>Serialization demo</h1>
3535
});
3636

3737
var serializedData = [
38-
{x: 0, y: 0, width: 2, height: 2},
39-
{x: 3, y: 1, width: 1, height: 2},
40-
{x: 4, y: 1, width: 1, height: 1},
41-
{x: 2, y: 3, width: 3, height: 1},
42-
{x: 1, y: 3, width: 1, height: 1}
38+
{x: 0, y: 0, width: 2, height: 2, id: '0'},
39+
{x: 3, y: 1, width: 1, height: 2, id: '1'},
40+
{x: 4, y: 1, width: 1, height: 1, id: '2'},
41+
{x: 2, y: 3, width: 3, height: 1, id: '3'},
42+
{x: 1, y: 3, width: 1, height: 1, id: '4'}
4343
];
4444

4545
loadGrid = function() {
46-
grid.removeAll();
4746
var items = GridStack.Utils.sort(serializedData);
4847
grid.batchUpdate();
49-
items.forEach(function (node) {
50-
grid.addWidget('<div><div class="grid-stack-item-content"></div></div>', node);
51-
});
48+
49+
if (grid.engine.nodes.length === 0) {
50+
// load from empty
51+
items.forEach(function (item) {
52+
grid.addWidget('<div><div class="grid-stack-item-content">' + item.id + '</div></div>', item);
53+
});
54+
} else {
55+
// else update existing nodes (instead of calling grid.removeAll())
56+
grid.engine.nodes.forEach(function (node) {
57+
var item = items.find(function(e) { return e.id === node.id});
58+
grid.update(node.el, item.x, item.y, item.width, item.height);
59+
});
60+
}
61+
5262
grid.commit();
5363
};
5464

@@ -59,7 +69,8 @@ <h1>Serialization demo</h1>
5969
x: node.x,
6070
y: node.y,
6171
width: node.width,
62-
height: node.height
72+
height: node.height,
73+
id: node.id
6374
});
6475
});
6576
document.querySelector('#saved-data').value = JSON.stringify(serializedData, null, ' ');

0 commit comments

Comments
 (0)