如何获取jsTree的元数据。

发布于 2024-09-16 18:29:17 字数 7541 浏览 7 评论 0原文

这是我的代码:

$("#demo1").jstree({
        "themes": {
            "theme": "default",
            "dots": true,
            "icons": true,
            "url": "static/themes/default/style.css"
        },
        "ui" : {
                // this makes the node with ID node_4 selected onload
                "initially_select" : [ location.hash.slice(1).split('@')[1]]
            },
        "json_data" : {
            "data" : [
                { 
                    "data" : "A node", 
                    "attr" : { "id" : "1" ,time:1321},
                    "callback":function(){alert('sss')},
                    "children" : [ 
                        {
                            "data" : "ttt node", 
                            "children" : [ "Child 1", "Child 2" ]   
                        }
                     ]
                },
                { 
                    "attr" : { "id" : "2" }, 
                    "data" : { 
                        "title" : "Long format demo", 
                        "attr" : { "href" : "#" } 
                    } 
                },
                {
                    "data" : "sss node", 
                    "attr" : { "id" : "3" },
                    "children" : [ 
                        {
                            "data" : "bbb node"
                        }
                        ,
                        {
                            "data" : "kkkk node", 
                            "attr" : { "id" : "11" },
                            "children" : [ 
                                {
                                    "data" : "oooo node", 
                                    "children" : [ "pppp", "nnnn" ] 
                                }
                             ]
                        },
                    ]
                },
                {
                    "data" : "wwqq node",
                    "attr" : { "id" : "4" },
                    "children" : [ "Child 1", "Child 2" ]   
                },
                {
                    "data" : "hhh node",
                    "attr" : { "id" : "5" },
                    "metadata ":"i am the metadata",
                    "children" : [ 
                            {
                            "data" : "A node", 
                            "children" : [ 
                                {
                                    "data" : "ttt node", 
                                    "children" : [ "Child 1", "Child 2" ]   
                                }
                                ]
                            },
                            {
                            "data" : "bbb node"
                            }

                        ]   
                },
            ]
        },
        /*
        "sort":function (a, b) { 
            return this.get_text(a) < this.get_text(b) ? 1 : -1; 
            },
        ////*/
        "contextmenu":{
                "show_at_node":false,
                "items":{
                        //"ccp":false,
                        "sort" : {
                            // The item label
                            "label"             : "sort",
                            /* The function to execute upon a click
                            "action"            : function (obj) { 
                                                    var fn=function (a, b) {return this.get_text(a) < this.get_text(b) ? 1 : -1;}
                                                    this.changeSort(obj,fn);

                                                    },
                            //*/
                            // All below are optional 
                            "_disabled"         : false,        // clicking the item won't do a thing
                            "_class"            : "sort",   // class is applied to the item LI node
                            "separator_before"  : false,    // Insert a separator before the item
                            "separator_after"   : true,     // Insert a separator after the item
                            // false or string - if does not contain `/` - used as classname
                            "icon"              : false,
                            "submenu"           : { 
                                "name":{
                                        "label" : "name",
                                        "action": function (obj) { 
                                                        var fn=function (a, b) {return this.get_text(a) < this.get_text(b) ? 1 : -1;}
                                                        this.changeSort(obj,fn);
                                                    }
                                    },
                                "time":{
                                        "label" : "time",
                                        "action": function (obj) { 
                                                        var fn=function (a, b) {return this.get_text(a) < this.get_text(b) ? 1 : -1;}
                                                        this.changeSort(obj,fn);

                                                    }
                                    }
                                }
                        },
                        "icons":{
                            "label"             : "icons",
                            "action":function(obj){window.a=obj;},
                            "submenu"           : { 
                                "apple":{
                                        "label" : "apple",
                                        "action": function (obj) { 
                                                        this.set_theme('apple');
                                                    }
                                    },
                                "classic":{
                                        "label" : "classic",
                                        "action": function (obj) { 
                                                        this.set_theme('classic');
                                                    }
                                    },
                                "default":{
                                        "label" : "default",
                                        "action": function (obj) { 
                                                        this.set_theme('default');
                                                    }
                                    }
                                }

                        }
            }
        },
        "core" : { "initially_open" : [ ] },
        "plugins" : [ "themes", "json_data","crrm","ui","contextmenu","search","sort" ]
    })
    .bind("search.jstree", function (e, data) {
            alert("Found " + data.rslt.nodes.length + " nodes matching '" + data.rslt.str + "'.");
        });

我设置了元数据:

"metadata ":"i am the metadata",

并希望在右键单击“上下文菜单”时获取它:

