How to retrive get_selected('full',true) attribute value

12,264 views
Skip to first unread message

sensor.evil sensor.evil

unread,
Mar 30, 2014, 12:47:58 PM3/30/14
to jst...@googlegroups.com
Hi,

I want to get those attribute of a selected node and store them in 3 variable:

- id
- text
- parent

if i want to get only the selected node "id" i do:
var selected = $('#tree').jstree(true).get_selected();  

Now, I've tryed to use the 'full' attribute, so that i can get a lot more data, but i can't get how to access those data from javascript.

If i use:
var selectedNode= $('#tree').jstree(true).get_selected('full',true)
from firebug i see this value for the variable "selectedNode":


selectedNode
           
a_attr          
Object { href="#"}            
children        
[]            
children_d      
[]            
data            
undefined            
icon            
true            
id              
"STZ2"            
li_attr        
Object { id="STZ2"}            
id              
"STZ2"            
original        
Object { id="STZ2", parent="MAG1", text="some text value"}            
id              
"STZ2"            
parent          
"MAG1"            
text            
"some text value"            
parent          
"MAG1"            
parents        
["MAG1", "#"]            
0               "MAG1"            
1               "#"            
state          
Object { loaded=true, opened=false, selected=true, altri elementi...}            
disabled        
false            
loaded          
true            
opened          
false            
selected        
true            
text            
"some text value"            
type            
"default"




but i don't know how to get id, parent and text value in 3 variable (in javascript).

Can someone help me?

Thanks. ;)
Message has been deleted

sensor.evil sensor.evil

unread,
Mar 31, 2014, 9:13:41 AM3/31/14
to jst...@googlegroups.com
Hi,

Thank you !

in the end i was doing this:

var selectedNode= $('#tree').jstree(true).get_selected('full',true);
selectedNode
= selectedNode[0];

var id = selectedNode.id;
var text = selectedNode.text;
var parent = selectedNode.parent;






but your code seems more correct.

Now i've only a little doubt on how to perform the insert/delete/edit action..

For the delete button i've done a js function that first do an ajax call, if is successfull then it'll remove the node with the api command.


function b_delete() {
$
.ajax({
  url
: '.....',
  type
: 'post',
 
...
  success
: function(){
 delete_node
(selectedNode);
 
},
  error
: function(){
 
}
});

}





But for the Rename (edit) button i use the both a function js and the rename_node.jstree Event:


function b_rename() {

rename_node
(selectedNode);

};



$
("#tree-container").bind("rename_node.jstree", function (e, data) {
 
// do stuff

$
.ajax({
  url
: '.....',
  type
: 'post',
 
...
  success
: function(){
     
null
 
},
  error
: function(){
       $
("#tree-container").refresh ()
 
}
});

});




and for the insert i do something similar to the edit stuff: a function that do the insert, a bind of this event that do the ajax, if successfull than i do a Set_id on the node and then i call the rename function.


Is this the best way? (to mix event and function)


Kind Regard.


Il giorno lunedì 31 marzo 2014 08:44:20 UTC+2, Ivan Bozhanov ha scritto:
You are using the function wrong, this is what you need:

var selectedNode= $('#tree').jstree(true).get_selected(true)

Then you will get an array, which you can use this way:

var id = [],
    parent = [],
    text = [];
for(var i = 0, j = selectedNode.length; i < j; i++) {
  id.push(selectedNode.id);
  parent.push(selectedNode.parent);
  text.push(selectedNode.text);
}

If you only want to process a single node - use:
var id = selectedNode[0].id,
     parent = selectedNode[0].parent,
     text = selectedNode[0].text;

Best regards,
Ivan

Ivan Bozhanov

unread,
Mar 31, 2014, 11:19:19 AM3/31/14
to jst...@googlegroups.com
I'd probably bind to events first, and in those events - sync to the server using AJAX calls, if any of those fail - I would call refresh. So basically - act on the jstree instance, sync on events, refresh if sync fails. That is how I would do it. Btw - I will provide such a demo shortly - it is almost ready and will be available in 3.0.0 final in a few days.

Best regards,
Ivan

Ivan Bozhanov

unread,
Mar 31, 2014, 2:44:20 AM3/31/14
to jst...@googlegroups.com
You are using the function wrong, this is what you need:

var selectedNode= $('#tree').jstree(true).get_selected(true)

Then you will get an array, which you can use this way:

var id = [],
    parent = [],
    text = [];
for(var i = 0, j = selectedNode.length; i < j; i++) {
  id.push(selectedNode.id);
  parent.push(selectedNode.parent);
  text.push(selectedNode.text);
}

If you only want to process a single node - use:
var id = selectedNode[0].id,
     parent = selectedNode[0].parent,
     text = selectedNode[0].text;

Best regards,
Ivan


30 март 2014, неделя, 19:47:58 UTC+3, sensor.evil sensor.evil написа:

abhishek arya

unread,
Jul 10, 2015, 7:04:56 AM7/10/15
to jst...@googlegroups.com
Thanks All,
Reply all
Reply to author
Forward
0 new messages