how to get click event of row element of jstree?

孤人 提交于 2019-12-06 18:52:54

问题


Can you please tell me how how to get click event of row element of jstree ? I make a demo of jstree in my fiddle .it is made in panel .you have to press "open panel " button to check panel

I want to click event of jstree element to get it id on click ? For preparation of tree I have to press "add test case button" many times and then press "open panel" button.

here is my fiddle http://jsfiddle.net/ZLe2R/6/

function addMenuItemsOfTestSuit(id){

 var menuid = "menu_" + id;
       var ref = $('#tree').jstree(true);
        alert('thank')
    ref.create_node("#", {"id" : menuid, "text" : id});

        ref.deselect_all();

}

回答1:


Use this event listener:

$('#tree').on("select_node.jstree", function (e, data) { alert("node_id: " + data.node.id); });

Look jsTree API events for a list of events.

EDIT: created a fiddle: http://jsfiddle.net/y7ar9/4/




回答2:


You can use

$(document).on('click', '.jstree-anchor', function(e) {...});

You may want to move your click handler to its own function and get the id from the anchor's parent:

$(document).on('click', '.jstree-anchor', function(e) {
    var anchorId = $(this).parent().attr('id');
    var clickId = anchorId.substring(anchorId.indexOf('_') + 1, anchorId.length);
    onMenuItemClick(clickId, e);
});
$(document).on('click', '.clickTestCaseRow', function (e) {
    onMenuItemClick(this.id, e);
});
function onMenuItemClick(clickId, e) {
    hideDisplayView();
    displayNewView(clickId);
    e.stopPropagation();
}

Here is a fiddle.




回答3:


Personally I like event 'activate_node' instead. if you do a postback on node selection change and the page is reloaded and the node is still selected it will not cause another event to fire causing an endless postback loop.

$('#jstree').on('activate_node.jstree', function (e, data) {
     if (data == undefined || data.node == undefined || data.node.id == undefined)
                return;
    alert('clicked node: ' + data.node.id);
});


来源:https://stackoverflow.com/questions/23456628/how-to-get-click-event-of-row-element-of-jstree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!