sortablejs-vue3 icon indicating copy to clipboard operation
sortablejs-vue3 copied to clipboard

How set item-key when List type is string[]

Open de1ck opened this issue 2 years ago • 3 comments

de1ck avatar Sep 08 '22 11:09 de1ck

Well, this is something I overlooked... at the moment, you can't. The easiest thing to do would be to adjust your data so each item has a unique key. If you want to make a pull request, the library could be modified to work with primitive arrays.

MaxLeiter avatar Sep 09 '22 17:09 MaxLeiter

You can provide the item index to the item-key callback as you do with the item. This will allow us to use the index as the key as a solution.

Alhubing avatar Jan 11 '23 17:01 Alhubing

+1 for this. In my case, my array contains the ids and the child components use this to access other data elsewhere. So I can use :itemKey="x => x"

I'm pretty sure this could be adapted for non-unique strings by changing line 162 of Sortable.vue to:

        :key="getKey(item, index)"

and then using :itemKey="(_, i) => i"

dsl101 avatar Feb 08 '24 17:02 dsl101