loader: function() {
$.ajax({
type: "get",
url: url,
dataType: 'jsonp',
data: data,
success: function(data) {
if(data.code == 0) {
var oriArr = data.content; //原始数据
var newArr = new Array(); //保存解析之后的数据
for(i = 0; i < oriArr.length; i++) {
notExist = true;
for(j in newArr) {
if(newArr[j].id == oriArr[i].channel) { //如果新数组中已经存在了当前的设备id,则将当前设备当做child保存
notExist = false; //将flag赋值为false,防止再次生成一级目录
var child = {};
child.id = newArr[j].id + j;
child.text = oriArr[i].kssj + "-" + oriArr[i].jssj;
child.attributes = {
bdt: oriArr[i].kssj,
edt: oriArr[i].jssj,
channel: oriArr[i].channel
};
newArr[j].children.push(child);
break;
}
}
if(notExist) { //生成一级目录
if(oriArr[i].channel == "undefind" || oriArr[i].channel == null) {
break;
} else {
var tree = {};
tree.id = oriArr[i].channel;
tree.text = oriArr[i].sbmc;
tree.state = "closed";
tree.children = [];
var child = {};
child.id = tree.id + i;
child.text = oriArr[i].kssj + "-" + oriArr[i].jssj;
child.attributes = {
bdt: oriArr[i].kssj,
edt: oriArr[i].jssj,
channel: oriArr[i].channel
}
tree.children.push(child);
newArr.push(tree);
}
}
};
$("#tree" ).tree("loadData", newArr);
}
},
error: function() {
error.apply(this, arguments);
}
});
}
在解决了跨域的问题之后,又遇到了一个问题,那就是请求回来的数据怎么渲染到树结构中,最后通过查看API找到loadData方法
最后欢迎大家进行讨论交流