简体   繁体   English

展开/折叠对象列表

[英]Expand/Collapsible list of objects

I'm trying to display a list of object Models (Robots), the models have a field Parent which can be another Robot. 我正在尝试显示对象模型(机器人)的列表,这些模型有一个字段Parent,可以是另一个Robot。

I've implemented a nested list using MPTT for Django: 我已经为Django使用MPTT实现了一个嵌套列表:

{% load mptt_tags %}
<ul>
    {% recursetree nodes %}
        <li>
            <a href="{{ node.get_absolute_url }}">{{ node.name }}</a>
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}

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

I'd now like to make the list exandable/collapsable - eg I can shrink all nodes children. 我现在想使列表可扩展/可折叠-例如,我可以缩小所有节点的子级。 I'm having trouble using Javascript for this because the nodes are all of the same class. 我在使用Javascript时遇到了麻烦,因为节点都是同一类。 Is there any other simple way of implementing this? 还有其他简单的方法可以实现吗?

you can figure out what level you're at in the tree using node.level , so all you need is add an additional CSS class to just the top level, something like this: 您可以使用node.levelnode.level您在树中的node.level ,因此您所需要的只是在顶层添加一个额外的CSS类,如下所示:

<ul id="node-{{ node.pk }}" class="children{% if node.level==0 %} top_level{% endif %}">

node.tree_id could also be interesting instead of just pk node.tree_id也可能很有趣,而不仅仅是pk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM