使用 json 动态加载树
我正在用 JavaScript 设计一棵树,并尝试加载一个节点,该节点存储在服务器上的 json 文件中。
伪代码 JavaScript:
var nodeRoot =
{
level: 4,
id: 12,
data: ...,
childs: Array() // the nodes, all similar to the root, but of level n-1
}
function downloadNodeData(_node){
//initializing the request
req.onreadystatechange=
function(){
//testing the request
var nodeResult = eval("("+req.responseText+")");
console.log(nodeResult);
// ----- What now ? -----
}
req.send(null);
}
我可能必须在多个地方调用此函数(其想法是根据用户操作动态加载树节点)。
关键是,一旦请求返回,我找不到任何方法来更新被调用节点的值。
是制作 SetNodeByLevelAndId(_node,_level,_value) 的唯一方法吗?
感谢您的帮助。
I'm designing a tree in JavaScript, and I'm trying to load a node, stored in a json file on the sever.
Pseudo-code JavaScript:
var nodeRoot =
{
level: 4,
id: 12,
data: ...,
childs: Array() // the nodes, all similar to the root, but of level n-1
}
function downloadNodeData(_node){
//initializing the request
req.onreadystatechange=
function(){
//testing the request
var nodeResult = eval("("+req.responseText+")");
console.log(nodeResult);
// ----- What now ? -----
}
req.send(null);
}
I may have to call this function in several places (the idea is to load dynamically the tree nodes, depending of the user actions).
The point is that I can't find no way to update the value of the node which was called, once the request returns.
Is the only way to make a SetNodeByLevelAndId(_node,_level,_value) ?
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在nodeRoot的描述中没有属性值。
您想在 _node 上更新什么(为您的函数提供的参数)。
如果要将_node替换为nodeResult,则需要将nodeResult的属性一一复制到_node中。因为如果您分配,引用 _node 的其他节点将使用旧引用
In the description of nodeRoot there is no attributes value.
What do you want to update on _node (parameter given to your function).
If it is to replace _node by nodeResult, you have to copy attributes of nodeResult one by one to _node. Because other nodes which reference _node will use an old reference if you assign