在jsTree中,我将data.rslt视为未定义

时间:2012-03-12 19:45:08

标签: jquery json jstree

使用jsTree,我在尝试读取数据对象时得到data.rslt未定义。

这是我的jQuery,创建一个树,加载JSON,它应该在加载时将data.rslt对象输出到控制台。

$(function () {
    $("#demo1").jstree({ 
        "plugins" : [ "themes","json_data","ui", "crrm" ],
        "json_data" : {
            "ajax" : {
                "url" : "categorytreejson.asp"
            }
        },
        "ui" : {
            "initially_select" : [ "root" ]
        }
    });

    $("#demo1").bind("loaded.jstree", function (e, data) {
        console.log(data.rslt)
    });
});

这是JSON数据

{"data": "root", "attr": {"id": "root"}, "children": [{"data": "Photography", "attr": {"id": "Photography"}, "children": [{"data": "Lenses", "attr": {"id": "Lenses"}, "children": [{"data": "Telephoto", "attr": {"id": "Telephoto"}},{"data": "Macro", "attr": {"id": "Macro"}},{"data": "Other", "attr": {"id": "Other"}}]}]}]}

生成的HTML

<li class="jstree-last jstree-open" id="root"><ins class="jstree-icon">&nbsp;</ins><a class="" href="#"><ins class="jstree-icon">&nbsp;</ins>root</a><ul style=""><li class="jstree-closed" id="Photography"><ins class="jstree-icon">&nbsp;</ins><a class="" href="#"><ins class="jstree-icon">&nbsp;</ins>Photography</a><ul><li class="jstree-last jstree-closed" id="Lenses"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Lenses</a><ul><li class="jstree-leaf" id="Telephoto"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Telephoto</a></li><li class="jstree-leaf" id="Macro"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Macro</a></li><li class="jstree-last jstree-leaf" id="Other"><ins class="jstree-icon">&nbsp;</ins><a href="#"><ins class="jstree-icon">&nbsp;</ins>Other</a></li></ul></li></ul></li></li></ul></li></ul></li>

Firebug中的数据对象。

args: []
inst: Object { data={...}, get_settings=function(), _get_settings=function(), more...}
rlbk: false
rslt: undefined

1 个答案:

答案 0 :(得分:1)

某些事件不会填充data.rslt对象 截至jsTree core documentation

数据结构

{ 
   "inst" : /* the actual tree instance */, 
   "args" : /* arguments passed to the function */, 
   "rslt" : /* any data the function passed to the event */, 
   "rlbk" : /* an optional rollback object - it is not always present */
}

特别是,loaded.jstree事件将有一个空的data.rslt,因为没有其他数据传递给该函数。

create.jstreerename.jstree等其他活动会填充data.rslt。

我希望你现在更清楚了: - )