展开/折叠对象列表 [英] Expand/Collapsible list of objects

查看:162
本文介绍了展开/折叠对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示一个对象模型列表(机器人),这些模型有一个可以是另一个Robot的Parent。

I'm trying to display a list of object Models (Robots), the models have a field Parent which can be another Robot.

我已经实现了使用MPTT for Django的嵌套列表:

I've implemented a nested list using MPTT for Django:

{% 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>

我现在想让列表不可扩展/可折叠 - 例如我可以缩小所有节点的子节点。我在使用Javascript时遇到麻烦,因为这些节点都是同一个类。有没有其他简单的方法来实现?

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. Is there any other simple way of implementing this?

推荐答案

你可以使用 node.level ,所以你需要的是添加一个额外的CSS类,只是顶级,这样的东西:

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:

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

node.tree_id 也可能是有趣的只是pk

node.tree_id could also be interesting instead of just pk

这篇关于展开/折叠对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