我将Django json数据传递给Jstree,但它不能正常工作

时间:2011-11-09 02:33:24

标签: django jstree django-mptt

我在Django + django-mptt中使用jstree来完成一个类似win-explorer-tree的界面:

{% load mptt_tags %}
var nodedata = {
    "data": {
    {% recursetree nodes %}
    "data": "{{ node.nodename }}",   
    "id": "{{ node.id }}", 
    "state":"open",
    "children": [{ {{ children }} }],
    {% endrecursetree %}
    }
};

当每个节点只有一个孩子时,它工作正常。但是当用户创建多个子节点时,它会在jstree中扩展,如:

var nodedata = {
    "data": {

    "data": "following1",   
    "id": "1", 
    "state":"open",
    "children": [{ 
    "data": "level3",   
    "id": "4", 
    "state":"open",
    "children": [{ 
    "data": "level 4",   
    "id": "5", 
    "state":"open",
    "children": [{ 
    "data": "New node",   
    "id": "6", 
    "state":"open",
    "children": [{ 
    "data": "New node 2",   
    "id": "7", 
    "state":"open",
    "children": [{  }],
     }],

    "data": "kkk",   
    "id": "8", 
    "state":"open",
    "children": [{  }],
     }],
     }],
     }],

    }
};

“kkk”节点显示但“新节点”和“新节点2”未显示。

我正在寻求一些建议,还是更好的方式来表示这棵树?

1 个答案:

答案 0 :(得分:1)

我认为你的json变量有错误。你有:

"children": [{
    "data": "New node",
    "id": "6",
    "state":"open",
    "children": [{
        "data": "New node 2",
        "id": "7",
        "state":"open",
        "children": [{  }],
    }],
    "data": "kkk",
    "id": "8",
    "state":"open",
    "children": [{  }],
}],

“children”数组中的每个节点必须位于单独的{}中,除以逗号。