Jquery plugins 无法使用Cookie保存JStree状态

Jquery plugins 无法使用Cookie保存JStree状态,jquery-plugins,jstree,jquery-cookie,Jquery Plugins,Jstree,Jquery Cookie,我正在用JStree创建树,并试图用cookie插件保存树的状态,但我无法这样做 每当我刷新页面时,树就会折叠并从头开始。我是否需要编写任何特殊代码来保存状态并从cookie中重新蚀刻 这是我的代码: $(function() { drawJsTree(); }); function drawJsTree(){ var totalNodes = 0; $("#jstree").jstree({ "themes" : { "theme" : "class

我正在用JStree创建树,并试图用cookie插件保存树的状态,但我无法这样做

每当我刷新页面时,树就会折叠并从头开始。我是否需要编写任何特殊代码来保存状态并从cookie中重新蚀刻

这是我的代码:

$(function() {
  drawJsTree();
});
function drawJsTree(){
var totalNodes = 0;
$("#jstree").jstree({
        "themes" : {
            "theme" : "classic",
            "dots" : false,
            "icons" : true
        },
        "cookies" : { 
                    "cookie_options" : {
                                    "path": "C:"

                                    } 
                }, 
        "types" : {
            "types" : {
                "root" : {
                    "icon" : { 
                        "image" : "http://static.jstree.com/v.1.0rc/_docs/_drive.png" 
                    }
                },
                "leaves" : {
                    "icon" : { 
                        "image" : "http://static.jstree.com/v.1.0rc/_docs/_drive.png" 
                    }
                }
            }
        },
        "json_data": {
            "data": (function(obj){
                var s = this.get_settings().json_data
                var newNodes = [];  
                for(var i = 0; i < 10; i++)
                {
                    totalNodes += 1;
                    newNodes.push({"data" : "Node " +(totalNodes-1), "attr": {"id": "node" + (i + totalNodes) , "rel" : "root"}, "state": "closed"});
                }
                if (obj!= -1)
                {
                                    // get parent / grandparent node
                    var lists = $(obj).parents('ul');
                    var p = $(lists[0]).prev('a');
                    var gp = $(lists[1]).prev('a');
                    var t= {
                        "parent": $.trim(p.text()),
                        "grandparent": $.trim(gp.text()),
                        "id": obj.attr ? obj.attr("id"): 1,

                    };
                }
                var d = this._parse_json(newNodes, obj);
                if(!d) {
                if(obj === -1 || !obj) {
                    if(s.correct_state) { this.get_container().children("ul").empty(); }
                }
                else {
                    obj.children("a.jstree-loading").removeClass("jstree-loading");
                    obj.removeData("jstree_is_loading");
                    if(s.correct_state) { this.correct_state(obj); }
                    }
                }
                else {
                    if((obj === -1) || !obj) { 
                        this.get_container().children("ul").empty().append(d.children()); 
                    }
                    else { 
                        obj.append(d).children("a.jstree-loading").removeClass("jstree-loading"); obj.removeData("jstree_is_loading"); 
                    }
                    //this.clean_node(obj);
                }       
                if((obj) && (obj!= -1))
                    obj.removeClass("jstree-closed").addClass("jstree-open").children("a").removeClass("jstree-loading");               

            })
        },
         "plugins": ["themes", "json_data", "ui", "types", "cookies"]
    })
    .bind("select_node.jstree", function (NODE, REF_NODE) {
            /*var newNodes = [];  
                for(var i = 0; i < 10; i++)
                {
                    newNodes.push({"data" : "Node " +i, "attr": {"id": "node" +i, "rel" : "root"}, "state": "closed"});
                }*/
                $.jstree._focused()._get_settings().json_data.data = [];
                $.jstree._focused().refresh(-1); 

                $.jstree._reference("#jstree").destroy();
                self.drawJsTree();
     });
}
$(函数(){
drawJsTree();
});
函数drawJsTree(){
var totalNodes=0;
$(“#jstree”).jstree({
“主题”:{
“主题”:“经典”,
“点”:假,
“图标”:正确
},
“cookies”:{
“cookie_选项”:{
路径“:“C:”
} 
}, 
“类型”:{
“类型”:{
“根”:{
“图标”:{
“图像”:http://static.jstree.com/v.1.0rc/_docs/_drive.png" 
}
},
“叶子”:{
“图标”:{
“图像”:http://static.jstree.com/v.1.0rc/_docs/_drive.png" 
}
}
}
},
“json_数据”:{
“数据”:(功能(obj){
var s=this.get_settings().json_数据
var newNodes=[];
对于(变量i=0;i<10;i++)
{
总节点数+=1;
push({“data”:“Node”+(totalNodes-1),“attr”:{“id”:“Node”+(i+totalNodes),“rel”:“root”},state”:“closed”});
}
如果(obj!=-1)
{
//获取父/祖父母节点
变量列表=$(obj).parents('ul');
var p=$(列出[0]).prev('a');
var gp=$(列表[1]).prev('a');
变量t={
“父项”:$.trim(p.text()),
“祖父母”:$.trim(gp.text()),
“id”:对象属性?对象属性(“id”):1,
};
}
var d=this.\u parse\u json(newNodes,obj);
如果(!d){
如果(obj===-1 | |!obj){
如果(s.correct_state){this.get_container().children(“ul”).empty()}
}
否则{
obj.children(“a.jstree-loading”).removeClass(“jstree-loading”);
对象removeData(“jstree_正在加载”);
如果(s.correct_state){this.correct_state(obj);}
}
}
否则{
如果((obj==-1)| |!obj){
this.get_container().children(“ul”).empty().append(d.children());
}
否则{
obj.append(d).children(“a.jstree-loading”).removeClass(“jstree-loading”);obj.removeData(“jstree-is-loading”);
}
//这个.clean_节点(obj);
}       
如果((obj)&(obj!=-1))
对象.removeClass(“jstree关闭”).addClass(“jstree打开”).children(“a”).removeClass(“jstree加载”);
})
},
“插件”:[“主题”、“json_数据”、“ui”、“类型”、“cookies”]
})
.bind(“select_node.jstree”,函数(node,REF_node){
/*var newNodes=[];
对于(变量i=0;i<10;i++)
{
push({“data”:“Node”+i,“attr”:{“id”:“Node”+i,“rel”:“root”},“state”:“closed”});
}*/
$.jstree._focused()._get_settings().json_data.data=[];
$.jstree.\u focused().refresh(-1);
$.jstree._引用(“#jstree”).destroy();
self.drawJsTree();
});
}

jsTree cookies插件中有一个bug。插件初始化代码应该取消从jquery cookie插件读取的数据。要解决此问题,请查找并更改以下行

tmp = $.cookie(s.save_opened);

tmp = unescape($.cookie(s.save_opened));