OdinaSpb / jstree

Automatically exported from code.google.com/p/jstree
0 stars 0 forks source link

Cannot get selected node ID upon clicking the node #845

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Using jquery 1.4.2
2. Using jquery UI 1.8.6
3. Using jsTree.v.1.0rc2

What is the expected output? What do you see instead?

Upon node select I want to get the selected node id.  However, when clicking 
the node the select_node event fires and the selectNode method (see below) 
executes but the console log shows an empty string instead of the node id.

Tree setup:

                var jsondata = buildNode_JSON(msg[0]); // Below

                $("#divtree").jstree({ 
                    "core" : {"html_titles" : true }, 
                    "json_data" : {
                        "data" : jsondata
                    },
                    "contextmenu": {
                      "items": createContextMenu 
                    },
                    "plugins" : ["themes", "json_data", "ui", "dnd", "crrm", "contextmenu"]
                }).bind("select_node.jstree",  function(event, data) { selectNode(event, data); } ); 

... more code

// selectNode function

        function selectNode(event, data){
            console.log(data.rslt.obj.attr('id'));
            return false;
        }

// Functions that retrieve nodes from server and parse for tree
        function buildNode_JSON(node){  // Start at the root node and recursively build JSON for tree
            var childnodes = new Array();
            var children = getChildren(node);
            for (var k = 0; k < children.length; k++){
                childnodes.push(buildNode_JSON(children[k]));
            }            
            var edit = canEditNode(node.maskcode);            
            var editicon = (edit)? "&nbsp;<img src='treeicons/square_greenS.gif' alt='Icon indicator for editable elements.' align='middle'/>" : "";
            var title = node.title + editicon;
            var state = ($.inArray(node.id, m_opennodes) >-1)? "open" : "closed";

            var attributes = { "id" : "node_" + node.id, "data-title" : node.title, "data-maskcode" : node.maskcode, "data-shortname" : node.shortname, "data-editmode" : edit};
            if (childnodes.length > 0){
                return { "data" : { "title" : title, "attr" : attributes, "icon" : "treeicons/" + node.icon, "state" : state}, "children" : childnodes };
            }
            else {
                return { "data" : { "title" : title, "attr" : attributes, "icon" : "treeicons/" + node.icon, "state" : state}};
            }

        }

        // Node helper functions            
        function getChildren(node){
            var children = new Array();
            var separators = (node.maskcode.indexOf(".") > 0)?  node.maskcode.match(/\./g).length : 0;
            for (var i = 0; i < m_node_cache.length; i++){
                if ( (m_node_cache[i].maskcode.substr(0, node.maskcode.length) == node.maskcode ) && "node_" + m_node_cache[i].id != "node_" + node.id ){ // It is a descendant
                    if (m_node_cache[i].maskcode.match(/\./g).length <= separators + 1){ // immediate-descendants only 
                        children.push( m_node_cache[i] );
                    }
                }
            }
            return children;
        }

What version of the product are you using? On what browser?
jsTree.v.1.0rc2
IE 7

Please provide any additional information below.

Sample of JSON data returned by recursive buildNode_JSON function:

{"data":{"title":"Allied Medical 
Services","attr":{"id":"node_1422","data-title":" Allied Medical 
Services","data-maskcode":"1","data-shortname":"AMS","data-editmode":false},"ico
n":"treeicons/hqgroup.gif","state":"open"},
    "children":[
        {"data":{"title":"Northern Regional Medical Services","attr":{"id":"node_1423","data-title":"Northern Regional Medical Services","data-maskcode":"1.1","data-shortname":"NAMS","data-editmode":false},"icon":"treeicons/reggroup.gif","state":"closed"},
            "children":[
                {"data":{"title":"Director of Operations","attr":{"id":"node_1424","data-title":"Director of Operations","data-maskcode":"1.1.1","data-shortname":"DCR1","data-editmode":false},"icon":"treeicons/localgroup.gif","state":"closed"}},
                {"data":{"title":"Chief of Staff","attr":{"id":"node_1425","data-title":"Chief of Staff","data-maskcode":"1.1.2","data-shortname":"WAMSCOS1","data-editmode":false},"icon":"treeicons/folder.gif","state":"closed"},
                    "children":[
                        {"data":{"title":"Human Resources","attr":{"id":"node_1426","data-title":"Human Resources","data-maskcode":"1.1.2.1","data-shortname":"WAMSHRG1","data-editmode":false},"icon":"treeicons/Folder.gif","state":"closed"}},
                        {"data":{"title":"Clinical Operations","attr":{"id":"node_1427","data-title":"Clinical Operations","data-maskcode":"1.1.2.2","data-shortname":"WAMSOPS1","data-editmode":false},"icon":"treeicons/folder.gif","state":"closed"}},
                        {"data":{"title":"Logistics","attr":{"id":"node_1428","data-title":"Logistics","data-maskcode":"1.1.2.3","data-shortname":"WAMSLOG1","data-editmode":false},"icon":"treeicons/folder.gif","state":"closed"}},
                        {"data":{"title":"Information Technology","attr":{"id":"node_1429","data-title":"Information Technology","data-maskcode":"1.1.2.4","data-shortname":"WAMSIT1","data-editmode":false},"icon":"treeicons/folder.gif","state":"closed"}}]}]}

Original issue reported on code.google.com by bikemoj...@yahoo.com on 13 Dec 2010 at 11:13

GoogleCodeExporter commented 9 years ago
You do not have IDs set on the actual LI node (your attr object is inside the 
"data" section, move it out of the data section) Or look for 
data.rslt.obj.children('a').attr('id').

Kindest regards,
Ivan

Original comment by ivan.bozhanov on 8 Feb 2011 at 10:21