【问题标题】:How to display django-treebeard MP in template as dropdown menu?如何在模板中将 django-treebeard MP 显示为下拉菜单?
【发布时间】:2020-11-08 03:25:05
【问题描述】:

按照treebeard docs api 示例,我使用get_annotated_list(parent=None, max_depth=None)parent= 创建了我的树的annotated_list。我将它传递给我的模板并使用他们在文档中归因于 Alexey Kinyov 的示例,我能够使用

成功显示我的树
{% for item, info in annotated_list %}
    {% if info.open %}
        <ul><li>
    {% else %}
        </li><li>
    {% endif %}

    {{ item }}

    {% for close in info.close %}
        </li></ul>
    {% endfor %}
{% endfor %}

我想要的是能够提供这些嵌套列表下拉功能。借用 w3schools 上的 this 标准示例,我对其进行了修改以使用我的 annotated_list 模板变量并最终得到以下结果:

<ul id="myUL">
    <li><span class="caret">{{ annotated_list.0.0 }}</span>
        {% for item, info in annotated_list|slice:"1:" %}
            {% if info.open %}
                <ul class="nested"><li>
            {% else %}
                </li><li>{% if item.get_children_count > 0 %}<span class="caret">
                        {% endif %}
            {% endif %}

            {{ item }}
            {% if item.get_children_count > 0 %}</span>
                    {% endif %}

            {% for close in info.close %}
                </li></ul>
            {% endfor %}

        {% endfor %}
    </li>
</ul>

我的代码几乎可以工作,但似乎没有显示最左边节点的子节点,我不知道为什么。

注意:CSS 和 JS 不包括在问题中,但需要使下拉菜单正常工作(我只是使用 w3schools 示例中使用的开箱即用 CSS/JS)

【问题讨论】:

    标签: django materialized-path-pattern django-treebeard


    【解决方案1】:

    最可能的原因是树有问题。 运行Model.find_problems()进行确认。

    Model.fix_tree()可以解决大部分常见问题。

    请注意,get_annotated_list_qs() 函数根本不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多