Django admin list_filter is too long
I have list_filter
with many sectors. This list on the right side of the page is too long.
Can I use an input select box because I can't select more than one sector?
I've seen this before, screenshots, but I can't seem to find a way to do it.
change:
I have my own FilterSpec and not list_filter
source to share
This is how I resolved it (jQuery):
$('#changelist-filter ul').each(function(){
var maxlength = 10;
if ($(this).children().length > maxlength )
{
var list=$(this),
select=$(document.createElement('select')).insertBefore($(this).hide());
$('>li a', this).each(function(){
console.log($(this).parent().attr('class'));
var target=$(this).attr('target'),
option=$(document.createElement('option'))
.appendTo(select)
.val(this.href)
.attr('selected', $(this).parent().attr('class'))
.html($(this).html())
.click(function(){
if (target==='_blank'){
window.open($(this).val());
}
else{
window.location.href=$(this).val();
}
});
});
list.remove();
}
});
source to share
You can write your own FilterSpec (custom user list filter).
This feature is not yet part of the Django code; this is planned for version 1.2. You will need to apply this patch to your Django code: http://code.djangoproject.com/ticket/5833 .
In stackoverflow there are many examples of how to do this, for example: fooobar.com/questions/77872 / ... .
source to share
The long list you said comes from the default "admin / filter.html" template, in django / contrib / admin / templates / admin / filter.html built-in ListFilters.
There are several ways to configure it:
-
Override 'admin / filter.html' globally. Highlight the select tag instead of the ul tag if the number of selections reaches a certain limit. This affects all list filters in admin. The select tag must have an onchange event handler like
<select ... onchange = "location.href = this.options [this.selectedIndex] .value">
-
Set the template attribute on your specific ListFilter instance to the name of the custom filter template. The content of the template is similar to # 1. This requires Django 1.4+.
- Add javascript to your ModelAdmin instance to transform HTML content inside the ul tag to select the tag once the DOM is fully loaded.
source to share