jsTree拖放事件无法触发

8
我正在尝试在jsTree 3.0.0中捕获拖放事件。我使用演示代码构建事件处理程序。树状结构构建良好,但事件从未触发。我错过了什么吗?
这是相关的部分。JSON工作正常,并且能够成功构建树形结构本身。然而,当我在树上拖放时,console.log调用从未发生。
<link href="/jquery/jquery-ui-1.10.3/css/ui-lightness/jquery-ui-1.10.3.custom.min.css" rel="Stylesheet"/>
<script src="/jquery/jquery-ui-1.10.3/js/jquery-1.9.1.js"/>
<script src="/jquery/jquery-ui-1.10.3/js/jquery-ui-1.10.3.custom.min.js"/>
<link href="/jquery/plugins/jsTree/themes/default/style.min.css" rel="stylesheet"/>
<script src="/jquery/plugins/jsTree/jstree.js"/>
<script>
    $(function () {
        $('#jstree')
        // listen for events
        .on('dnd_start.vakata', function (e, data) {
            console.log('Dragged');
        })
        .on('dnd_stop.vakata', function (e, data) {
            console.log('Dropped');
        })
        // create the instance
        .jstree({
            "core" : {
                "check_callback" : true,
                'data' : {                              
                    'url' : 'categoriesJson.pm?json=1',
                    'data' : function(node) {
                        console.log (node);
                        return {
                            'id' : node.id
                        }
                    }
                },
                "themes" : {
                    "stripes" : true
                }
            },
            "plugins" : [ "dnd" ]
        });

        $(document).bind("drag_stop.vakata", function (e, data) {
            if (data.data.jstree) {
                console.log('User stopped dragging');
            }
        });                 
    });
</script>                   

2
好的,我发现触发的是 move_node 事件。所以这样可以: .on('move_node.jstree', function (e, data) { console.log('Moved'); }) 不过我还是很好奇为什么 vakata 文档中的事件不起作用。 - GeoJunkie
2个回答

23

该事件仅在“document”上触发。 尝试这样做:

$(document).on('dnd_start.vakata', function (e, data) {
    console.log('Started');
});

它有效!


10
在文档https://www.jstree.com/api/#/?q=event&f=dnd_start.vakata中,DnD事件(dnd_start、 dnd_move、 dnd_stop)在文档上触发,而不是树上触发。
这些事件与“move_node.jstree”事件不同,后者仅在拖放结束时调用(相当于Drop事件)。
$(document).bind("dnd_start.vakata", function(e, data) {
    console.log("Start dnd");
})
.bind("dnd_move.vakata", function(e, data) {
    console.log("Move dnd");
})
.bind("dnd_stop.vakata", function(e, data) {
    console.log("Stop dnd");
});
$("#tree").jstree({
    // tree...
}).bind("move_node.jstree", function(e, data) {
   console.log("Drop node " + data.node.id + " to " + data.parent);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接