Skip to content

Instantly share code, notes, and snippets.

@stefanbackor
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanbackor/8fb29306b9ade7365aac to your computer and use it in GitHub Desktop.
Save stefanbackor/8fb29306b9ade7365aac to your computer and use it in GitHub Desktop.
Django Grappelli Admin: sidebar changelist with filter highlight
from django.contrib import admin
class AdminChangelistMixin(admin.ModelAdmin):
change_list_template = "grappelli-overrides/change_list_filter_sidebar.html"
change_list_filter_template = "grappelli-overrides/filter.html"
pass
{# templates/grappelli-overrides/change_list_filter_sidebar.html #}
{% extends "admin/change_list_filter_sidebar.html" %}
{% load i18n %}
{% load staticfiles %}
{% load admin_list %}
{% load admin_urls %}
{% load grp_tags %}
<!-- FILTERS -->
{% block filters %}
<style type="text/css" media="screen">
#grp-filters.closed .grp-module {
display:none;
}
/* HTML & Styles uses font Awesome carets for closing */
#grp-filters .fa { display:none; padding:0 0.5em; }
#grp-filters .fa-caret-down { display:inline; }
#grp-filters.closed .fa-caret-down { display:none; }
#grp-filters.closed .fa-caret-right { display:inline; }
#grp-filters.closed h2 {
border-bottom:none;
}
#grp-filters h2 a {
color:inherit;
cursor:pointer;
}
#grp-filters.grp-module.selected,
#grp-filters.grp-module.selected h2 {
border-color: #36b0d9;
}
#grp-filters.grp-module.selected h2 {
color :#fff;
background: url('data:image/svg+x…pZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
background: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #36b0d9),color-stop(100%, #309bbf));
background: -moz-linear-gradient(#36b0d9,#309bbf);
background: -webkit-linear-gradient(#36b0d9,#309bbf);
background: linear-gradient(#36b0d9,#309bbf);
text-shadow: none;
position: relative;
}
#grp-filters.grp-module h2 .reset {
color: #fff;
visibility: hidden;
position: absolute;
right: 0;
padding: 0 10px;
}
#grp-filters.grp-module h2 .reset:hover {
text-decoration:underline;
}
#grp-filters.grp-module.selected h2 .reset {
visibility: visible;
}
</style>
<div id="grp-filters" class="grp-module grp-filter {% if request.COOKIES.grappelli_filter_hide == "true" %}closed{% endif %}">
<h2><a class="toggler">{% trans 'Filter' %}<i class="fa fa-caret-down"></i><i class="fa fa-caret-right"></i></a> <a href="?" class="reset">reset</a></h2>
{% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
</div>
<script type="text/javascript">
(function($) {
// Remember closed filter (jquery.cookie.js required)
$("#grp-filters h2 .toggler").on("click",function(e){
$("#grp-filters").toggleClass("closed");
$.cookie('grappelli_filter_hide',$("#grp-filters").hasClass("closed"),{path:'/'});
e.preventDefault();
return false;
});
// Highlight filter box and each filter separately
{% for key,value in request.GET.items %}{% spaceless %}
{% for spec in cl.filter_specs %}{% spaceless %}
{% if key == spec.lookup_kwarg and value %}
$("#grp-filters").addClass("selected");
$("#grp-filters .filter-{{ spec.lookup_kwarg }}").addClass("selected");
{% endif %}
{% if key == spec.lookup_kwarg_isnull and value %}
$("#grp-filters").addClass("selected");
$("#grp-filters .filter-{{ spec.lookup_kwarg_isnull }}").addClass("selected");
{% endif %}
{% endspaceless %}{% endfor %}
{% endspaceless %}{% endfor %}
})(grp.jQuery);
</script>
{% endblock %}
{# templates/grappelli-overrides/filter.html #}
{% load i18n %}
<div class="grp-module filter-{{ spec.lookup_kwarg }} filter-{{ spec.lookup_kwarg_isnull }}">
<div class="grp-row">
<label>{% blocktrans with title|capfirst as filter_title %}{{ filter_title }}{% endblocktrans %}</label>
<select class="grp-filter-choice">
{% for choice in choices %}<option value="{{ choice.query_string|iriencode }}"{% if choice.selected %} selected='selected'{% endif %}>{{ choice.display }}</option>{% endfor %}
</select>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment