Sorting
Drag-and-drop reordering with server-side state updates. Requires Alpine.js Sort plugin.
Installation
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/sort@3.x.x/dist/cdn.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
Basic usage
PREVIEW
- ⠿ Draft
- ⠿ Review
- ⠿ Publish
- ⠿ Archive
class SortDemo(Component):
items: list = ["Draft", "Review", "Publish", "Archive"]
def reorder(self, item, position):
self.items.remove(item)
self.items.insert(int(position), item)
def inline_template(self):
return """
<ul lace:sort="reorder($item, $position)" class="...">
{% for item in items %}
<li lace:sort:item="{{ item }}" lace:key="{{ item }}" class="...">
<span lace:sort:handle class="...">⠿</span> {{ item }}
</li>
{% endfor %}
</ul>
"""
Cross-list sorting
Drag cards within a column and across columns. Each column is a sortable group sharing a group name, and moves persist server-side.
PREVIEW
Recruitment Pipeline
Drag candidates between stages or reorder columns
Applied
2Alice Chen
Bob Smith
Screening
1Carol Wu
Interview
1David Park
class KanbanBoard(Component):
columns: list = []
def mount(self):
# demo data; in a real app load from the DB, e.g.
# Candidate.objects.order_by("position") grouped by stage
self.columns = [
{"id": "applied", "title": "Applied", "candidates": [
{"id": 1, "name": "Alice Chen"}, {"id": 2, "name": "Bob Smith"}]},
{"id": "screening", "title": "Screening", "candidates": [
{"id": 3, "name": "Carol Wu"}]},
{"id": "interview", "title": "Interview", "candidates": [
{"id": 4, "name": "David Park"}]},
]
def move_candidate(self, candidate_id, position, target_column_id):
candidate_id = int(candidate_id)
# pull the candidate out of whichever column holds it
candidate = None
for column in self.columns:
for i, c in enumerate(column["candidates"]):
if c["id"] == candidate_id:
candidate = column["candidates"].pop(i)
break
if candidate:
break
# drop it into the target column at the new position
target = next(c for c in self.columns if c["id"] == target_column_id)
target["candidates"].insert(int(position), candidate)
def reorder_columns(self, column_id, new_position):
column = next(c for c in self.columns if c["id"] == column_id)
self.columns.remove(column)
self.columns.insert(int(new_position), column)
{# the columns themselves are sortable (reorder left/right) #}
<div lace:sort="reorder_columns($item, $position)" class="...">
{% for column in columns %}
<div lace:sort:item="{{ column.id }}" lace:key="col-{{ column.id }}" class="...">
<h3><span lace:sort:handle>⠁</span> {{ column.title }}</h3>
{# candidates share ONE group name, so they drag within and across columns #}
<div lace:sort="move_candidate($item, $position, '{{ column.id }}')"
lace:sort:group="candidates" class="...">
{% for candidate in column.candidates %}
<div lace:sort:item="{{ candidate.id }}" lace:key="card-{{ candidate.id }}" class="...">
{{ candidate.name }}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
See lace:sort for the full list of sort sub-directives
(lace:sort:item, lace:sort:handle, lace:sort:group, lace:sort:ignore).