"icons":{
                            "label"             : "icons",
                            "action":function(obj){console.log(this.data);},

我显示 this.data 遵循此 文章

// the `metadata` property will be saved using the jQuery `data` function on the `li` node
    metadata : "a string, array, object, etc",

但是我拿不到,怎么办?

This is my code:

$("#demo1").jstree({
        "themes": {
            "theme": "default",
            "dots": true,
            "icons": true,
            "url": "static/themes/default/style.css"
        },
        "ui" : {
                // this makes the node with ID node_4 selected onload
                "initially_select" : [ location.hash.slice(1).split('@')[1]]
            },
        "json_data" : {
            "data" : [
                { 
                    "data" : "A node", 
                    "attr" : { "id" : "1" ,time:1321},
                    "callback":function(){alert('sss')},
                    "children" : [ 
                        {
                            "data" : "ttt node", 
                            "children" : [ "Child 1", "Child 2" ]   
                        }
                     ]
                },
                { 
                    "attr" : { "id" : "2" }, 
                    "data" : { 
                        "title" : "Long format demo", 
                        "attr" : { "href" : "#" } 
                    } 
                },
                {
                    "data" : "sss node", 
                    "attr" : { "id" : "3" },
                    "children" : [ 
                        {
                            "data" : "bbb node"
                        }
                        ,
                        {
                            "data" : "kkkk node", 
                            "attr" : { "id" : "11" },
                            "children" : [ 
                                {
                                    "data" : "oooo node", 
                                    "children" : [ "pppp", "nnnn" ] 
                                }
                             ]
                        },
                    ]
                },
                {
                    "data" : "wwqq node",
                    "attr" : { "id" : "4" },
                    "children" : [ "Child 1", "Child 2" ]   
                },
                {
                    "data" : "hhh node",
                    "attr" : { "id" : "5" },
                    "metadata ":"i am the metadata",
                    "children" : [ 
                            {
                            "data" : "A node", 
                            "children" : [ 
                                {
                                    "data" : "ttt node", 
                                    "children" : [ "Child 1", "Child 2" ]   
                                }
                                ]
                            },
                            {
                            "data" : "bbb node"
                            }

                        ]   
                },
            ]
        },
        /*
        "sort":function (a, b) { 
            return this.get_text(a) < this.get_text(b) ? 1 : -1; 
            },
        ////*/
        "contextmenu":{
                "show_at_node":false,
                "items":{
                        //"ccp":false,
                        "sort" : {
                            // The item label
                            "label"             : "sort",
                            /* The function to execute upon a click
                            "action"            : function (obj) { 
                                                    var fn=function (a, b) {return this.get_text(a) < this.get_text(b) ? 1 : -1;}
                                                    this.changeSort(obj,fn);

                                                    },
                            //*/
                            // All below are optional 
                            "_disabled"         : false,        // clicking the item won't do a thing
                            "_class"            : "sort",   // class is applied to the item LI node
                            "separator_before"  : false,    // Insert a separator before the item
                            "separator_after"   : true,     // Insert a separator after the item
                            // false or string - if does not contain `/` - used as classname
                            "icon"              : false,
                            "submenu"           : { 
                                "name":{
                                        "label" : "name",
                                        "action": function (obj) { 
                                                        var fn=function (a, b) {return this.get_text(a) < this.get_text(b) ? 1 : -1;}
                                                        this.changeSort(obj,fn);
                                                    }
                                    },
                                "time":{
                                        "label" : "time",
                                        "action": function (obj) { 
                                                        var fn=function (a, b) {return this.get_text(a) < this.get_text(b) ? 1 : -1;}
                                                        this.changeSort(obj,fn);

                                                    }
                                    }
                                }
                        },
                        "icons":{
                            "label"             : "icons",
                            "action":function(obj){window.a=obj;},
                            "submenu"           : { 
                                "apple":{
                                        "label" : "apple",
                                        "action": function (obj) { 
                                                        this.set_theme('apple');
                                                    }
                                    },
                                "classic":{
                                        "label" : "classic",
                                        "action": function (obj) { 
                                                        this.set_theme('classic');
                                                    }
                                    },
                                "default":{
                                        "label" : "default",
                                        "action": function (obj) { 
                                                        this.set_theme('default');
                                                    }
                                    }
                                }

                        }
            }
        },
        "core" : { "initially_open" : [ ] },
        "plugins" : [ "themes", "json_data","crrm","ui","contextmenu","search","sort" ]
    })
    .bind("search.jstree", function (e, data) {
            alert("Found " + data.rslt.nodes.length + " nodes matching '" + data.rslt.str + "'.");
        });

i set the metadata:

"metadata ":"i am the metadata",

and want to get it when i right click,on the "contextmenu" :

"icons":{
                            "label"             : "icons",
                            "action":function(obj){console.log(this.data);},

I show this.data follow this article:

// the `metadata` property will be saved using the jQuery `data` function on the `li` node
    metadata : "a string, array, object, etc",

but I can't get it, what can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

趴在窗边数星星i 2024-09-23 18:29:17

JsTree 使用 jQuery 存储元数据:

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

要访问此元数据,请使用:

.data("jstree")

例如,一旦您以 json 格式传递一些 DateTime 对象:

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

访问它:

$.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");
};

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");
};
海螺姑娘 2024-09-23 18:29:17

接受的答案不适用于最新版本的 jsTree。这是一个基于前一个示例的更新示例。

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

访问数据:

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

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") );
});
在巴黎塔顶看东京樱花 2024-09-23 18:29:17

您可以使用 jstree 中的 get_node 函数获取完整节点,如

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

访问数据

然后您可以从node .original.metadata

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

江湖彼岸 2024-09-23 18:29:17

这些解决方案都不适合我。所做的如下:

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

谢谢

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

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

Thanks

故人如初 2024-09-23 18:29:17

我正在使用 jstree 1.0-rc3,日期为 2011-02-09。首先,我发现将字符串分配给像这样的元数据“元数据”:“我是元数据”是行不通的。它必须是一个 JSON 对象。我的树代表从根文件夹“exercises”开始的目录结构,因此我希望每个节点都将路径存储在存储目录结构的服务器上。服务器发送 JSON 数据(为了清晰起见,进行了简化),如下所示:

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

我使用元数据中的路径值来为打开包含其他文件夹或文件的文件夹时发送的 AJAX 请求构建正确的 URL。用于配置树的 ajax 属性的 url 属性如下所示:

"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;
}

正如文档所承诺的,我们可以在传递给 url 函数的节点上使用 data() 函数,并且潜伏在返回的对象中的是 path 属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文