最小化 django-admin 中的列表过滤器
我非常喜欢 django 管理视图的过滤功能(list_filter
)。
但是,在具有很多字段的视图上,我真的很希望能够通过单击来最小化/展开它,以节省屏幕空间,而且因为它有时实际上会隐藏一些东西。
有没有一种简单的方法来添加折叠按钮(我还没有找到一些已经存在的插件或类似的东西)?
I quite like the filter feature of django admin views (list_filter
).
But, on views with a lot of fields, I would really like the ability to minimize/expand it with a click, to save screen real-estate and also because it sometimes actually hides stuff.
Is there an easy way to add a collapse button (some already existing plugin I haven't found or something similar)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
对此进行了另一项更改,以便当您单击顶部的 H2 时隐藏 H3 以及过滤器列表。如果您单击顶部的“过滤器”,这将使整个过滤器列表消失。
这是js文件内容
Made another change to this so that the H3's are hidden, as well as the filter lists, when you click on the top H2. This will get the entire list of filters out of the way if you click on the top "Filters".
This is the js file content
修改 fanlix 解决方案为:
代码
Modified fanlix solution to:
Code
结合了 Tim 和 maGo 的方法,并进行了一些调整:
优点:
缺点:
代码:
Combined Tim's and maGo's approaches, with some tweaks:
Pros:
Cons:
The code:
我编写了菜单折叠和单元素菜单折叠的片段。
它是 abyx 代码的一个分支,我刚刚对其进行了扩展。
如果先前激活了过滤器,则与此相关的元素菜单将按打开状态启动。
过滤器菜单默认关闭。
希望这有帮助
https://github.com/peppelinux /Django-snippets/tree/master/django-admin.js-snippets
I wrote a snippets for menu collapse and single element menu collapse.
It's a fork from abyx code, I've just extended it.
If a filter was previously activated the element menu related to this will start as opened.
The filter menu starts closed as default.
Hope this helps
https://github.com/peppelinux/Django-snippets/tree/master/django-admin.js-snippets
Giuseppe De Marco 的片段效果最好。所以我在这里添加他的代码片段以便于访问。它甚至解决了 joelg 上面讨论的问题(缺点):
Giuseppe De Marco's snippet works best. So i am adding his code snippet here for easy access. It even solves the problem (Cons) discussed above by joelg:
Django 4.x,这是我的做法。
创建管理模板如下
{% extends "admin/change_list.html" %} {% 阻止额外样式 %}
{{ 块.super }}
函数toggle_filter() {
$("#changelist-filter").toggle("慢");
};
$(文档).ready(函数(){
// 默认关闭它们
$("#changelist-filter").toggle("快速");
}); {% 末端嵌段 %}
增强 admin/base_site.html 添加按钮
Django 4.x, here is how I do.
create admin template as below
{% extends "admin/change_list.html" %} {% block extrastyle %}
{{ block.super }}
function toggle_filter() {
$("#changelist-filter").toggle("slow");
};
$(document).ready(function(){
// close them by default
$("#changelist-filter").toggle("fast");
}); {% endblock %}
enhance admin/base_site.html to add button
由于某种原因,Javascript 无法与 Django 5 一起使用。
我最终用一些自定义 CSS 覆盖了模板,通过采用变更列表模板,效果很好。
您还可以正确地在媒体中仅添加自定义 CSS 文件。
无悬停视图:
正常悬停视图:
For some reason the the Javascripts didn't work with Django 5.
I ended up overwriting the template with some custom CSS, which works just fine, by adopting the changelists template.
You could properly also add only a custom CSS file inside the media.
No hover view:
Hover view as normal:
鉴于您现在在 django admin 中拥有 jQuery,可以轻松地将
slideToggle()
绑定到列表过滤器中的标题。这似乎足以让它工作:
然后在
ModelAdmin
子类中,您想要激活设置媒体内部类:确保将 list_filter_collapse.js 文件放在 STATIC_DIRS 内的“js”文件夹中或 STATIC_ROOT (取决于您的 Django 版本)
Given that you now have jQuery in django admin, it's easy to bind a
slideToggle()
to the titles in the List Filter.This seems enough Javascript for it to work:
Then in the
ModelAdmin
subclass you want to activate that set the Media inner class:Make sure to drop the list_filter_collapse.js file in a 'js' folder inside your STATIC_DIRS or STATIC_ROOT (Depending on your Django version)
我更改了 Jj 的答案,以在单击“过滤器”标题时折叠整个过滤器,为了完整性将其添加到此处,要点如下 这里:
I changed Jj's answer to collapse the whole filter when clicking on the 'filter' title, adding it here for completeness, a gist is available here:
为此,我编写了一个小片段,可在 bitbucket 上下载。
过滤器的状态存储在 cookie 中,并且所选过滤器保持可见。
I have written a small snippets downloadable on bitbucket for the purpose.
The state of the filters are stored in a cookie and the selected filters stay visible.
感谢@JJ的想法。
我为整个窗口添加了切换开关,比 @abyx 的实现更简单。
这是 js 文件内容:
Thanks to @JJ's idea.
I added toggles for the whole window, simpler than @abyx's implement.
This is the js file content: