How can I refresh jstree with new json data?

2,903 views
Skip to first unread message

gmapst...@gmail.com

unread,
Mar 20, 2014, 5:14:26 AM3/20/14
to jst...@googlegroups.com
Hi,
I fill jstree via json and want to change it with click event altough I change the variable for it, it calls data with old value. When I click the button, I see same tree.


var $user;

$('#tree_menu').jstree({
    'plugins': ["wholerow", "checkbox", "types"],
    "checkbox": {
        "keep_selected_style": false,
        "three_state": false
    },
    'core': {
        // 'check_callback': true,
        "themes": {
            "responsive": false
        },
        'data': {
            type: "POST",
            url: "Menu_Permission.aspx/Menu",
            data: '{user: "' + $user + '"}',
            contentType: "application/json"
        }
    }
});


$(document).on('click', '.btn-role', function () {
    var $item = $(this);
    $user = $item.attr('data-u');

    $('#tree_menu').jstree('refresh');
});


$(function () {
    $('#tree_menu').on('deselect_node.jstree Event', function (e, data) {
        var item_id = data.node.id;
        // if (item_id)
        console.log(data.node.id);
    });
});

$(function () {
    $('#tree_menu').on('select_node.jstree Event', function (e, data) {
        var item_id = data.node.id;
        //   if (item_id)
        console.log(data.node.id);
    });
});



When I click the button $user is change and jstree reload but with default value. I seems like it does not see new value with  jstree('refresh'or I do not know. How can I reload it with new data via every click?


Ivan Bozhanov

unread,
Mar 20, 2014, 5:52:49 AM3/20/14
to jst...@googlegroups.com
Use "data: function () { return '{user: "' + $user + '"}'; },"

gmapst...@gmail.com

unread,
Mar 20, 2014, 7:21:12 AM3/20/14
to jst...@googlegroups.com
It worked, I got the new datas but jstree loaded with old data ):

gmapst...@gmail.com

unread,
Mar 20, 2014, 8:41:24 AM3/20/14
to jst...@googlegroups.com
var $user = 0;

$
(document).ready(function () {

    $
('#tree_menu').jstree({
       
'plugins': ["wholerow", "checkbox", "types"],
       
"checkbox": {
           
"keep_selected_style": false,
           
"three_state": false
       
},
       
'core': {

           
'check_callback': true,
           
"themes": {
               
"responsive": false
           
},
           
'data': {
                type
: "POST",
                url
: "Menu_Permission.aspx/Menu",

                data
: function () { return '{"user":"' + $user + '"}' },

                contentType
: "application/json"
           
}
       
}
   
});
});


$
(document).on('click', '.btn-role', function () {
   
var $item = $(this);
    $user
= $item.attr('data-u');
   $
('#tree_menu').jstree('refresh');
});

These are my last code changing but it does not work, just reload itself without new data, altough new data is loaded.

gmapst...@gmail.com

unread,
Mar 20, 2014, 9:41:54 AM3/20/14
to jst...@googlegroups.com
Edit-problem updated 

When I changed the data from db, I noticed that jstree was refreshed well except checkbox checked. I've already had same data with deifferent checkbox value for all users. I realise that now.
How can I fix it?

Ivan Bozhanov

unread,
Mar 20, 2014, 11:08:34 AM3/20/14
to jst...@googlegroups.com
Sorry but I do not understand the problem - I have no idea how you manage the checked state. You might want to "deselect_all()" before each refresh, if you pass the selected state from the server.

best regards,
Ivan
Reply all
Reply to author
Forward
0 new messages