0

I am trying to display a list of draggable elements using vue-draggable, which are sometimes separated by a fixed element at given position(s).

So far I tried the following approach : creating a different element when needed inside the v-for loop.

<draggable :list="list" class="dragArea" :class="{dragArea: true}" 
           :options="{group:'people', draggable: '.drag'}" @change="handleChange">
    <template v-for="(element, index) in list">
      <div class="drag">
        {{element.name}}
      </div>
      <div class="nodrag" v-if="index === 2">Fixed element</div>
    </template>
</draggable>

However, it is breaking the behavior of my app, as the indexes returned by the onChange event are not what is expected anymore. (You can check a reproduction on this jsfiddle)

Am I doing this wrong ? I also considered using the move prop to disable the dragging ability as suggested here, but the problem remains, because I am using element from outside the draggable list I believe.

In our production usecase, fixed element's index is variable, if that matters.

7
  • So the position of fixed element should not change? or are they just not draggable? Commented Nov 13, 2018 at 16:04
  • Position of fixed element should not change, there should always be the same number of draggable items before and after it. It will actually represent some kind of a threshold Commented Nov 13, 2018 at 16:16
  • Why don't you make two lists? Otherwise the expectation doesn't really make sense. If there are 2 elements before fixed and 2 elements after fixed element --> then you move 1 element from before fixed to after, so, what will be the elements before fixed? an empty string? Commented Nov 13, 2018 at 16:34
  • Say you have A,B,Fixed,C,D and you move B inbetween C and D, then you should get A,C,Fixed,B,D Ideally I would like Fixed to be just a visual sign completely ignored by the draggable component logic Commented Nov 13, 2018 at 16:44
  • 1
    I would listen to some onChange event and just manually move the elements above/below the fixed element. When using something like vue-draggable you have to remember that if it wasn't developed with the functionality you have in mind you're probably gonna have to create it yourself, i.e. moving them "manually" up and down. You can also fork vue-draggable or make a pull request in order to add the functionality so you can keep your code cleaner. Commented Nov 14, 2018 at 6:09

2 Answers 2

1

You can use move in your draggable component, and then, to specify what elements will be fixed. Something like this:

<draggable
  :list="list"
  :disabled="!enabled"
  class="your_class"
  :move="checkMove"
  @start="dragging = true"
  @end="dragging = false"
>
...
</draggable>

...........

data() {
  return{
    list: [{foo: "bar", ..., fixed: true}, {foo: "bar", ...}, {foo: "bar", ...}, {foo: "bar", ..., fixed: true}]
  }
}

...........

methods: {
  checkMove(e) {
    return !this.list[e.draggedContext.futureIndex].fixed
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is you need to include your "fixed elements" as data in the array you are passing to Vue.Draggable... You also cannot have the v-if because SortableJS will count those as an element, even though they are not visible, because VueJS leaves invisible elements with v-if's as comments... SortableJS will index these comments. So add an item to your array after John3, and get rid of the v-if.. instead, include a draggable property in your objects and only set the 'drag' class if that element has that draggable property set to true. The last issue is that Sortable will not index an element that is not draggable, but this will probably be fixed in a later update. I recommend you use filter option for now.

Example (not using filter): https://jsfiddle.net/3wrj07et/5/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.