How to build grid from Gridstack with saved html(如何使用保存的 html 从 Gridstack 构建网格)
问题描述
我一直在使用 Gridstack 来动态创建网格.我使用以下函数来序列化网格及其数据.但我似乎无法弄清楚如何从它创建的 JSON 数组构建我的网格和它的内容.我检查了 https://github.com/troolee/gridstack.js#load-grid-from-array,但添加内容部分是整个问题.
I've been using Gridstack for dynamically creating a grid. I have used the following function to serialize the grid and it's data. But I can't seem to figure out how to build my grid and its content from the JSON array it created. Ive checked https://github.com/troolee/gridstack.js#load-grid-from-array, but adding the content part is the whole problem.
function saveData() {
var s_data = [];
$('.grid-stack-item.ui-draggable').each(function () {
var $this = $(this);
s_data.push({
x: $this.attr('data-gs-x'),
y: $this.attr('data-gs-y'),
w: $this.attr('data-gs-width'),
h: $this.attr('data-gs-height'),
content: $('.grid-stack-item-content', $this).html()
});
});
}
这将创建以下数组:
[
{"x":"0","y":"0","w":"4","h":"2","content":"<h1>Test title for content</h1>"},
{"x":"4","y":"0","w":"4","h":"4","content":""}
];
所以我的问题是:如何使用这个数组构建我的网格,包括它的内容?
So my question is: how can I build my grid, including its content, using this array?
推荐答案
我回答了一个类似的问题 这里.
I answered a similar question here.
在你的情况下,你可以在gridstack序列化示例提供的load函数中使用如下代码:
In your case, you can use the following code in the load function provided by the gridstack serialization example:
this.load_grid = function () {
var items = GridStackUI.Utils.sort( this.s_data );
this.grid.remove_all();
_.each( items, function( node ) {
this.grid.add_widget( jQuery( '<div class="grid-stack-item"><div class="grid-stack-item-content">' + node.content + '</div></div>' ), node.x, node.y, node.width, node.height );
}, this );
}.bind( this );
这篇关于如何使用保存的 html 从 Gridstack 构建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用保存的 html 从 Gridstack 构建网格


基础教程推荐
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01