【问题标题】:how to append json variable as child nodes using "jstree" jquery plugin - no ajax如何使用“jstree”jquery 插件将 json 变量附加为子节点 - 没有 ajax
【发布时间】:2011-10-27 10:01:11
【问题描述】:

我有一个使用 json 数据格式的 jsTree。 加载根节点集就可以了。

我的问题是如何将子节点附加到被点击的父节点。

任何帮助将不胜感激。

谢谢!

    $("#tree-cat1")
    .bind("open_node.jstree", function (event, data) {
        console.log(data.rslt.obj.attr("id"));
        //eval(loadChild());
        //at this point i need to append the result of loadChild() to the tree under the relevant node
    })
    .jstree({
        "json_data": {
            "data": eval(loadRoot())
        },
        "themes": {"theme": "classic","dots": true,"icons": true},
        "plugins": ["themes", "json_data", "ui"]
    })

function loadRoot() {
    return "[{'data':'A node','state':'closed','attr':{'id':'A'}}]";
}
function loadChild() {
    return "[{'data':'A1 node','attr':{'id':'A1'}}]";
}

【问题讨论】:

  • 是否要在每次打开节点时添加子节点?为什么你不使用 'children' 属性在树初始化时附加项目
  • 每次节点扩展时,我都会运行 oracle 查询来获取数据(全部在 javascript 中)。返回到 js 变量中的数据。然后我必须在树中的相关点用返回的数据填充树(根据代码示例)

标签: javascript jquery plugins jstree


【解决方案1】:

在此处查看文档:jsTree docu

编辑

这里是代码,你需要把url改成你的目的地,试试看

html:

<div id="tree-cat1"></div>

js:

 $("#tree-cat1").jstree({
    "plugins": ["themes", "json_data", "ui"],
    "themes": {"theme": "classic","dots": true,"icons": true},
    "json_data": {
          //root elements
        "data": [{"data":'A node',"state":'closed',"attr":{"id":'A'}}], 
        "ajax": {
            "type": 'POST',
            "data": {"action": 'getChildren'},
            "url": function (node) { 
                var nodeId = node.attr('id'); //id="A"

                return 'yuorPathTo/GetChildrenScript/' + nodeId;
            },
            "success": function (new_data) {
                //where new_data = node children 
                //e.g.: [{'data':'A1 node','attr':{'id':'A1'}}, {'data':'A2 node','attr':{'id':'A2'}}]
                return new_data;
            }
        }
    }
});

旧零件
如果还没有完成,类似的东西会用孩子填充打开的节点:

 ...
    "json_data": {
          //root elements
        "data": [{"data":'A node',"state":'closed',"attr":{"id":'group_A'}}], 
        "ajax": {
            "type": 'POST',
            "data": {"action": act.GET_GROUPREPORTS},
            "url": function (node) { 
                var nid = node.attr('id'); //id="group_A"
                nid = nid.substr(nid.lastIndexOf('_')+1);

                return module.getDBdata_path + nid;
            },
            "success": function (data) {
                var rid, new_data = data;

                if (typeof data[0] === 'undefined') {
                    new_data = [];
                    for (rid in data) {
                        if(data.hasOwnProperty(rid)) {
                          new_data.push({"data": data[rid], 
                               "attr": {"id": 'rprefix_'+rid, 
                                        "title": ' ', 
                                        "rel": 'report',
                                        "href": module.repView_path+rid
                              }
                          });
                        }
                    }
                }
                return new_data;
            }
        }
    }, ...

【讨论】:

  • 感谢 Irishka,但我仍然无法正常工作。我尝试了使用 ajax 对象的建议,但在某处我遗漏了一些东西。如果您有时间,您可以使用我的示例作为解决方案的基础吗?如果我们得到这个工作,我会欠你很多时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多