ingo
ingo

Reputation: 856

Refresh JStree data

I am using a JSTree and for this, I load the data, but in some situations, I need to reload the data with another database filter that will arrive. But how can I add the data to the JSTRee, it is keeping the old data always.

I tried in in the complete as following:

complete: function() { a $('#mytree').jstree(true).refresh();

But no change.

function loadTreeContent(bindTo)
{
    $.ajax({
        type: 'get',
        url: bindTo,
        dataType: 'json',
        success: function(data) {
            $('#mytree').jstree({
                'core' : {
                    'data' : data
                },
                'checkbox' : {
                    "keep_selected_style" : false
                },
                "plugins" : [ 'checkbox', 'contextmenu', 'state'],
                'checkbox': {
                    three_state : true,
                    whole_node : false,
                    tie_selection : false
                },
                'contextmenu': {
                    'items': function ($node) {
                        var tree = $('#mytree').jstree(true);
                        if($node.a_attr.type === 'test')
                            return getTestContextMenu($node, tree);
                    }
                }
            });
        },
        complete: function() {
            // This function will be triggered always, 
            // when ajax request is completed, even it fails/returns other status code
            console.log("complete");
        },
        error: function() {
            // This will be triggered when ajax request fail.
            console.log("Error");
        }
    });
};

Upvotes: 2

Views: 1709

Answers (2)

Robin Ding
Robin Ding

Reputation: 911

Try:

$(".jstree").jstree(true).refresh(true, true);

If you use state (checked and selected) plugin in jstree, then the refresh method become complicated. refresh method has two parameters

refresh(skip_loading, forget_state)

By default:

  1. skip_loading: {Boolean} skip_loading an option to skip showing the loading indicator
  2. forget_state: {Mixed} forget_state if set to true state will not be reapplied, if set to a function (receiving the current state as argument) the result of that. So, by default, it you don't give any value, it will apply existing state before refresh.

So, my case was that I need to clean up my previous state, then I need to call

$(".jstree").jstree(true).refresh(true, true);

See https://github.com/vakata/jstree/issues/2112#issuecomment-421551095 and source code: https://github.com/vakata/jstree/blob/master/src/jstree.js

Upvotes: 1

akshaypjoshi
akshaypjoshi

Reputation: 1295

Try redraw instead of refresh:

complete: function() { a $('#mytree').jstree(true).redraw(true);

Upvotes: 1

Related Questions