Skip to content

Commit 2d8bf5d

Browse files
committed
Support of CSS classes for ordering
1 parent a14ecf9 commit 2d8bf5d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/webstack_django_sorting/common.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ def render_sort_anchor(
2020
"""Render an HTML anchor tag for sorting a column."""
2121
get_params = request.GET.copy()
2222
sort_by = get_params.get("sort", None)
23+
css_class = ""
24+
icon = ""
25+
2326
if sort_by == field_name:
2427
dir = get_params.get("dir", "")
2528

2629
if dir == "asc":
2730
icon = settings.DEFAULT_SORT_UP
31+
css_class = settings.SORTING_CSS_CLASS_ASC
2832
elif dir == "desc":
2933
icon = settings.DEFAULT_SORT_DOWN
30-
else:
31-
icon = ""
34+
css_class = settings.SORTING_CSS_CLASS_DESC
3235

3336
# Mapping of direction transitions based on the default sort direction
3437
transition_map = {
@@ -38,14 +41,16 @@ def render_sort_anchor(
3841
next_direction_code = transition_map[default_direction].get(dir, "")
3942

4043
else:
41-
icon = ""
4244
next_direction_code = default_direction
4345

4446
# Not usual dict (can't update to replace)
4547
get_params["sort"] = field_name
4648
get_params["dir"] = next_direction_code
4749
url_append = "?" + get_params.urlencode() if get_params else ""
48-
return f'<a href="{request.path}{url_append}" title="{title}">{title}{icon}</a>'
50+
51+
# Build the anchor tag with optional CSS class
52+
class_attr = f' class="{css_class}"' if css_class else ""
53+
return f'<a href="{request.path}{url_append}"{class_attr} title="{title}">{title}{icon}</a>'
4954

5055

5156
def get_order_by_from_request(request: HttpRequest) -> str:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
from django.conf import settings
22

3+
# HTML entities for sort indicators (default)
34
DEFAULT_SORT_UP = getattr(settings, "DEFAULT_SORT_UP", " &uarr;")
45
DEFAULT_SORT_DOWN = getattr(settings, "DEFAULT_SORT_DOWN", " &darr;")
6+
7+
# CSS class-based sort indicators (alternative to HTML entities)
8+
# When set, these CSS classes are added to the anchor tag instead of using icons
9+
# Example: SORTING_CSS_CLASS_ASC = "sorted-asc" produces <a class="sorted-asc" ...>
10+
SORTING_CSS_CLASS_ASC = getattr(settings, "SORTING_CSS_CLASS_ASC", "")
11+
SORTING_CSS_CLASS_DESC = getattr(settings, "SORTING_CSS_CLASS_DESC", "")
12+
513
INVALID_FIELD_RAISES_404 = getattr(settings, "SORTING_INVALID_FIELD_RAISES_404", False)

0 commit comments

Comments
 (0)