确定“最后” django-mptt 中每个级别的元素

发布于 2024-12-25 06:38:33 字数 1736 浏览 1 评论 0原文

我试图生成一个类似于以下内容的列表:

<ul>
    <li>Parent 1
        <ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li class="last">Child 3</li>
        </ul>
    </li>
    <li>Parent 2
        <ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li class="last">Child 3</li>
        </ul>
    </li>
    <li class="last">Parent 3
        <ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li class="last">Child 3</li>
        </ul>
    </li>
</ul>

我最初只是这样做:

<ul>
    {% recursetree mytree %}
    <li class="{% if not node.get_next_sibling %}last{% endif %}">
        {{ node }}
        {% if not node.is_leaf_node %}
        <ul>
            {{ children }}
        </ul>
        {% endif %}
    </li>
</ul>

但是,对 node.get_next_sibling 的调用会导致对每个项目进行额外的查询。显然,这并不理想。因此,我尝试使用 tree_infostruction.close_levels 来确定最后一个项目:

{% for node,structure in mytree|tree_info %}
    {% if structure.new_level %}<ul><li>{% else %}</li><li>{% endif %}
        <li class="{% if structure.closed_levels|length > 0 %}last{% endif %}">
            {{ node }}
    {% for level in structure.closed_levels %}</li></ul>{% endfor %}
{% endfor %}

这非常有效,除了最后一个根级别项目没有获得“最后一个”类,因为它的struct.close_levels 始终是一个空列表。 (它实际上只适用于儿童物品)。

我确信我不是第一个需要完成类似事情的人,所以我希望这里有人可能已经有了解决方案。

I'm trying to generate a list akin to:

<ul>
    <li>Parent 1
        <ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li class="last">Child 3</li>
        </ul>
    </li>
    <li>Parent 2
        <ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li class="last">Child 3</li>
        </ul>
    </li>
    <li class="last">Parent 3
        <ul>
            <li>Child 1</li>
            <li>Child 2</li>
            <li class="last">Child 3</li>
        </ul>
    </li>
</ul>

I originally just did:

<ul>
    {% recursetree mytree %}
    <li class="{% if not node.get_next_sibling %}last{% endif %}">
        {{ node }}
        {% if not node.is_leaf_node %}
        <ul>
            {{ children }}
        </ul>
        {% endif %}
    </li>
</ul>

However, the call to node.get_next_sibling results in an extra query for each item. Obviously, that's not ideal. So I tried using tree_info and structure.closed_levels to determine the last item:

{% for node,structure in mytree|tree_info %}
    {% if structure.new_level %}<ul><li>{% else %}</li><li>{% endif %}
        <li class="{% if structure.closed_levels|length > 0 %}last{% endif %}">
            {{ node }}
    {% for level in structure.closed_levels %}</li></ul>{% endfor %}
{% endfor %}

This works great, except the last root level item does not get the "last" class, because its structure.closed_levels is always an empty list. (It really only works for child items).

I'm sure I'm not the first to need to accomplish something similar, so I'm hoping someone here might have a solution already.

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

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

发布评论

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

评论(2

毁虫ゝ 2025-01-01 06:38:33

我想您应该能够从 MPTT 订单信息中获取您需要的信息。这是关于 MPTT 如何工作的良好介绍(从 django-mptt 文档链接)。关键是保留对父节点的引用,因此您可以检查节点的“右”属性是否比父节点的“左”属性小1。

django-mptt 对根节点进行特殊处理,让您拥有多个树。如果您要迭代单个树中的节点,类似这样的事情应该可以工作(尽管我没有测试过):

<ul class="root">
    {% recursetree nodes %}
        <li class="{% if parent == None or node.rgt|plusone == parent.lft %}last{% endif %}">
            {{ node.name }}
            {% if not node.is_leaf_node %}
                {% with node as parent %}
                <ul class="children">
                    {{ children }}
                </ul>
                {% endwith %}
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

但是,如果“节点”包含所有根的列表,则您需要明确地捕获它。像这样的事情应该可以解决问题:

{% with nodes|last as lastnode %}
<ul class="root">
    {% recursetree nodes %}
        <li class="{% if node == lastnode or parent and node.rgt|plusone == parent.lft %}last{% endif %}">
            {{ node.name }}
            {% if not node.is_leaf_node %}
                {% with node as parent %}
                <ul class="children">
                    {{ children }}
                </ul>
                {% endwith %}
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>
{% endwith %}

您会注意到上面的代码引用了“plusone”模板过滤器。像这样的事情应该做:

from django import template

register = template.Library()

@register.filter
def plusone(value):
    return value + 1

话虽这么说,这对我来说有点太多的模板计算。如果这是您经常做的事情,那么将其包装在自定义模板标签中可能是明智的做法。

I imagine you should be able to get the info you need from the MPTT order info. Here's a good intro to how MPTT works (linked from the django-mptt docs). The key is keeping a reference to the parent around, so you can check whether your node's "right" attribute is one less than your parent's "left".

django-mptt special-cases the root nodes, to let you have multiple trees. If you're iterating over the nodes in a single tree, something like this should work (though I haven't tested):

<ul class="root">
    {% recursetree nodes %}
        <li class="{% if parent == None or node.rgt|plusone == parent.lft %}last{% endif %}">
            {{ node.name }}
            {% if not node.is_leaf_node %}
                {% with node as parent %}
                <ul class="children">
                    {{ children }}
                </ul>
                {% endwith %}
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

If, however, "nodes" holds a list of all your roots, you'll need to catch that explicitly. Something like this should do the trick:

{% with nodes|last as lastnode %}
<ul class="root">
    {% recursetree nodes %}
        <li class="{% if node == lastnode or parent and node.rgt|plusone == parent.lft %}last{% endif %}">
            {{ node.name }}
            {% if not node.is_leaf_node %}
                {% with node as parent %}
                <ul class="children">
                    {{ children }}
                </ul>
                {% endwith %}
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>
{% endwith %}

You'll notice that the code above references a "plusone" template filter. Something like this should do:

from django import template

register = template.Library()

@register.filter
def plusone(value):
    return value + 1

All that being said, this is a little too much template computation for my liking. If this is something you're doing regularly, it's probably wise to wrap it up in a custom template tag.

泼猴你往哪里跑 2025-01-01 06:38:33

您应该使用{{forloop.last}}
{{if 1 == forloop.revcounter}}
第二个最后是 {{if 2 == forloop.revcounter}}

添加带有 django 逻辑的类...

如果你只想添加样式,你可以使用 css selecotrs :last-child 则不要添加类css 选择器来为 jquery 工作

如果您需要 javascript 并且正在使用 jquery,

You should use the{{forloop.last}}
or {{if 1 == forloop.revcounter}}
2nd last would be {{if 2 == forloop.revcounter}}

to add classes with django logic...

if you just want to add styles you can use css selecotrs :last-child instead of adding a class

css selectors to work for jquery if you need it for javascript and are using jquery

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