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

前端 未结 6 607
南方客
南方客 2020-12-31 01:18

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

相关标签:
6条回答
  • 2020-12-31 01:43

    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
            });
    
    0 讨论(0)
  • 2020-12-31 01:51

    '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? */ }
    
    0 讨论(0)
  • 2020-12-31 01:52

    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
            });
    
    0 讨论(0)
  • 2020-12-31 01:52

    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.

    0 讨论(0)
  • 2020-12-31 01:56

    It worked for me

    $("#MyTree").on('dblclick','.jstree-anchor', function (e) {
        var instance = $.jstree.reference(this),
        node = instance.get_node(this);
        console.log(node);
    });
    
    0 讨论(0)
  • 2020-12-31 02:00

    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.

    0 讨论(0)
提交回复
热议问题