本文介绍了在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管理员中有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 子类中激活设置媒体内部类:

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中最小化列表过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:49