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

Adding an item to Gridster.js grid with native add_widget causing unknown errors

本文关键字:添加 网格 错误 未知 js Gridster 本机 add widget      更新时间:2023-09-26

我整个上午都在尝试将一个简单的项目添加到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
});

这不是获取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);