我正在使用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();
});
}
答案 0 :(得分:0)
jsTree cookies插件中存在错误。插件初始化代码应该从jquery cookie插件中读取数据。要解决此问题,请查找并更改以下行
tmp = $.cookie(s.save_opened);
到
tmp = unescape($.cookie(s.save_opened));