确定“最后” django-mptt 中每个级别的元素
我试图生成一个类似于以下内容的列表:
<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_info
和 struction.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您应该能够从 MPTT 订单信息中获取您需要的信息。这是关于 MPTT 如何工作的良好介绍(从 django-mptt 文档链接)。关键是保留对父节点的引用,因此您可以检查节点的“右”属性是否比父节点的“左”属性小1。
django-mptt 对根节点进行特殊处理,让您拥有多个树。如果您要迭代单个树中的节点,类似这样的事情应该可以工作(尽管我没有测试过):
但是,如果“节点”包含所有根的列表,则您需要明确地捕获它。像这样的事情应该可以解决问题:
您会注意到上面的代码引用了“plusone”模板过滤器。像这样的事情应该做:
话虽这么说,这对我来说有点太多的模板计算。如果这是您经常做的事情,那么将其包装在自定义模板标签中可能是明智的做法。
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):
If, however, "nodes" holds a list of all your roots, you'll need to catch that explicitly. Something like this should do the trick:
You'll notice that the code above references a "plusone" template filter. Something like this should do:
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.
您应该使用
{{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 classcss selectors to work for jquery if you need it for javascript and are using jquery