1

I use jstree and want to reload it with new data but I cannot send it to new data.

var $driver = 0;

$(document).ready(function () {
    $('#tree_folder').jstree({
        'core': {
            'check_callback': true,
            "themes": {
                "responsive": false
            },
            'data': {
                type: "POST",
                url: "Doc.aspx/Folder",
                data: function () { return '{"driver":"' + $driver + '"}' },
                contentType: "application/json"
            }
        }
    });
});        


$(document).on('click', '.tile', function () {
    var $item = $(this);    
    var $driver = $item.attr('data-driver');    
    //  alert($driver);    
    $('#tree_folder').jstree('refresh');
});

I got the new value when clicked, it send old default data every time. Above in alert function, it can give me the right value although json post send default one even data is written with function

function () { return '{"driver":"' + $driver + '"}' }

How can I get the new value from variable?

Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
  • I think the issue might be json post is a async call, and you need to get the value in callback. – Lawrence Liu Mar 25 '14 at 07:20
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Andreas Mar 25 '14 at 07:21

1 Answers1

0

Remove var before $driver from .tile click function as you are created global previously,

Try this,

$(document).on('click', '.tile', function () {
    var $item = $(this);    
    $driver = $item.attr('data-driver');   // remove var from this line
    //  alert($driver);    
    $('#tree_folder').jstree('refresh');
});

Also use cache:false in jstree like,

function getDriver() {
    return '{"driver":"' + $driver+ '"}';
}

$(document).ready(function () {
    $('#tree_folder').jstree({
        'core': {
            'check_callback': true,
            "themes": {
                "responsive": false
            },
            cache: false, // cache false
            'data': {
                type: "POST",
                url: "Doc.aspx/Folder",
                data: getDriver(), // get updated driver
                contentType: "application/json"
            }
        }
    });
});

Also take a look on change ajax options in jstree from server

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106