Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsTree dnd events not firing

I'm trying to capture dnd events in jsTree 3.0.0. I used the demo code to build event handlers. The tree builds fine, but the events never fire. What am I missing?

This is the pertinent part. The JSON works fine and builds the tree itself find. However, the console.log calls never occur when I drag and drop on the tree.

<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>                   
like image 957
GeoJunkie Avatar asked Apr 29 '14 18:04

GeoJunkie


2 Answers

The event is only triggered on the "document". Try this:

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

It works!

like image 62
Sandro Stadler Avatar answered Jan 04 '23 12:01

Sandro Stadler


In the documentation https://www.jstree.com/api/#/?q=event&f=dnd_start.vakata

DnD events (dnd_start, dnd_move, dnd_stop) are triggered on document, not on tree.

These events are differents from "move_node.jstree" event, which is called only at the end of Drag & Drop (equivalent to Drop event).

$(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);
});
like image 42
Lia Stk Avatar answered Jan 04 '23 13:01

Lia Stk