SortableJS / vue.draggable.next

Vue 3 compatible drag-and-drop component based on Sortable.js
https://sortablejs.github.io/vue.draggable.next/#/simple
MIT License
3.91k stars 531 forks source link

How to use array subscripts as itemKey #38

Closed AHRL closed 3 years ago

AHRL commented 3 years ago
<draggable
        v-if="model && model.length"
        v-model="model"
        item-key="$index"
      >
        <template #item="{ element, $index }">
        </template>
</draggable>

model = ['111', '222'] or [{ a: '111', b: '222' }]

because value is uncertain in model, so i want to use array subscripts as itemKey. please help me ~

sdwdjzhy commented 3 years ago

code like this:

    computed: {
        value: {
            get() {
                return this.model.map((i) => ({ id: i }));
            },
            set(val) {
                this.model = val.map((i) => i.id );
            },
        },
    },

so you can do :

<draggable
        v-if="model && model.length"
        v-model="value"
        item-key="id"
      >
        <template #item="{ element, index }">
             {{ element.id }}
        </template>
</draggable>
AHRL commented 3 years ago

thanks~