Uzair Khan
Uzair Khan

Reputation: 2970

How to make jstree empty on initialization?

On click of a button I send some key to controllerand get my lists using json , the array inside my lists acts as my children in my jstree,

 $("#btnSearch").on("click", function () {
        alert("I'm also here");
        $.ajax({
            type: "POST",
            url: "/Claims/JSTree",
            success: function (response) {

                var clients = [];
                var claimKeys = [];
                $.each(response.ClientNames, function (i, item) {
                    clients.push(item);
                });
                alert(clients);
                $.each(response.ClaimNumbers, function (i, item) {
                    claimKeys.push(item);
                });
                alert(claimKeys);
                $('#FolderTree').jstree({
                    'core': {
                        'data': [
                           {
                               'text': 'Client',
                               'state': {
                                   'opened': false,
                                   'selected': false
                               },
                               'children': clients
                           },
                            {
                                'text': 'Claim key',
                                'state': {
                                    'opened': false,
                                    'selected': false
                                },
                                'children': claimKeys
                            },                               
                         ]
                    },
                    "plugins": ["checkbox"]
                });
            }
        });
    });
});

Everything works fine for the first time, when second time I pass a different key I get the lists, it even shows the values in alert, but my jstree still retains the previous values..Unless I stop and restart debugging, jstree doesn't become empty..Is there a way to empty jstree before populating it with children??

Upvotes: 3

Views: 1896

Answers (1)

vakata
vakata

Reputation: 3886

Try using this code, which first destroys the instance:

...
var tmp = $('#FolderTree').jstree(true);
if(tmp) { tmp.destroy(); }
$('#FolderTree').jstree({ ...

Also make sure you are using the latest jsTree version.

Upvotes: 3

Related Questions