Utpal
Utpal

Reputation: 807

Setting data attribute to a node in jstree

I have implemented JStree in my project. In certain operation I want to set data attribute in the current selected node .I have tried this. but data attribute is not updating.

function SetData(obj)
    {
        debugger;
        var jdata = $('#leftpane').jstree().get_selected(true);
        var key = $('#Key').val();
        var operator = $('#operator').val();
        var value = $('#value').val();

        var newdata = {};
        newdata.key = key;
        newdata.operator = operator;
        newdata.value = value;
        $('#leftpane').jstree().get_selected(true).data = JSON.stringify(newdata);

    }

Thanks

Upvotes: 0

Views: 1919

Answers (1)

Roamer-1888
Roamer-1888

Reputation: 19288

You could try ...

var arrayOfSelectedNodes = $('#leftpane').jstree().get_selected(true);
$(arrayOfSelectedNodes).data(newdata);

// or, avoiding the assignment
$($('#leftpane').jstree().get_selected(true)).data(newdata);

... which will set newdata against however many nodes in the tree were selected (none, one, more than one).

In all pobability, you don't need to JSON.stringify() as jQuery.data() is implemented wholly in javascript, not the DOM, and unstringified objects can be reliably written/read.

You would choose to JSON.stringify() if you needed a "snapshot" of an object that is liable to subsequent change.

Upvotes: 1

Related Questions