最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - not sure how to use gridster.serialize() with Gridster jQuery plugin - Stack Overflow

programmeradmin3浏览0评论

I've just started using the Gridster jQuery plugin and I'm having trouble using gridster.serialize(). According to the site, it is supposed to create a JavaScript array of objects with positions of all the widgets, ready to be encoded as a JSON string.

I just have some basic code:

        $(function(){ //DOM Ready
            $(".gridster ul").gridster({
                widget_margins: [10, 10],
                widget_base_dimensions: [140, 140]
            });
        });

With corresponding HTML:

    <div class="gridster">
        <ul>
        <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">1</li>
        <li data-row="2" data-col="1" data-sizex="1" data-sizey="1">1</li>
        <li data-row="3" data-col="1" data-sizex="1" data-sizey="1">1</li>

        <li data-row="1" data-col="2" data-sizex="2" data-sizey="1">2</li>
        <li data-row="2" data-col="2" data-sizex="3" data-sizey="2">2</li>

        <li data-row="1" data-col="4" data-sizex="1" data-sizey="1">3</li>
        <li data-row="2" data-col="4" data-sizex="2" data-sizey="1">3</li>
        <li data-row="3" data-col="4" data-sizex="1" data-sizey="1">3</li>
        </ul>
    </div>

And it's not entirely clear to me how the gridster.serialize() function is suppose to work. I'm just trying to record the current size/position of all the tiles in my grid and put it into an array of some kind. Anyone have any ideas?

EDIT: Here's the site.

I've just started using the Gridster jQuery plugin and I'm having trouble using gridster.serialize(). According to the site, it is supposed to create a JavaScript array of objects with positions of all the widgets, ready to be encoded as a JSON string.

I just have some basic code:

        $(function(){ //DOM Ready
            $(".gridster ul").gridster({
                widget_margins: [10, 10],
                widget_base_dimensions: [140, 140]
            });
        });

With corresponding HTML:

    <div class="gridster">
        <ul>
        <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">1</li>
        <li data-row="2" data-col="1" data-sizex="1" data-sizey="1">1</li>
        <li data-row="3" data-col="1" data-sizex="1" data-sizey="1">1</li>

        <li data-row="1" data-col="2" data-sizex="2" data-sizey="1">2</li>
        <li data-row="2" data-col="2" data-sizex="3" data-sizey="2">2</li>

        <li data-row="1" data-col="4" data-sizex="1" data-sizey="1">3</li>
        <li data-row="2" data-col="4" data-sizex="2" data-sizey="1">3</li>
        <li data-row="3" data-col="4" data-sizex="1" data-sizey="1">3</li>
        </ul>
    </div>

And it's not entirely clear to me how the gridster.serialize() function is suppose to work. I'm just trying to record the current size/position of all the tiles in my grid and put it into an array of some kind. Anyone have any ideas?

EDIT: Here's the site.

Share Improve this question edited Oct 17, 2013 at 15:48 aadu asked Oct 17, 2013 at 15:33 aaduaadu 3,25410 gold badges42 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

Got it.

        var gridster = $(".gridster ul").gridster().data('gridster');
        $(function(){ //DOM Ready
            $(".gridster ul").gridster({
                widget_margins: [10, 10],
                widget_base_dimensions: [140, 140],
                serialize_params: function($w, wgd) { 
                    return { 
                           id: $w.attr('id'), 
                           col: wgd.col, 
                           row: wgd.row, 
                           size_x: wgd.size_x, 
                           size_y: wgd.size_y 
                    };
                }
            })
            var gridster = $(".gridster ul").gridster().data('gridster');
            gridData = gridster.serialize();
            alert(gridData.toSource())
        }); 
发布评论

评论列表(0)

  1. 暂无评论