Skip to content

Commit e154d93

Browse files
Merge pull request #661 from ExpressionEngine/feature/7.x/sortablejs-for-relationship-channel-form
Added example how to use SortableJs for Relationship in Channel Form
2 parents 53d0270 + 8708625 commit e154d93

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

docs/_images/sort-relationship.gif

126 KB
Loading

docs/channels/channel-form/fields.md

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,56 @@ Simple relationship fields with only 1 relationship allowed are very similar in
124124
{/options:my_field_name}
125125
</select>
126126

127-
For relationship fields that allow multiple selections, you may also set the sort order:
128-
129-
{options:my_field_name}
130-
Order:
131-
<input type="text" name="my_field_name[sort][]" value="{option_order}" />
132-
<input type="checkbox" name="my_field_name[data][]" value="{option_value}" {checked}/>
133-
{option_name}<br />
134-
{/options:my_field_name}
127+
For relationship fields that allow multiple selections, you may also set the reorderable drag-and-drop lists:
128+
129+
{exp:channel:entries channel="yourChannel"}
130+
<ul id="list-group">
131+
{relationship_field}
132+
<li class="list-item">
133+
<div class="list-item__handle">::</div>
134+
<div class="list-item__content">
135+
<div class="list-item__title">{relationship_field:title}</div>
136+
<div class="list-item__secondary"><span> #{relationship_field:entry_id} / </span>{relationship_field:status}</div>
137+
</div>
138+
</li>
139+
{/relationship_field}
140+
</ul>
141+
{/exp:channel:entries}
142+
143+
You would need a bit of Javascript code (the example below is using SortableJs library):
144+
145+
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
146+
<script>
147+
var el = document.getElementById('list-group');
148+
var sortable = Sortable.create(el);
149+
</script>
150+
151+
Add CSS:
152+
153+
<style>
154+
#list-group {
155+
list-style-type: none;
156+
padding: 0;
157+
margin-left: 15px;
158+
}
159+
#list-group .list-item{
160+
margin-bottom: 10px;
161+
border: 1px solid #cecece;
162+
padding: 5px 15px;
163+
background: #fff;
164+
display: flex;
165+
}
166+
#list-group .list-item__title {
167+
font-weight: bold;
168+
}
169+
#list-group .list-item__handle {
170+
cursor: move;
171+
cursor: -webkit-grabbing;
172+
margin-right: 10px;
173+
}
174+
</style>
175+
176+
![Relationship fields](_images/sort-relationship.gif)
135177

136178
### Select field
137179

0 commit comments

Comments
 (0)