繁体   English   中英

使用JQuery.getJSON()从json的文件加载contecxtmenu

[英]load a contecxtmenu from json's file using a JQuery.getJSON()

我需要从json的文件加载jsTree的contextmenu。 contextmenu保存在此文件(“test.json”)中:

{
    "item1" : {
        "label" : "voce1"
    },
    "item2" : {
        "label" : "voce2"
    }
}

并且加载contextmenu的代码是:

$(function () {

    $("#tree").jstree({ 
        "plugins" : [ "themes", "json_data", "ui", "contextmenu" ],

        // other code ....

        "contextmenu" : {
        "items" : customMenu
    }

    })
});

function customMenu(node) {

    $.getJSON( "test.json", function(json) {
        return json;
    });
}

这样,我看不到上下文菜单。 你能帮助我吗?

我不知道jstree插件是如何工作的,但也许你应该尝试不同的方法,首先加载发出Ajax请求的JSON数据,当它完成时,初始化jstree:

$(function () {
 $.getJSON( "test.json", function(json) {
  $("#tree").jstree({ 
    "plugins" : [ "themes", "json_data", "ui", "contextmenu" ],
    "contextmenu" : {
      "items" : json
    }
  });
 });
});

这是因为Ajax调用是异步的,因此您的customMenu()函数不会向"contextmenu" "items"选项返回任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM