vue.draggable.next icon indicating copy to clipboard operation
vue.draggable.next copied to clipboard

Refs inside v-for does not work within draggable

Open Luciennnnnnn opened this issue 3 years ago • 9 comments

I put a component in draggable like this:

const refs = ref([])

<draggable :list="imageUrls" item-key="url" ghost-class="ghost" dragClass="dragitem" animation=500 handle=".mover" @start="dragging = true" @end="endDrag"

<template #item="{element, index}" style="flex-basis:none">

but it seems does not work, refs only represent one component randomly, not an array.

I expect it could be used like this

@David-Desmaisons

Luciennnnnnn avatar Jun 24 '22 08:06 Luciennnnnnn

Same problem here. I need to use a template ref on the elements inside the draggable template, but I'm only getting one Item and not the whole list. Any updates on this?

Is there another way of getting the complete node list of elements inside the draggable template ?

This does not work: <draggable :list="previewArray.images" group="images" @start="drag = true" @end="drag = false" item-key="id" class="upload_previews" > <template #item="{ element }"> <div class="image"> <img class="preview imgPreview" :src="element.image" ref="imgRefs" /> </div> </template> </draggable>

StefanFlaschko avatar Sep 26 '22 13:09 StefanFlaschko

Same problem here.

alexfict avatar Sep 28 '22 08:09 alexfict

Might be helpful. https://stackoverflow.com/questions/71414977/refs-inside-v-for-loop-for-vue-v3-2-25-or-above

murohamasho0606 avatar Oct 25 '22 07:10 murohamasho0606

Might be helpful. https://stackoverflow.com/questions/71414977/refs-inside-v-for-loop-for-vue-v3-2-25-or-above

It doesn't help.

yoyosan avatar Oct 25 '22 15:10 yoyosan

Do you guys have any update for this issue?

sonnguyenxaion avatar Nov 11 '22 08:11 sonnguyenxaion

I ended up doing something like this,

In <template>,

<Draggable v-model="emails" class="column" @change="update" item-key="id" ghost-class="ghost" group="emails">
      <template #item="{ element, index }">
        <EmailCard
          :key="element.id"
          class="q-my-sm"
          :ref="addEmailCardRef"
          v-model:item="emails[index]"
        />
      </template>
</Draggable>

In <script setup lang="ts">,


/**
 * Refs to EmailCard components
 */
const emailCards = ref<Array<InstanceType<typeof EmailCard>>>([]);

function addEmailCardRef(element: InstanceType<typeof EmailCard>) {
  if (emailCards.value.indexOf(element) === -1) {
    emailCards.value.push(element);
  }
}

And to call something on each component, use:

emailCards.value.forEach((component) => {
    if (component) {
      component.checkFieldsValid();
    }
  });

yoyosan avatar Nov 11 '22 11:11 yoyosan

@yoyosan Thank you so much but I tried to implement as yours but it seems not to work. image

<Draggable
      v-if="!isLoading"
      v-bind="dragOptions"
      v-model="listData"
      group="list"
      item-key="listId"
      tag="transition-group"
      :component-data="{
        name: 'flip-list',
        type: 'transition',
        class: $style.listGroupContainer,
        tag: 'div',
      }"
      @start="drag = true"
      @change="onMovedList"
    >
      <template #item="{ element: item }">
        <StatusList
          :ref="addStatusListRef"
          v-model:model-flag="flag"
          :list-id="item.listId"
        />
      </template>
    </Draggable>
const statusLists = ref<Array<InstanceType<typeof StatusList>>>([]);
const addStatusListRef = (element: InstanceType<typeof StatusList>) => {
  console.log('element: ', element);
  if (statusLists.value.indexOf(element) === -1) {
    statusLists.value.push(element);
  }
};

sonnguyenxaion avatar Nov 15 '22 15:11 sonnguyenxaion

@yoyosan I already solved my issue, this errors was because missing :key props in <Draggable/>

sonnguyenxaion avatar Nov 15 '22 16:11 sonnguyenxaion