简体   繁体   English

如何使用保存的html从Gridstack构建网格

[英]How to build grid from Gridstack with saved html

I've been using Gridstack for dynamically creating a grid. 我一直在使用Gridstack来动态创建网格。 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. 但我似乎无法弄清楚如何建立自己的网格,并从它创建的JSON数组的内容。 Ive checked https://github.com/troolee/gridstack.js#load-grid-from-array , but adding the content part is the whole problem. 我已经检查了https://github.com/troolee/gridstack.js#load-grid-from-array ,但添加内容部分是整个问题。

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()
            });
        });         
    }

And that creates the following array: 这会创建以下数组:

[
    {"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 . 在这里回答了类似的问题。

In your case, you can use the following code in the load function provided by the gridstack serialization example: 在您的情况下,您可以在gridstack序列化示例提供的加载函数中使用以下代码:

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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用rowpan和colspan(gridstack)从网格生成HTML表 - Generating HTML Table from Grid with rowspan and colspan (gridstack) Gridstack:将小部件从一个网格拖到另一个网格中,嵌套一个 - Gridstack: Dragging widget from one grid into another, nested one 如何在 gridstack.js 中序列化和加载 HTML 元素的数据? - How to serialize and load an HTML element's data in gridstack.js? 如何从 html 文件中获取保存的数据? - How to get saved data from html file? 我可以使用像 Gridstack 这样的 JS 网格布局库和 Blazor 吗? 如果是怎么办? - Can I use a JS Grid layout library like Gridstack with Blazor ? If yes how? 如何在gridstack中创建一个文本框,使其适合整个grid-item-content? - How to create a textbox inside gridstack so that it fits the entire grid-item-content? GridStack JS设置网格框的顺序 - GridStack JS set order of grid boxes 如何在简单的网格中而不是从数据库的Web网格中显示图像。 图片以字节数组格式保存 - How to Show Image in a simple grid not web grid from database. Image saved in byte array format 如何将markdown保存的内容转换回html - How to convert contents saved from markdown back to html Gridstack.js 从 JSON 获取位置 - Gridstack.js get positions from JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM