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

How to build grid from Gridstack with saved html

本文关键字:Gridstack 构建 网格 html 何用 保存      更新时间:2023-09-26

我一直在使用Gridstack动态创建网格。我使用下面的函数来序列化网格和它的数据。但是我似乎无法弄清楚如何从它创建的JSON数组构建我的网格其内容。我检查了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()
            });
        });         
    }

创建如下数组:

[
    {"x":"0","y":"0","w":"4","h":"2","content":"<h1>Test title for content</h1>"},
    {"x":"4","y":"0","w":"4","h":"4","content":""}
];

所以我的问题是:我如何建立我的网格,包括它的内容,使用这个数组?

我在这里回答了一个类似的问题。

在您的示例中,可以在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 );