javascript - How to build grid from Gridstack with saved html - Stack Overflow

I've been using Gridstack for dynamically creating a grid. I have used the following function to s

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

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

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?

Share Improve this question asked May 21, 2015 at 16:17 SamSam 311 silver badge4 bronze badges 2
  • Just had to add node.content on the right place. Solved! – Sam Commented May 21, 2015 at 19:15
  • @ can you please elaborate your solution? – Souvik Ray Commented Oct 5, 2020 at 17:56
Add a ment  | 

1 Answer 1

Reset to default 6

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:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744507027a4577764.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信