How to disable dragging on specific element using Vue.Draggable
Asked Answered
G

6

18

How can I set so that only .btn-drag can drag whole row? I am using https://github.com/SortableJS/Vue.Draggable

Currently I can drag also with button#options which is undesired

    <draggable v-model="textElements">
        <transition-group>
            <div class="is-draggable" v-for="element in textElements" :key="element.text">
                <div>
                    {{ element.text }}
                </div>
                <button class="btn btn-transparent">Options</button>
                <button class="btn btn-transparent btn-drag">Drag</button>
            </div>
        </transition-group>
    </draggable>

In docs they mention that we can place :move="checkMove" on <draggable> but in function I am not sure how can I check what exactly was dragged? Returning false works correctly for disabling dragging in general

methods: {
    checkMove(evt) {
        console.log(evt)
        return false
    }
}

Console.log(evt) shows this but I am not sure which property I can use to pinpoint exactly what started a drag https://image.prntscr.com/image/r17zNkxoSWGdVQs_5nR09w.png

Garter answered 26/12, 2017 at 20:36 Comment(0)
A
17

The functionality is called "handles".

https://github.com/SortableJS/Vue.Draggable#features

https://github.com/SortableJS/Vue.Draggable#options

https://github.com/RubaXa/Sortable#options
handle option here should help.

Awad answered 27/12, 2017 at 2:1 Comment(2)
Thank you, this is a solution: <draggable :options="{handle: '.btn-drag'}" ...> – Garter
The actual solution (to the title) is to use filter, for anyone wondering – Margarine
R
7

Starting from version 2.19, you can use

<draggable handle=".handle">
Rascal answered 7/3, 2019 at 23:19 Comment(0)
R
4

Here the elements with class "item" are only draggable , that can specified like this draggable=".item" usign the draggable option inside the draggable tag

<draggable v-model="myArray" draggable=".item">
<div v-for="element in myArray" :key="element.id" class="item">
    {{element.name}}
</div>
<button slot="header" @click="addPeople">Add</button>
Rompish answered 24/3, 2021 at 10:6 Comment(0)
K
1

You have to pass a prop in a draggable component.

Example:

<draggable :options="{handle: '.drag-only-this'}">

For exploring more here is a All sortable options

Konopka answered 29/12, 2021 at 9:25 Comment(0)
H
1

can use :not() selector for specific element for group of multiple elements

<draggable v-model="myArray" draggable=".item:not(.exclude-this-item)"> πŸ‘ˆ
  <div v-for="element in myArray" :key="element.id" class="item exclude-this-item"> πŸ‘ˆ
    {{element.id}}
  </div>
  <div v-for="element in myArray" :key="element.name" class="item">
    {{element.name}}
  </div>
  <div v-for="element in myArray" :key="element.city" class="item">
    {{element.city}}
  </div>
</draggable>
Hatfield answered 2/2, 2022 at 8:56 Comment(0)
A
0

the easiest way is to use the prop move

<draggable :list="list" :move="checkMove">

checkMove: function(evt){
  return evt.draggedContext.element.name === 'draggable element';
}

This checkMove function is called for each element in the list. if it returns true the element will be draggable and vice versa.

Likewise if you want to make the elements non draggable and fixed in its positions, draggable prop can be used. :draggable=".class-name". put the elements that you want to drag under the class .class-name. Rest of the elements will stay put in its place.

Aladdin answered 14/2 at 11:16 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.