Replies: 6 comments 20 replies
-
|
You need to use unique keys on the x-for. |
Beta Was this translation helpful? Give feedback.
-
|
The sort plugin also looks broken to me (just because I can't access this.$store directly). Another approach is to have a "sort" button or up/down buttons near every element. Up/Down buttons: Sort button: |
Beta Was this translation helpful? Give feedback.
-
|
This is solved with #4361 |
Beta Was this translation helpful? Give feedback.
-
|
Hi guys I finally found a solution that works without issues so far. The trick is to give random :keys in the x-for template tag. I do this by incrementing a sortIteration variable every time the sort handler is called. That way a rerender is forced. <div x-data="columnSorting">
<ul x-sort="handleSortOrder">
<template x-for="column in selectedColumns" :key="sortIteration + column">
<li x-sort:item="column">
<span x-text="column"></span>
</li>
</template>
</ul>
</div>Alpine.data('columnSorting', () => {
let self;
return {
selectedColumns: ['item1', 'item2', 'item3'],
sortIteration: 0,
init() {
self = this;
},
handleSortOrder(item, newIndex) {
let currentIndex = self.selectedColumns.indexOf(item);
self.selectedColumns.splice(currentIndex, 1);
self.selectedColumns.splice(newIndex, 0, item);
self.sortIteration++;
},
}
});Since I don't get access to the alpine store data object using "this", I have to declare a self variable outside of the object. Hacky workaround, but works. Hope that helps anyone. |
Beta Was this translation helpful? Give feedback.
-
|
This works for me <ul x-sort="$store.myStore.handleSort">
<template x-for="(item) in $store.myStore.items" :key="item.id + '-' + item.order">
<li x-sort:item="{ item }">
...
</li>
</template>
</ul>In handleSort() I make a POJO of the items array, manually update the item.order then overwrite the store's version. Working fine so far. |
Beta Was this translation helpful? Give feedback.
-
|
Just ran into this as well. I'm setting the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have issue with x-for and sorting the elements.
I have an array that I want to sort using alpine x-sort plugin. Issue is that each element of the array has an x-model attribute to allow modification of that element.
Once I move element around, the array updates correctly but since the original element is moved in dom, x-model render the wrong position in the array.
I know that the issue is with using the index with x-model.
Codepen for reference: https://codepen.io/nlet/pen/qBwJJzg
Beta Was this translation helpful? Give feedback.
All reactions