简体   繁体   English

如何将click事件添加到jstree的(jQuery插件)异步列表?

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

I want to add click event to jstree 's asynchronous list items. 我想将click事件添加到jstree的异步列表项中。

The ideal result is: when i click the items in the jstree, the content of the item will be transfered to a sql query as a parameter, and then, the query is executed and display a result set in the same page or in another page. 理想的结果是:当我单击jstree中的项目时,项目的内容将作为参数转移到sql查询,然后执行查询并在同一页面或另一页面中显示结果集。

While I don't know how to implement it. 虽然我不知道如何实现它。 I found the following code in jquery.tree.js. 我在jquery.tree.js中找到了以下代码。 And I think I should modify the event. 我想我应该修改这个事件。 But i don't know how. 但我不知道怎么做。 Can you see the code and give me some suggestions or guidance? 你能看到代码并给我一些建议或指导吗?

$("#" + 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;
  })

The page code: 页面代码:

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

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

Thanks very much. 非常感谢。

You could use the callback method onselect which usually takes place when the node is clicked (even though you can also select it by script) 您可以使用回调方法onselect ,通常在单击节点时进行(即使您也可以通过脚本选择它)

if your nodes (the li) have an id of the form "node_1234", then: 如果您的节点(li)具有“node_1234”形式的id,则:

<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>

I just realized, that there is also an even simpler way of doing what you need: 我刚才意识到,还有一种更简单的方法可以满足您的需求:

<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>
.delegate("a","click", function(e) {
         console.log($(this).parent().attr("id").split("_")[1]); //get ID of clicked node
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM