1

我想使用 gridstack 创建一个带有小部件的页面。我的问题是当我“懒惰”加载widgeds时,gridstack不会初始化这个新的dom节点。(它们不可移动)有人可以解释一下我是如何做到这一点的吗?我认为我的问题是 $('.grid-stack').gridstack(options); 正如我在这里看到的,只执行一次:https ://github.com/troolee/gridstack.js/blob/master/src/gridstack.js#L1727

或者是否存在已经意识到“延迟”加载的框架。

索引.html:

<div class="grid-stack" id="widgeds">
    <div class="grid-stack-item ui-draggable ui-resizable ui-resizable-autohide ui-resizable-disabled"
        data-gs-x="0" data-gs-y="0"
        data-gs-width="1" data-gs-height="1">
            <div class="grid-stack-item-content ui-draggable-handle">Item 1</div>
    </div>
    <div class="grid-stack-item"
        data-gs-x="1" data-gs-y="0"
        data-gs-width="1" data-gs-height="1">
            <div class="grid-stack-item-content">Item 2</div>
    </div>
</div>
<script type="text/javascript">
        var girdstackRefresh = function() {
            var options = {
                cell_height: 200,
                vertical_margin: 10
            };
            $('.grid-stack').gridstack(options);
        }
        $(function () {
            girdstackRefresh();

            loadWidged('/widgeds/chat', function() {
                girdstackRefresh();
            });

        });
//function for lazy loading:
var widgeds = {};
function loadWidged(srcUrl, onloaded) {
    $.get(srcUrl+"/meta.wjs", function(meta) {
        code = "var foo = function() { return "+meta+"; }\nfoo();";
        widged = eval(code);
        if(widged.singleton) {
            if(widgeds[widged.uid]) {
                throw({'message' : 'Error widged can only be loaded once'});
            }
        }
        widged.url = srcUrl;
        widged.init.bind(widged);
        var ui = $('<div class="grid-stack-item ui-draggable ui-resizable ui-resizable-autohide ui-resizable-disabled" data-gs-x="5" data-gs-y="1" data-gs-width="4" data-gs-height="2">')
        $("#widgeds").append(ui);
        widged.dom = ui;
        widged.init();
        widgeds[widged.uid] = widged;
        onloaded();
    });
}
        </script>

Widged 示例:

聊天/meta.wjs:

{
    "uid"       : "SockIoChat",
    "singleton" : true,
    "init"      : function() {
        console.log("debug:");
        console.log(this.url);
//        $.loadCSS('style.css');
        this.dom.load(this.url+"/chat.html")
    }
}

聊天/聊天.html:

<div class="grid-stack-item-content ui-draggable-handle">Item 2</div>
4

2 回答 2

0

这可以通过使用文档中的addWidgetormakeWidget函数来完成:

https://github.com/troolee/gridstack.js/tree/develop/doc#addwidgetel-xy-width-height-autoposition-minwidth-maxwidth-minheight-maxheight-id

https://github.com/troolee/gridstack.js/tree/develop/doc#makewidgetel

makeWidget如果要将现有元素转换为 gridstack 元素,则使用该元素,而addWidget将创建该元素并使其成为小部件。

于 2017-05-04T20:16:52.673 回答
0

只需在完成延迟加载后将其加载为回调或其他内容,或使用 setTimeout 函数对其进行初始化。

于 2017-04-26T10:26:40.253 回答