Jstree 没有为 li 元素分配 id

发布于 2024-12-29 15:41:51 字数 1610 浏览 4 评论 0原文

我有一个 jstree 实例。它是ajax和json。

这是我创建树的方法。

        $("#tree").jstree({
            "plugins" : ["themes", "json_data", "ui"],
            "json_data" : {
                "ajax" : {
                    "type": 'GET',
                    "url": function (node) {
                        var nodeId = "";
                        var url = ""
                        if (node == -1)
                        {
                            //first url called is static
                            url = "myUrl";
                        }
                        else
                        {
                           //click on a node "li", get the id, append it to the url.
                           nodeId = node.attr('id');
                           url = "myUrl" + nodeId;
                        }

                        return url;
                    },
                    "success": function (new_data) {
                       //get the data topNode out of the json object
                       new_data = new_data.topNode;

                       return new_data;
                    }
                }
            },

        }); 

这是输出。

<div id="tree" class="jstree-classic jstree jstree-0 jstree-focused jstree-default">
    <ul>
        <li class="jstree-closed jstree-last">
            <ins class="jstree-icon">&nbsp;</ins>
            <a class="" href="#">Item 1</a> 
        </li>
    </ul>
</div>

然而,当渲染树时,

  • 元素没有 id。应自动附加到
  • 。我缺少什么?
  • I have an instance of jstree. It is ajax with json.

    Here is how I am creating the tree.

            $("#tree").jstree({
                "plugins" : ["themes", "json_data", "ui"],
                "json_data" : {
                    "ajax" : {
                        "type": 'GET',
                        "url": function (node) {
                            var nodeId = "";
                            var url = ""
                            if (node == -1)
                            {
                                //first url called is static
                                url = "myUrl";
                            }
                            else
                            {
                               //click on a node "li", get the id, append it to the url.
                               nodeId = node.attr('id');
                               url = "myUrl" + nodeId;
                            }
    
                            return url;
                        },
                        "success": function (new_data) {
                           //get the data topNode out of the json object
                           new_data = new_data.topNode;
    
                           return new_data;
                        }
                    }
                },
    
            }); 
    

    Here is the output.

    <div id="tree" class="jstree-classic jstree jstree-0 jstree-focused jstree-default">
        <ul>
            <li class="jstree-closed jstree-last">
                <ins class="jstree-icon"> </ins>
                <a class="" href="#">Item 1</a> 
            </li>
        </ul>
    </div>
    

    However when the tree is rendered the <LI> element does not have an id. Should be automatically appended to the <LI>. What am i missing?

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

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

    发布评论

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

    评论(2

    心病无药医 2025-01-05 15:41:51

    如果下面是从 ajax 调用返回的数据,您需要将 "id":"1" 移动到 attributes 部分,

    {"topNode":{"data":"Item 1",
                "children":[],
                "state":"closed",
                "id":"1",
                "attributes":{"class":"editContainerL‌​ink","href":"#"}
               }
    }
    

    这样它看起来像

    {"topNode":{"data":"Item 1",
                "children":[],
                "state":"closed",
                "id":"1",
                "attributes":{"class":"editContainerL‌​ink",
                              "href":"#",
                              "id":"1"
                              }
               }
    }
    

    If below is your data returned from ajax call you need to move "id":"1" to attributes section

    {"topNode":{"data":"Item 1",
                "children":[],
                "state":"closed",
                "id":"1",
                "attributes":{"class":"editContainerL‌​ink","href":"#"}
               }
    }
    

    so it would look like

    {"topNode":{"data":"Item 1",
                "children":[],
                "state":"closed",
                "id":"1",
                "attributes":{"class":"editContainerL‌​ink",
                              "href":"#",
                              "id":"1"
                              }
               }
    }
    
    千寻… 2025-01-05 15:41:51

    只需将属性更改为 attr,id 就会出现在“a”节点中。我还想要“li”节点中的 id,但直到现在我对元数据的尝试都没有成功。

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

    从:
    http://www.jstree.com/documentation/json_data

    just change attributes to attr and an id will appear in the 'a' node. I also want the id in the 'li' node but until now my tries with metadata are not successful.

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

    from:
    http://www.jstree.com/documentation/json_data

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