2016-04-23 8 views
1

Ich möchte die Attribute jedes Knotens in meinem Baum abrufen. Nachdem ich online geschaut habe, habe ich einen Weg gefunden, es zu tun, aber es funktioniert nicht. Der Fehler ist (an der Linie von attr („Beschreibung“):So erhalten Sie die Knotenattribute in Jstree JSON-Daten

Uncaught TypeError: Cannot read property 'obj' of undefined 

Hier ist mein Code:

jQuery(document).ready(function() { 
    var $ = jQuery; 
    $('#jstree').jstree({ 'core' : { 
    'data' : [ 
     {"id":"parent","parent":"#","text":"parent"}, 
     {"id":"cs","text":"Short Stay","parent":"parent","li_attr":{"label":"Short Stay","description":"example"}}, 
     {"id":"ls","text":"ls","parent":"parent"},{"id":"cs_1","text":"cs_1","parent":"cs"}, 
     {"id":"ls_1","text":"ls_1","parent":"ls"},{"id":"cs_1_1","text":"cs_1_1","parent":"cs_1"}, 
     {"id":"cs_1_1_1","text":"cs_1_1_1","parent":"cs_1_1"}, 
     {"id":"cs_1_1_2","text":"cs_1_1_2","parent":"cs_1_1"} 
    ] 
} }) 
.on("select_node.jstree", 
    function(evt, data){ 
      $('#data').html(data.rslt.obj.attr("description")); 
    } 
); 
    }); 
+0

meinst du Attributwerte oder Attributnamen? –

Antwort

5

Sie müssen nur die ID des ausgewählten Elements bekommen und dann bekommen das Attribut dieses Element:

$(function() { 
    $('#jstree').jstree({ 'core' : { 
    'data' : [ 
     {"id":"parent","parent":"#","text":"parent"}, 
     {"id":"cs","text":"Short Stay","parent":"parent","li_attr":{"label":"Short Stay","description":"example"}}, 
     {"id":"ls","text":"ls","parent":"parent"},{"id":"cs_1","text":"cs_1","parent":"cs"}, 
     {"id":"ls_1","text":"ls_1","parent":"ls"},{"id":"cs_1_1","text":"cs_1_1","parent":"cs_1"}, 
     {"id":"cs_1_1_1","text":"cs_1_1_1","parent":"cs_1_1"}, 
     {"id":"cs_1_1_2","text":"cs_1_1_2","parent":"cs_1_1"} 
    ] 
} }).on("select_node.jstree", 
    function(evt, data){ 
      var node_id = (data.node.id); // element id 
      var description = $("#"+node_id).attr("description"); // get value of element attribute 
      $('#data').html(description); 
    } 
); 
}); 

Beachten Sie, dass alle das Element nicht description Attribut hat

.