Skip to content

Commit 24b9f16

Browse files
authored
feat: horizontal choices filter attribute (#1092)
1 parent 2cd48e2 commit 24b9f16

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

docs/filters/horizontal.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Horizontal layout filter
3+
order: 5
4+
description: Horizontal layout for filter choices in the changelist view.
5+
---
6+
7+
# Horizontal layout filter
8+
9+
Unfold provides a horizontal layout for filter choices in the changelist view. By default, the layout for filter choices is vertical but by providing the `horizontal` attribute set to `True` to the filter class, the layout will be horizontal.
10+
11+
```python
12+
# admin.py
13+
14+
from django.contrib import admin
15+
from django.contrib.auth import get_user_model
16+
from django.contrib.admin.filters import ChoicesFieldListFilter
17+
18+
from unfold.admin import ModelAdmin
19+
20+
21+
User = get_user_model()
22+
23+
class HorizontalChoicesFieldListFilter(ChoicesFieldListFilter):
24+
horizontal = True # Enable horizontal layout
25+
26+
27+
@admin.register(User)
28+
class UserAdmin(ModelAdmin):
29+
list_filter = (
30+
["other_model_field", HorizontalChoicesFieldListFilter],
31+
)
32+
```

src/unfold/templates/admin/filter.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h3 class="font-semibold mb-2 text-font-important-light dark:text-font-important
1111
{% endif %}
1212
{% endfor %}
1313

14-
{% if spec|class_name == "BooleanFieldListFilter" %}
14+
{% if spec|class_name == "BooleanFieldListFilter" or spec.horizontal %}
1515
<ul class="dark:bg-base-900 border border-base-200 flex min-w-20 rounded shadow-sm text-font-default-light dark:border-base-700 dark:text-font-default-dark w-full">
1616
{% for choice in choices %}
1717
<li class="basis-1/3 border-r border-base-200 flex-grow truncate last:border-r-0 dark:border-base-700 {% if choice.selected %}font-semibold text-primary-600 dark:text-primary-500 {% else %}hover:text-base-700 dark:hover:text-base-200{% endif %}">

0 commit comments

Comments
 (0)