【问题标题】:In jsTree I get data.rslt as undefined在 jsTree 我得到 data.rslt 为未定义
【发布时间】:2012-03-12 19:45:07
【问题描述】:

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

这是我的 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

【问题讨论】:

    标签: jquery json jstree


    【解决方案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。

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

    【讨论】:

    • 谢谢你,你的权利,我现在可以工作了。我想我应该多读一点。
    • 我现在已经在这个问题上花了几个小时,这个答案并没有真正为我解决任何问题。我从这里关注 ajax 演示:jstree.com/demo/open_close 并且收到关于没有 addBack 函数的错误。另外,传递给 ajax 数据函数的 'n' 对象没有 attr("id") 值,所以我必须使用这个复杂的动物园来获取点击项目的 id:$(n[0]).find( "a").attr("id").
    猜你喜欢
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2021-12-30
    • 2016-09-10
    • 2021-03-24
    • 2015-03-23
    • 2012-02-05
    相关资源
    最近更新 更多