MaxLeiter / sortablejs-vue3

A thin wrapper around Sortable.js for Vue 3
https://sortablejs-vue3.maxleiter.com
MIT License
380 stars 21 forks source link

How to get the new list as an array? #100

Open idesignzone opened 11 months ago

idesignzone commented 11 months ago

I have trouble getting the new array list. here is my code

<template>
  <div>
    <Sortable :list="elements" item-key="id" tag="div" :options="options" @change="DragableData($event)">
      <template #item="{ element, index }">
        <div :key="element.id">
          {{ index }} - {{ element.name }}
        </div>
      </template>
    </Sortable>
  </div>
</template>

<script setup>
const elements = [
  { id: "1", name: "one" },
  { id: "2", name: "two" },
  { id: "3", name: "three" },
  { id: "4", name: "four" },
];
const options = {sort: true};

function DragableData(data) {
  console.log(data)
}
</script>

data here does not have any array in it. is it possible to get new array?

eazyurk commented 11 months ago

I did the following on the Sortable element I added: @end="(event) => onEnd(event)"

Then the onEnd function:

function onEnd(event) {
    array.splice(event.newIndex, 0, array.splice(event.oldIndex, 1)[0]);
}