加载json_data后更改jsTree节点css类?

iHa*_*ter 7 javascript jquery jstree

每当用户点击文件夹旁边的[+]时,我就会通过json_data加载我的jsTree.我想要做的是将css类应用于某些节点,以便为用户突出显示它们.这里不是关于鼠标悬停或当前选择的节点,而是一些人类必须稍后查看的多个节点.适当的css类已经在服务器的JSON响应中:

[
{"attr":{"id":"node_5","rel":"document","page_id":"4"},"data":"Test123","csscl":"ui-state-error","state":""},
{"attr":{"id":"node_6","rel":"folder","page_id":"6"},"data":"Services","csscl":"","state":"closed"}
]
Run Code Online (Sandbox Code Playgroud)

我的"Test123"节点应该在树后面获得类"ui-state-error".这是我的jsTree:

$(function () {
// Settings up the tree. note to self: dont use the cookie plugin here, this will always overwrite pre-selected nodes
$("#jstree").jstree({
    "plugins" : [ "themes", "json_data", "ui", "types", "hotkeys",],
    "json_data" : { 
        "ajax" : {
            "url" : "inc/tree_server.php",
            "data" : function (n) {
                return { 
                    "operation" : "get_children", 
                    "id" : n.attr ? n.attr("id").replace("node_","") : 1 
                };
            },
            success: function(n) {

                for (var i in n)
                {
                    jqid = "#"+n[i].attr["id"]+" a";
                    $(jqid).addClass(n[i].csscl);
                }                   
            }
        }
    },
    // the UI plugin
    "ui" : {
        // selected onload
        "initially_select" : [ "node_<?=$p->oTopic->iId;?>" ]
    },
    // the core plugin
    "core" : { 
        "initially_open" : [ <?=$p->oTopic->sJstreeOpenSeq;?> ],
        "animation" : 0
    }
})
Run Code Online (Sandbox Code Playgroud)

这不行.我认为发生的是"成功:函数(n)"在加载树之后调用,但在它被绘制或准备好让JQuery找到所选节点并应用我的类之前调用​​.任何人都知道如何解决这个问题,或者也许有在这种情况下,一个CSS类适用于$("#节点5 A")更好的方式...?

ink*_*ink 1

我想我找到了解决方法。

    success: function(n) {
        for (var i in n)
        {
            some_global_array_id.push(n[i].attr["id"]);
            some_global_array_data.push(n[i].csscl);
        }                   
    }
Run Code Online (Sandbox Code Playgroud)

然后在加载和绘制之后,您可以像这样调用函数:

$("#jstree").jstree({
       // ... code you posted
    }).bind("reopen.jstree", function (e, data) {
       for (var i in some_global_array_id) {
           $("#"+some_global_array_id[i]+" a").addClass(some_global_array_data[i]);
       }
    });
Run Code Online (Sandbox Code Playgroud)