如何使Gridster添加小部件可调整大小?

时间:2013-05-31 03:40:10

标签: javascript jquery gridster

我的项目基于此Fiddle

但我已经做到了,所以你可以使用:

添加小部件
$(document).on("click", "#add", function() {
var gridster = $(".layouts_grid ul").gridster().data('gridster');    

gridster.add_widget('<li class="layout_block" data-row="1" data-col="1" data-sizex="1" data-sizey="1"><div class="remove_element">X</div></li>', 1, 1)
    .resizable({ 
    grid: [grid_size + (grid_margin * 2), grid_size + (grid_margin * 2)],
    animate: false,
    minWidth: grid_size,
    minHeight: grid_size,
    containment: '#layouts_grid ul',
    autoHide: true,
    stop: function(event, ui) {
        var resized = $(this);
        setTimeout(function() {
            resizeBlock(resized);
        }, 300);
    } });

现在这个工作,它确实创建了一个新的小部件,但小部件无法调整大小。虽然它是用所需的类和id创建的,但我认为这是因为它是一个动态添加的小部件。有什么方法可以让它发挥作用吗?

感谢。

3 个答案:

答案 0 :(得分:3)

我会尝试这样的事情:

$(document).on("click", "#add", function() {
var gridster = $(".layouts_grid ul").gridster().data('gridster');    

gridster.add_widget('<li class="layout_block" id="block" data-row="1" data-col="1" data-sizex="1" data-sizey="1"><div class="remove_element">X</div></li>', 1, 1);
    $('#block').resizable({ 
    grid: [grid_size + (grid_margin * 2), grid_size + (grid_margin * 2)],
    animate: false,
    minWidth: grid_size,
    minHeight: grid_size,
    containment: '#layouts_grid ul',
    autoHide: true,
    stop: function(event, ui) {
        var resized = $(this);
        setTimeout(function() {
            resizeBlock(resized);
        }, 300);
    } });

答案 1 :(得分:1)

根据documentation from Gridster,您可以使用resize属性:

gridster.add_widget('<li class="layout_block" data-row="1" data-col="1" data-sizex="1" data-sizey="1"><div class="remove_element">X</div></li>', 1, 1)
    .resizable({ 
    ...
    stop: function(event, ui) {
        ...
    },
    resize: {
        enabled:true
    }
    ...
});

答案 2 :(得分:-1)

当您添加新窗口小部件时,您只需要在botom rigth角上添加小箭头的html代码...尝试这样的事情

var widget = gridster.add_widget('<li />', width, heigth, newWidgetCol, newWidgetRow);
var resizableWidgetHtml = '<span class="gs-resize-handle gs-resize-handle-both"></span>';
widget.attr('id', ui.draggable.id);
widget.html(htmlContent+resizableWidgetHtml);
widget.resize.enabled = true;