Javascript jsTree onSelect 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10218818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:22:11  来源:igfitidea点击:

jsTree onSelect event

javascriptjstree

提问by Dylan Klomparens

I've been trying to get the text of a node that is selected in a jsTree. I am able to populate the tree and trigger the onSelect event, but I can't find out which node was clicked. I've seen examples on the net that use data.rslt.obj.attr("data")to fetch the text, however this is returning undefined for me. Additionally, when I try to get the selected node using .jstree('get_selected')I can't find the node text anywhere in the object. How can I get the node text?

我一直在尝试获取在 jsTree 中选择的节点的文本。我能够填充树并触发 onSelect 事件,但我无法找出单击了哪个节点。我在网上看到过data.rslt.obj.attr("data")用于获取文本的示例,但是这对我来说返回 undefined 。此外,当我尝试获取所选节点时,.jstree('get_selected')我无法在对象中的任何位置找到节点文本。如何获取节点文本?

Here is my onSelect callback function:

这是我的 onSelect 回调函数:

function onSelect(event, data)
{
    // Get the name of the equipment that was selected.
    var selected_node = $("#equipment_tree").jstree('get_selected');
    var equipment_name = data.rslt.obj.attr("data");
}

回答by shaochuancs

Update in 2018.

2018 年更新。

Thanks to @ProfK's comment, the API has changed in new version of jstree. In jstree v3.1.0 (or earlier), the API has changed to:

感谢@ProfK 的评论,新版本的 jstree 中的 API 发生了变化。在 jstree v3.1.0(或更早版本)中,API 已更改为:

$("#treeContainer").on(
        "select_node.jstree", function(evt, data){
            //selected node object: data.node;
        }
);


For jstree old version (before 2013).

对于 jstree 旧版本(2013 年之前)。

You can get the selected node object and its text by:

您可以通过以下方式获取选定的节点对象及其文本:

$("#treeContainer").bind(
        "select_node.jstree", function(evt, data){
            //selected node object: data.inst.get_json()[0];
            //selected node text: data.inst.get_json()[0].data
        }
);

回答by Behnam Mohammadi

jstree new version for get text from node should use data.node.text

用于从节点获取文本的 jstree 新版本应使用 data.node.text

$("#treeContainer").on("select_node.jstree",
     function(evt, data){
          alert(data.node.text);
     }
);

回答by Hyman

$("#equipment_tree").bind("select_node.jstree", function(evt, data){

             var i, j, r = [], ids=[];
                for(i = 0, j = data.selected.length; i < j; i++) {
                  r.push(data.instance.get_node(data.selected[i]).text);
                }
                alert('Selected: ' + r.join(', '));
           }
);

you have to try this.

你必须试试这个。

回答by mikus

With the current version it's best to use get_selectedwith full: true, which means that the method will return the full object(s), and not only id.

对于当前版本,最好使用get_selectedwith full: true,这意味着该方法将返回完整的对象,而不仅仅是 id。

So for example:

例如:

$("#treeNode").jstree('get_selected', true);

or:

或者:

$("#treeNode").jstree().get_selected(true);

Each element in the array will have all the properties as text or id.

数组中的每个元素都将具有文本或 id 形式的所有属性。

回答by MMeah

The click event doesn't pass any data with it, so the event object will need to be used.

click 事件不传递任何数据,因此需要使用事件对象。

.bind("click.jstree", function (event) {
    alert($(event.currentTarget).parent("li:first").text().trim());
});