i want to change the attribute dynamically in jstree.

4,761 views
Skip to first unread message

Utkarsh Pandey

unread,
Jun 1, 2015, 2:46:53 AM6/1/15
to jst...@googlegroups.com
I am adding child node and in attribite of that node i am saving some data. I used attr() function but every time when i close the node and open it again jstree change my node data attribute to previous state.
Please tell me if you have a function to change attribute.
MY CODE HERE

if(req.action == "updateTopic") {
li = $('.jstree-clicked').parent('li').eq(0);
$('#tree_1').jstree('set_text', li, req.name);
li.attr('data-desc', req.description); // here i am changing data attribute
}
if(req.action == "addNewTopic")
var position = 'before';
var parent = $('#'+$('#tree_1').jstree('get_selected'));
var Node = { "text" : req.name };
nodeId = $('#tree_1').jstree().create_node(parent , Node, position);
attributes = {
'data-desc' : req.description,
'data-mode' : 'edit',
'data-action' : 'getTopic'
};
$('#'+nodeId).attr(attributes); // and same thing here



Ivan Bozhanov

unread,
Jun 1, 2015, 3:01:19 AM6/1/15
to jst...@googlegroups.com, utkar...@gmail.com
To make jstree persist any attributes you need to add them to the internal collection:

$('#tree_1').jstree('set_text', li, req.name);
li.attr('data-desc', req.description); // here i am changing data attribute
$('#tree_1').jstree('get_node', li).li_attr['data-desc'] = req.description;

Best regards,
Ivan

P.S. You can replace this:

$('#'+$('#tree_1').jstree('get_selected'));
with this:
$('#tree_1').jstree('get_selected', true)[0];


Dheeraj Kumar

unread,
Jul 1, 2016, 2:08:37 PM7/1/16
to jsTree
Hi , 

I am trying to update Li attribute as below 
 var JstreeObj = $('#GEOjstree');
    var TargetNode = JstreeObj.jstree(true).get_node(NodeId);
    if (TargetNode != null)
    {
        JstreeObj.jstree('rename_node', TargetNode, SiteDetails.text);
        JstreeObj.jstree('set_id', TargetNode, SiteDetails.id);

        var LiTag = $('#' + SiteDetails.id);
        if (LiTag!=null)
        {
            LiTag.attr('SiteId', SiteDetails.li_attr.SiteId);
            LiTag.attr('UserCountryId', SiteDetails.li_attr.UserCountryId);
            LiTag.attr('WindowsTimeZoneId', SiteDetails.li_attr.WindowsTimeZoneId);
            LiTag.attr('OLsonsTimeZoneId', SiteDetails.li_attr.OLsonsTimeZoneId);
        }
    }
    JstreeObj.jstree('clear_buffer');

but if i re-edit it and read the attribute again , it gives me old values , i check in DOM all attributes have been updated .
Please help and tell me how can i get latest value with object. 

var inst = $.jstree.reference(data.reference);
    obj = inst.get_node(data.reference);

i am using this "obj" to read attributes.

Thank you 
Dheeraj Kumar

Constantin Mitocaru

unread,
Nov 2, 2016, 3:47:43 AM11/2/16
to jsTree
Same is happening here. I modified a node.li_attr.depth attribute, then I called redraw() but the dom obj still has the old value
Please help solving this problem
Reply all
Reply to author
Forward
0 new messages