How to add click event to jstree's asynchronous list (jQuery plugin)?

I want to add a click event to jstree asynchronous list items.

Ideal result: when I click elements in jstree, the contents of the element will be passed to the sql query as a parameter, and then the query is executed and displays the result set on the same page or on another page.

While I do not know how to implement it. I found the following code in jquery.tree.js. And I think I should change this event. But I do not know how to do this. Can you see the code and give me some tips or tricks?

$("#" + this.container.attr("id") + " li a")
 .live("click.jstree", function (event) { // WHEN CLICK IS ON THE TEXT OR ICON
  if(event.which && event.which == 3) return true;
  if(_this.locked) {
   event.preventDefault(); 
   event.target.blur();
   return _this.error("LOCKED");
  }
  _this.select_branch.apply(_this, [event.target, event.ctrlKey || _this.settings.rules.multiple == "on"]);
  if(_this.inp) { _this.inp.blur(); }
   event.preventDefault();
   event.target.blur();
   return false;
  })

Page Code:

<script type="text/javascript" >
    $(function () { 
        $("#async_json_1").tree({
            data : { 
                type : "json",
                opts : {
                    url : "twodimen.php"
                }
            },
            callback:{
                onselect: function(node,tree){

                }
            }
        });
    });
</script>

Many thanks.

+3
source share
2

onselect, node ( script)

(li) "node_1234", :

<script type="text/javascript" >
 $(function () { 
  $("#async_json_1").tree({
   data : { 
    type : "json",
    opts : {
     url : "twodimen.php"
    }
   },
   callback: {
      onselect: function(node, tree) {
         var id = $(node).attr("id").split("_")[1]; // that is 1234
         $.ajax({
            url: "your/url",
            data: "id="+id,
            success: function(data){
               alert("Great");
            }
         });
      }
   }
  });
 });
</script>

, , :

<script type="text/javascript" >
 $(function () { 
  $("#async_json_1").tree({
   data : { 
    type : "json",
    opts : {
     url : "twodimen.php"
    }
   }
  });
  $(".tree a").live("click", function(e) {
     // your code here
  }) 
 });
</script>
+5
.delegate("a","click", function(e) {
         console.log($(this).parent().attr("id").split("_")[1]); //get ID of clicked node
  })
0

Source: https://habr.com/ru/post/1732539/


All Articles