how to get the metadata of jsTree.

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 03:56:53

JsTree stores metadata with jQuery:

.data("jstree", "a string, array, object, etc")

To access this metadata use:

.data("jstree")

For example, once you pass some DateTime object in json format:

metadata : { lastModified : "/Date(1283198400000)/" }

Access it:

$.jstree
.bind("select_node.jstree", function (event, data) {
    alert( formatJsonDateTime( data.rslt.obj.data("jstree").lastModified ) );
});

function formatJsonDateTime(jsonDate) {
    var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    return date.format("M/dd/yyyy HH:mm");
};

The accepted answer does not work with the latest version of jsTree. Here's an updated example building upon the previous.

metadata : { lastModified : "/Date(1283198400000)/" }

Access the data:

$.jstree
.bind("select_node.jstree", function (event, data) {
    alert( data.rslt.obj.data("lastModified") );
});

you can get the full node by using get_node function from jstree like

var node = $(container).jstree().get_node("node_id");

then you can access the data from

node.original.metadata

None of those solutions worked for me. What did is the following:

alert(data.rslt.obj.data()[0].lastModified)

Thanks

I'm working with jstree 1.0-rc3, dated 2011-02-09. First of all, I found that assigning a string to metadata like this "metadata ":"i am the metadata" didn't work. It had to be a JSON object. My tree is representing a directory structure starting from the root folder "exercises" so I want each node to store the path on the server where the directory structure is stored. The server sends out JSON data (simplified for clarity) like this:

[
    {
        "data":"Book1",
        "metadata":{"path":"exercises\/Book1"},
    },
    {
        "data":"vocabulary",
        "metadata":{"path":"exercises\/vocabulary"}
    }
]

I use the path value from the metadata to build the correct URL for the AJAX request sent when you open a folder that contains other folders or files. The url property of the ajax property used to configure the tree looks like this:

"url": function (node) {
    var path = "",
    url = "/tree_service/tree/format/json?path=";
    if(node === -1){
        url += "exercises";
    }
    else{
        path = encodeURIComponent(node.data().path);
        url += path;
    }
    return url;
}

As promised by the documentation, we can use the data() function on the node passed to the url function and lurking in the object returned is the path property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!