I am using the jsTree jQuery plug-in with its "Checkbox" plug-in and using an async http request to lazy-load each level of the tree. All works great, except that I cannot get the tree to pre-select certain nodes after the first level. I am using the "selected" attribute to provide an array of ID's to preselect. ID's in the top level of the tree are correctly pre-selected. However, ID's in lower levels of the tree are not selected when the level loads. Am I missing something?
Here is the constructor code:
var myArrayOfIDs = new Array();
myArrayOfIDs[0] = '123'; //etc...
$(sDivID).tree(
{
data : {
async : true,
opts : {url : sURL}
},
plugins:{
"checkbox" : {th开发者_如何学JAVAree_state : false}
},
selected : myArrayOfIDs,
ui:{
theme_name : "checkbox",
dots : false,
animation : 400
},
callback : {
beforedata : function(NODE, TREE_OBJ) { return { id : $(NODE).attr("id") || 0, rand : Math.random().toString() } }
}
}
)
$(".jstree").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "ui" ],
"checkbox": {
"real_checkboxes": true,
"real_checkboxes_names": function (n) {
return [n[0].id, 1];
},
"two_state": true
}
}).bind("loaded.jstree", function (event, data) {
$('li[selected=true]').each(function () {
$(this).removeClass('jstree-unchecked').addClass('jstree-checked');
});
});
I'm using this for the latest version of jstree.
Hmm, your code looks fine to me.
I took a slightly different approach in my solution because I wanted the server i.e. your sURL to tell me which items should be selected - then in jsTree's onload callback I select them.
Update: I've updated my jsTree blog post to the latest version of jsTree here
-Matt
This is how I update selection after tree was loaded:
$('#jstree_div')
.on('changed.jstree', treeChanged)
.on('loaded.jstree', treeLoaded)
.jstree({
'plugins': ["checkbox"],
'core': {
'data': {
'url': '/xmldata/getcategories'
}
}
});
function treeLoaded(event, data) {
data.instance.select_node(['2', '5']); //node ids that you want to check
}
精彩评论