如何使用jsTree jQuery插件预选节点

Ed *_*bor 3 jquery jquery-plugins jstree

我正在使用带有"Checkbox"插件的jsTree jQuery插件,并使用异步http请求来延迟加载树的每个级别.一切都很好,除了我不能让树在第一级之后预先选择某些节点.我使用"selected"属性来提供要预选的ID数组.树的顶层ID正确预先选择.但是,当级别加载时,不会选择树的较低级别的ID.我错过了什么吗?

这是构造函数代码:

    var myArrayOfIDs = new Array();
    myArrayOfIDs[0] = '123';  //etc...

    $(sDivID).tree(
        {
            data : {
                async : true,
                opts : {url : sURL}
            },
            plugins:{ 
                "checkbox" : {three_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() } }
            }
        }
    )
Run Code Online (Sandbox Code Playgroud)

小智 6

$(".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');
    });
});
Run Code Online (Sandbox Code Playgroud)

我正在使用它来获取最新版本的jstree.