Skip to content

How can i set a selected value without using the mouse (programmatically, on init) ? #1048

Open
@edvinkuric

Description

@edvinkuric

Currently, i am trying to select an item of the draggable-list programmatically / manually (without the usage of the mouse).

This behaviour is important, because i want to make a pre-selection when the draggable-component is loaded.
I wanted to use the "vuedraggable-multi" Fork (check #744 or https://github.com/divinespear/Vue.Draggable ) as well, which is based on this repository. This feature would allow me to select multiple elements for the drag-and-drop feature.
I already tested the #744 and it works for me very well - sad to see that it is not merged yet.

I have read, that the "MultiDrag.singleton.utils.select()"-Method is the way to go for this, but my current Implementation does not contain this singleton (MultiDrag.singleton is undefined when i am importing MultiDrag).
This link describes how it should have worked: https://www.fabiofranchino.com/log/add-programmatically-a-draggable-element-in-vuedraggable-with-sortable/

I can set the styling/css-class for the selected element manually, but this has no effect on the internal selection of the vueDraggable-component.

Jsfiddle link

N/A, since the feature does not exist yet afaik.

Relevant code-parts:

import { defineComponent, onMounted } from '@vue/composition-api';
import VueDraggable from 'vuedraggable';
import {Sortable, MultiDrag} from "sortablejs"; 

if (!MultiDrag.singleton) {
   console.log("singleton should exist, when using vuedraggable - but it doesn't");
   MultiDrag.singleton = new MultiDrag();
   Sortable.mount(MultiDrag.singleton);
}

export default defineComponent({
    name: 'MyPage',
    components: {
         draggable: VueDraggable
     },
    setup(_, ctx) {
  ... 
  onMounted(async () => { 
     // try to select a vuedraggable-item, when the items are loaded via API-call
      const el = ctx.refs.draggablelist.$el.querySelector(".selected");
     // the following line doesn't change the internal state of the draggable-list 
      MultiDrag.singleton.utils.select(el);
    }
  }
});

Step by step scenario

  1. Open up VueDraggable and try to set the list-selection via javascript.

Current Solution

N/A - no method currently available to select items programmatically.
When importing MultiDrag from SortableJS ( import {Sortable, MultiDrag } from "sortablejs"; ), then the MultiDrag.singleton-field is undefined.

Expected Solution

  1. Expose the "MultiDrag.singleton.utils"-Methods from within VueDraggable, so that the selection can be done programmatically.
  2. If there are other methods for the item-selection, then expose these as well.
  3. Ideally, i can pass an Index/key of the listitem and the corresponding element is then added to the selection

Can you help me with this? That would be great :-)
If you can point me to the solution, i also can do a PR if desired.

fyi @David-Desmaisons @divinespear

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions