You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This wrapper only impacts the actual DOM order, **it does not mutate the data order**.
149
+
This avoids a lot of overhead in the code, and give you the full control on your data.
150
+
151
+
It is really simple to change the order in your data after an item is dropped:
152
+
```html
153
+
<template>
154
+
<divv-sortable@end="onOrderChange">
155
+
<divv-for="item in items">
156
+
{{ item }}
157
+
</div>
158
+
</div>
159
+
160
+
<span>Items data: {{ items }}</span>
161
+
</template>
162
+
163
+
<script>
164
+
exportdefault {
165
+
data() {
166
+
return {
167
+
items: [ "a", "b", "c" ]
168
+
}
169
+
},
170
+
171
+
methods: {
172
+
onOrderChange(event) {
173
+
// Remove item from old index
174
+
let item =this.items.splice(event.oldIndex, 1)[0];
175
+
176
+
// Insert at new index
177
+
this.items.splice(event.newIndex, 0, item);
178
+
}
179
+
}
180
+
};
181
+
</script>
182
+
```
183
+
184
+
185
+
## Notes
186
+
187
+
It is highly recommended to set a **key on the children items**, to help Sortable track the DOM:
188
+
189
+
```html
190
+
<template>
191
+
<divv-sortable>
192
+
<divkey="a">a</div>
193
+
<divkey="b">b</div>
194
+
<divkey="c">c</div>
195
+
</div>
196
+
</template>
197
+
```
198
+
199
+
In the same way, if you use the `group` option, it is highly recommended to set a **key on the parent** itself. Otherwise the DOM managed by Sortable can become out-of-sync with the actual data state. I have noticed this helps a lot when using Sortable with complex components.
200
+
The key must be based on the number of items the parent contains. This will force a re-render when an item is added / removed, and make Sortable re-initialize and start from a clean state every time. This may seem a bit hacky, but it's the only way to keep a consistant behavior.
0 commit comments