如何使用 jsTree 构造 HTML 数据以显示为树

发布于 2024-10-11 18:51:36 字数 1412 浏览 8 评论 0原文

我是 jsTree 的新手,我想从 HTML 页面内的

  • 列表开始创建一棵树。这使用 html_data 插件(我希望这是正确的方式)。

我想知道:用 HTML 编写将由 jsTree 转换为树的数据的正确方法是什么?

jsTree 文档 建议这样做:

<li>
    <a href="some_value_here">Node title</a>
    <!-- UL node only needed for children - omit if there are no children -->
    <ul>
        <!-- Children LI nodes here -->
    </ul>
</li>

但它没有指定将 id< 放在哪里/code> jsTree 用于引用数据的属性。

而且它似乎效果不太好。我见过有人将该代码嵌入

    标签。例如:

<div id="categories">
    <ul>
        <li><a href="#">Category 1</a>
            <ul>
                <li><a href="#">SubCategory 1</a></li>
                <li><a href="#">SubCategory 2</a></li>
            </ul>
        </li>
        <li><a href="#">Category 2</a></li>
    </ul>
</div>

注意div标签中的id。 这是正确的方法吗? 对我来说,仅使用 标签来编写节点的文本似乎不太舒服......如果我使用 < /code> 我失去了项目图标...

希望有人比我有更清晰的头脑。

I'm new with jsTree and I would like to create a tree starting from a <ul> and <li> list inside my HTML page. This using html_data plugin (I hope it's the right way).

I'm wondering: which is the correct way to write in HTML the data that will be turned into a tree by jsTree?

jsTree documentation suggests this one:

<li>
    <a href="some_value_here">Node title</a>
    <!-- UL node only needed for children - omit if there are no children -->
    <ul>
        <!-- Children LI nodes here -->
    </ul>
</li>

But it doesn't specify where to put the id attribute that jsTree uses to refer to the data.

Moreover it doesn't seem to work so well. I've seen someone who embeds that code with a <div> and a <ul> tags. For instance:

<div id="categories">
    <ul>
        <li><a href="#">Category 1</a>
            <ul>
                <li><a href="#">SubCategory 1</a></li>
                <li><a href="#">SubCategory 2</a></li>
            </ul>
        </li>
        <li><a href="#">Category 2</a></li>
    </ul>
</div>

Note that the id in the div tag.
Is this a correct way?
For me it seems not so comfortable using <a href="#"> tags only to write the text of a node... And if I use a <span> I loose the item icon...

Hope someone has a clearer head than mine.

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

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2024-10-18 18:51:36

这似乎是 jsTree 用来绘制树的模式:

<div id="mytree">
    <ul>
        <li>
            <a href="#">Node 1</a>
            <ul>
                <li>
                    <a href="#">Node 1.1</a>
                </li>
                <li>
                    <a href="#">Node 1.2</a>
                    <ul>
                        <li>
                            <a href="#">Node 1.2.1</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>
            <a href="#">Node 2</a>
        </li>
    </ul>
</div>

即每个节点都具有 jsTree 文档推荐的结构:

<li>
    <a href="some_value_here">Node title</a>
    <!-- UL node only needed for children - omit if there are no children -->
    <ul>
        <!-- Children LI nodes here (recursively apply the same pattern)-->
    </ul>
</li>

并且所有结构都必须用以下内容包装(文档没有说):

<div id="mytree">
    <ul>
        <!-- all the tree here -->
    </ul>
</div>

This seems to be the pattern used by jsTree to draw the tree:

<div id="mytree">
    <ul>
        <li>
            <a href="#">Node 1</a>
            <ul>
                <li>
                    <a href="#">Node 1.1</a>
                </li>
                <li>
                    <a href="#">Node 1.2</a>
                    <ul>
                        <li>
                            <a href="#">Node 1.2.1</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>
            <a href="#">Node 2</a>
        </li>
    </ul>
</div>

That is each node has the structure recommended by jsTree documentation:

<li>
    <a href="some_value_here">Node title</a>
    <!-- UL node only needed for children - omit if there are no children -->
    <ul>
        <!-- Children LI nodes here (recursively apply the same pattern)-->
    </ul>
</li>

And all the structure must be wrapped with (what documentation doesn't say):

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