简体   繁体   中英

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

I need to load a jsTree's contextmenu from a json's file. The contextmenu is saved in this this file ("test.json"):

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

and the code to load the contextmenu is:

$(function () {

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

        // other code ....

        "contextmenu" : {
        "items" : customMenu
    }

    })
});

function customMenu(node) {

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

In this way, I don't see the contextmenu. Can you help me?

I don't know how jstree plugin works, but maybe you should try a different approach, loading first the JSON data making the Ajax request, and when it's finished, initialize the jstree:

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

This is because Ajax calls are asynchronous, so your customMenu() function is not returning anything to your "items" option of "contextmenu" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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