使用本机add_widget将项添加到Gridster.js网格会导致未知错误

时间:2015-12-24 15:24:30

标签: javascript html5 drag-and-drop gridster

我一直在尝试将一个简单的项目添加到gridster网格,但是在参考gridsterjs时遇到错误'add_widget有人可以告诉我有关如何动态添加小部件的详细信息吗?

也许我没有正确地实例化或缺少依赖...

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

    // errors why?
    gridster.add_widget.apply(gridster, ['<li class="gw_s">The HTML of the widget...</li>', 2, 1]); // Cannot read property 'apply' of undefined
    gridster.add_widget('<li class="gw_s">The HTML of the widget...</li>', 2, 1); // Uncaught TypeError: gridster.add_widget is not a function

});

1 个答案:

答案 0 :(得分:1)

这不是你获得gridster对象的方法。试试这个:

$(".gridster ul").gridster({
    widget_margins: [10, 10],
    widget_base_dimensions: [140, 140]
});
var gridster = $(".gridster ul").gridster().data('gridster');

现在您可以使用函数gridster.add_widget(html, 2, 1);