Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

javascript - JSTree Select Open Node

I have a problem with the JSTree.

I'm filling the selected item with data from DB. But I want the item I selected to open automatically after it is filled. I could not do the automatic opening part.

I added one button to the page (btn_test). I wrote the following in the click event of this button.

$("#TreeElement").jstree("toggle_node","#34B526D0-1E2C-4170-829E-1995782DB831");

When I click on the btn_test button, the node whose ID is written opens and closes. But I could not set this to open automatically when selecting and filling the corresponding node.

$("#btn_test").click(function () {
        $("#TreeElement").jstree("toggle_node","#34B526D0-1E2C-4170-829E-1995782DB831");
    });

JS side is as follows;

var TreeElement = $("#TreeElement");

TreeElement.jstree({
        "core" : {
          "check_callback" : true
        },
        state : { "opened" : true }
     });

TreeElement.bind("select_node.jstree", function (e, data) {

        GetSelectedElementChildrenNodes(data.node.id);

    });
 

function GetSelectedElementChildrenNodes(SelectedNodeId) {

    $.getJSON('http://serveripadress/api/browser.php?getchildren='+SelectedNodeId,function (data) {
        
        $.each(data,function (index,element) {
            TreeElement.jstree().create_node(SelectedNodeId
                , { "id" : element.nodeid, "text" : element.nodename}, "last");
            TreeElement.jstree(true).set_icon(element.nodeid,element.icon);

        });

    });
}

I get a different result when I change the code as follows. I click on Node1, it is filled with relevant data, but it does not open, then I click on Node2, but node1 is opening :)

TreeElement.bind("select_node.jstree", function (e, data) {

        GetSelectedElementChildrenNodes(data.node.id);
        $("#"+data.node.id).jstree("open_all");
    });

I've been changing the code for a while, but I just couldn't get it to work. If someone can help me with this, I really thank him.

Thank you so much!

question from:https://stackoverflow.com/questions/65908011/jstree-select-open-node

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem solved.

In the function where I pulled the data with Json, I added the following code to the end of getJson.

TreeElement.jstree("toggle_node",NodeId);

The new state of the function is as follows;

function GetSelectedElementChildrenNodes(SelectedNodeId) {

    $.getJSON('http://serveripadress/api/browser.php?getchildren='+SelectedNodeId,function (data) {
        
        $.each(data,function (index,element) {
            TreeElement.jstree().create_node(SelectedNodeId
                , { "id" : element.nodeid, "text" : element.nodename}, "last");
            TreeElement.jstree(true).set_icon(element.nodeid,element.icon);

        });

        
        TreeElement.jstree("toggle_node",NodeId);


    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...