Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 966
Michael Philips Avatar asked Dec 19 '25 14:12

Michael Philips


1 Answers

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.

like image 114
vakata Avatar answered Dec 22 '25 04:12

vakata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!