最小化 django-admin 中的列表过滤器 [英] Minimize the list filter in django-admin

查看:24
本文介绍了最小化 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)?

推荐答案

鉴于您现在在 django admin 中有 jQuery,很容易将 slideToggle() 绑定到列表过滤器中的标题.

Given that you now have jQuery in django admin, it's easy to bind a slideToggle() to the titles in the List Filter.

这似乎足以让 Javascript 工作:

This seems enough Javascript for it to work:

// Fancier version https://gist.github.com/985283 

;(function($){ $(document).ready(function(){
    $('#changelist-filter').children('h3').each(function(){
        var $title = $(this);
        $title.click(function(){
            $title.next().slideToggle();
        });
    });   
  });
})(django.jQuery);

然后在 ModelAdmin 子类中你要激活那个设置 Media 内部类:

Then in the ModelAdmin subclass you want to activate that set the Media inner class:

class MyModelAdmin(admin.ModelAdmin):
  list_filter = ['bla', 'bleh']
  class Media:
    js = ['js/list_filter_collapse.js']

确保将 list_filter_collapse.js 文件放在 STATIC_DIRS 或 STATIC_ROOT 内的js"文件夹中(取决于您的 Django 版本)

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)

这篇关于最小化 django-admin 中的列表过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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