How can I attach custom behaviour to a double click in jsTree?
Asked Answered
M

6

23

I'm using the jsTree jQuery plugin and want to execute code when the user double clicks a node.

I can't seem to get it to work. I found some documentation on a ondblclk event but it doesn't fire.

    browser.jstree(
            {
                plugins: ["themes", "json_data", "ui", "cookies"],
                callback:
                {
                    ondblclk: function (node, tree) {
                        if (!thisReportBrowserthis._isFoldersOnly) {
                            var f = node;
                        }
                    }
                }
            }
        );

How can I handle double click events with jstree?

Madeline answered 9/9, 2010 at 7:37 Comment(1)
It appears the documentation I found for the above code snippet was out-of-date.Madeline
M
25

It turns out I can do this:

jstree.bind("dblclick.jstree", function (event) {
   var node = $(event.target).closest("li");
   var data = node.data("jstree");
   // Do my action
});

node contains the li that was clicked and data contains the metadata with my info in it.

Madeline answered 9/9, 2010 at 23:59 Comment(1)
Data returns undefined.Libertylibia
E
6

'dblclick.jstree' doesn't exist in last version jsTree 1.0.

DoubleClick for node:

$("#yourtree").delegate("a","dblclick", function(e) {
  var idn = $(this).parent().attr("id").split("_")[1];
  alert(idn); //return NodeID    
});

Insert this if you want just dblclicked node

if (this.className.indexOf('icon') == -1) {  /* is the node clicked a leaf? */ }
Entopic answered 19/9, 2010 at 15:49 Comment(1)
I don't see any "id"s on my tree (I'm using the HTML_DATA plugin to create my tree and I didn't bother to create any IDs), but leaving out that code this works. ".on()" is the more modern jQuery way to do this, so do $("#yourtree").on("dblclick","a", function(e) {...});Hallucination
A
4

It's a bit different to get the data out for me, but otherwise GiddyUpHorsey's answer was spot-on. Here is the code again:

        jstree.bind("dblclick.jstree", function (e, data) {
            var node = $(e.target).closest("li");
            var id = node[0].id; //id of the selected node
        });
Abscess answered 13/4, 2011 at 16:49 Comment(1)
same for me, except remove the data from the callback as it is undefinedCourtland
S
2

The above answers do not work on the latest version of jstree (which is 3.3.4)
This cost me a day of mind bending work but I finally got it. Here is working doubleclick to Edit code:

$('#tree1').bind("dblclick.jstree", function (event) {
  var tree = $(this).jstree();
  var node = tree.get_node(event.target);
  tree.edit(node);
});

and here is a working jsfiddle.

Stipel answered 15/11, 2017 at 22:22 Comment(1)
Your Code Snippet is not working, but the Fiddle. Solution works fine for me.Altis
S
1

as version 3.3.5, I'm using this one:

        $('#jstree').on("dblclick.jstree", function (e) {
            var instance = $.jstree.reference(this),
            node = instance.get_node(e.target);
            // Code
        });
Saito answered 18/2, 2018 at 20:45 Comment(0)
W
0

It worked for me

$("#MyTree").on('dblclick','.jstree-anchor', function (e) {
    var instance = $.jstree.reference(this),
    node = instance.get_node(this);
    console.log(node);
});
Waitress answered 22/4, 2020 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.