Gridstack.js使用Jquery删除小部件

Aji*_*her 5 jquery plugins

下面是我正在使用的Jquery函数我不知道如何使用remove_widget请帮助:我无法找到任何有关删除小部件的帮助,而Knockout.js不是我使用的,因为我没有使用它.

$(function () {
var options = {resizable: {
handles: 'e, se, s, sw, w'
}
};

$('.grid-stack').gridstack(options);

        new function () {
            this.serialized_data = [
                {x: 0, y: 0, width: 2, height: 2},
                {x: 3, y: 1, width: 1, height: 2},
                {x: 4, y: 1, width: 1, height: 1},
                {x: 2, y: 3, width: 3, height: 1},
                {x: 1, y: 4, width: 1, height: 1},
                {x: 1, y: 3, width: 1, height: 1},
                {x: 2, y: 4, width: 1, height: 1},
                {x: 2, y: 5, width: 1, height: 1}
            ];

            this.grid = $('.grid-stack').data('gridstack');

            this.load_grid = function () {
                this.grid.remove_all();
                var items = GridStackUI.Utils.sort(this.serialized_data);
                _.each(items, function (node) {
                    this.grid.add_widget($('<div><div class="grid-stack-item-content" /><div/>'),
                        node.x, node.y, node.width, node.height);
                }, this);
            }.bind(this);

            this.save_grid = function () {
                this.serialized_data = _.map($('.grid-stack > .grid-stack-item:visible'), function (el) {
                    el = $(el);
                    var node = el.data('_gridstack_node');
                    return {
                        x: node.x,
                        y: node.y,
                        width: node.width,
                        height: node.height
                    };
                }, this);
                $('#saved-data').val(JSON.stringify(this.serialized_data, null, '    '));
            }.bind(this);

            this.clear_grid = function () {
                this.grid.remove_all();
            }.bind(this);
            this.add_grid = function(){
                this.grid.add_widget($('<div><div class="grid-stack-item-content" /><i class=" fa fa-remove"></><div/>'), 0, 0, 3, 2, true);
                $('.fa-remove').click(this.remove_grid);
            }.bind(this);
            this.remove_grid = function(){
                this.grid.remove_widget("What should I use Here", true);
            }.bind(this);
            $('#save-grid').click(this.save_grid);
            $('#load-grid').click(this.load_grid);
            $('#clear-grid').click(this.clear_grid);
            $('#add-grid').click(this.add_grid);
            $('.fa-remove').click($(this).remove_grid);
            this.load_grid();
        };
    });
Run Code Online (Sandbox Code Playgroud)

Kem*_*min 7

假设您动态添加项目; 假设你有一个带有'remove'类的触发元素;

$('body').on('click', '.remove', function (e) {
    e.preventDefault();
    var grid = $('.grid-stack').data('gridstack'),
        el = $(this).closest('.grid-stack-item')

    grid.remove_widget(el);
});
Run Code Online (Sandbox Code Playgroud)

注意:缩小您的上下文到实际容器并引用它而不是$('body')

函数remove_widget自v0.2.5起已弃用,并已替换为removeWidget.它将在v1.0中完全删除.


ᴍᴀᴛ*_*ᴋᴇʀ 5

我知道这是一个老问题,但我想我会分享我的方法,这可能会对某人有所帮助。

var $eGridstack = $(".grid-stack");

$(".btn-delete").click(function ()
{
    let $widget = $(this).closest(".grid-stack-item");

    if (confirm("Are you sure you wish to delete this widget?") == true)
    { 
        $eGridstack.data('gridstack').removeWidget($widget);
    }
}
Run Code Online (Sandbox Code Playgroud)

注释remove_widget已被弃用。

remove_widget从 v0.2.5 开始不推荐使用函数,并已替换为removeWidget. 它将在 v1.0 中完全删除。