I have a grid of styled books and their images, created with the gridstack library:
I also have a "save" button that calls a saveGrid()
function to save the location of each book on the grid, and loads its serialized data with grid.load(serializedData)
HTML:
<div>
<h1 class="title">Your Books</h1>
<button onclick="saveGrid(); grid.load(serializedData)">Save</button>
</div>
<div class="grid-stack"></div>
JS:
saveGrid = () => {
serializedData = [];
grid.engine.nodes.forEach((node) => {
serializedData.push({
x: node.x,
y: node.y,
width: node.width,
height: node.height,
noResize: true,
el: node.el
});
});
};
The problem is that, when the "save" button is clicked, the positions of the items are saved but not the actual HTML images and content (like buttons), as shown below:
I've figured out the problem and edited my
saveGrid()
function, as well as made a newloadGrid()
function that loads the grid after saving and anaddBooks()
function that loads books from an array upon page load. Below is the HTML and the full JS code:HTML:
JS: