网站上有一个关于如何从自定义数据构造子节点的示例:
$("#tree").dynatree({
[…]
onLazyRead: function(node){
$.ajax({
url: […],
success: function(data, textStatus){
// In this sample we assume that the server returns JSON like
// { "status": "...", "result": [ {...}, {...}, ...]}
if(data.status == "ok"){
// Convert the response to a native Dynatree JavaScipt object.
var list = data.result;
res = [];
for(var i=0, l=list.length; i<l; i++){
var e = list[i];
res.push({title: "" + i + ": " + e.fcurr + "-" + e.tcurr + ":" + e.ukurs,
icon: false});
}
// PWS status OK
node.setLazyNodeStatus(DTNodeStatus_Ok);
node.addChild(res);
}else{
// Server returned an error condition: set node status accordingly
node.setLazyNodeStatus(DTNodeStatus_Error, {
tooltip: data.faultDetails,
info: data.faultString
});
}
}
});
[…]
});
但是没有提及如何为树的初始化执行此操作。我尝试了以下方法:
initAjax: {
type: "POST",
url: "/doSomething",
data: ...
contentType: "application/json; charset=utf-8"
success: function(data, textStatus){
// In this sample we assume that the server returns JSON like
// { "status": "...", "result": [ {...}, {...}, ...]}
if(data.status == "ok"){
// Convert the response to a native Dynatree JavaScipt object.
var list = data.result;
res = [];
for(var i=0, l=list.length; i<l; i++){
var e = list[i];
res.push({title: "" + i + ": " + e.fcurr + "-" + e.tcurr + ":" + e.ukurs,
icon: false});
}
// PWS status OK
node.setLazyNodeStatus(DTNodeStatus_Ok);
node.addChild(res);
}else{
// Server returned an error condition: set node status accordingly
node.setLazyNodeStatus(DTNodeStatus_Error, {
tooltip: data.faultDetails,
info: data.faultString
});
}
}
},
但是后来我收到一个错误,说成功不起作用并使用其他方法,但是没有关于如何使用其他方法的文档?有谁可以帮我离开这里吗?我尝试使用 dataFilter 过滤掉我返回的 json 字符串,但这没有用;我尝试使用 onPostInit 和 postProcess 但由于没有文档,我不知道该怎么做:我是否在重新格式化后返回数据字符串,是否返回数据的 json 版本?我只做数据 = 格式(数据)吗?
任何帮助将不胜感激。
我会有很多状态代码,需要根据代码做不同的事情;例如,如果我有代码 1,这意味着我需要将节点的类更改为红色文本;或者如果我返回代码 2,则表示存在内部错误,我需要将其添加到节点文本中;等等