.on('loaded.jstree', fun) didn't work when manually calling ajax to refresh the tree

2,860 views
Skip to first unread message

elgoog

unread,
Dec 9, 2014, 3:38:32 AM12/9/14
to jst...@googlegroups.com
$('#jstree_demo_div2')
            .on('loaded.jstree', function (e, data) {
                $('#jstree_demo_div2').jstree("open_all");
                console.log("CountNodes");
                CountNodes(); //This method is used to count how many nodes have been checked after the tree data loading
            })
            .jstree({
            'core': {
                'data': {
                    "url": function (node) {
                        return "tree.ashx?a=" + Date.now();
                    },
                    "dataType": "json" 
                }
            },
            "checkbox": {
                'visible': true,
                'keep_selected_style': false,
            },
            "plugins": ["wholerow", "checkbox"]
        });



'loaded.jstree' has been called only at the very first time (which is loading the page) .

Then if I manually call ajax to refrash :  $('#jstree_demo_div2').jstree(true).refresh();
the data has been changed, but method  CountNodes() has not been called. (Actually, none of them in 'loaded.jstree' have been called)

Isn't 'refresh()' a way of 'loading' event?

elgoog

unread,
Dec 9, 2014, 3:57:12 AM12/9/14
to jst...@googlegroups.com
Using redraw event instead of loaded can solve it.

elgoog

unread,
Dec 9, 2014, 3:57:22 AM12/9/14
to jst...@googlegroups.com

elgoog

unread,
Dec 9, 2014, 3:57:50 AM12/9/14
to jst...@googlegroups.com

Ivan Bozhanov

unread,
Dec 9, 2014, 10:24:36 AM12/9/14
to jst...@googlegroups.com
Use
.on('ready.jstree refresh.jstree', function ....)

loaded is only fired on the first load (the same with ready), refresh is triggered after each node (also load_node - but load node is triggered per node)

Best regards,
Ivan

elgoog

unread,
Dec 11, 2014, 1:10:46 AM12/11/14
to jst...@googlegroups.com

data.selected is undefined under refresh event?

Ivan Bozhanov

unread,
Dec 11, 2014, 5:25:05 AM12/11/14
to jst...@googlegroups.com
Yes, of course, since refreshing has nothing to do with selection. All the available properties are listed in the API docs - please read them.
In the mean time, use: data.instance.get_selected() instead of data.selected

Best regards,
Ivan

elgoog

unread,
Dec 12, 2014, 2:46:04 AM12/12/14
to jst...@googlegroups.com


.on('refresh.jstree', function (e, data) {}

data.instance.get_selected() under the refresh event seems to be unchanged even though I called the tree to refresh.
Meanwhile, no matter how many times I call refreshing, $('#tree').jstree(true).get_selected() returns the same array which is the very first selected nodes when the page is initiated.

But when jsTree is called to reload(refresh) by ajax, status of nodes is supposed to change. So I need to know the ids of seleted nodes.

Since you said that refreshing has nothing to do with selection, is there a way to get selected nodes every time I call 
$('#tree').jstree(true).refresh();

I tried loaded, ready, redraw, refresh,changed event according to the APIs. Some of them is not fired, others gets unexpected value because data.selected is not changed.
.on('XX.jstree', function (e, data) {
           
$('#tree').jstree("open_all");
            console.log(data.instance.get_selected());
       
})


Ivan Bozhanov

unread,
Dec 12, 2014, 5:19:32 AM12/12/14
to jst...@googlegroups.com
Once again - please read the docs - calling refresh preserves the state, if you want it to not preserve the state - use the second parameter to refresh:
http://www.jstree.com/api/#/?q=refresh&f=refresh%28%29

So use .refresh(false, true);

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