Prevent VueJs draggable from adding elements without cancelling the drag preview

Currently, I am exploring the functionality of draggable and sortable with VueJS by using Vue.Draggable library. My goal is to create a scenario where I have two lists: the first list contains sections like tables and paragraphs, while the second list consists of building blocks associated with these sections. The workflow I aim for involves dragging an element from the first list to the second list, without immediately adding the element. This step is crucial as I need to open a modal window first to set the properties of the element before adding it to the desired position.

Currently, I have a partial solution that almost meets my requirements. However, I am facing challenges in canceling the addition of the element without affecting the drag preview. By preview, I mean the visual indication of the dragged element shown here:

https://i.sstatic.net/Xf1vu.png

I want to visualize the element being added at the intended drop point.

In the first list setup, I have implemented the following code:

<draggable v-model="sectionList" class="dragArea" @end="changeView()" :move="moveItem" :options="{group:{ name:'sections',pull:'clone',put:false }}">
    <div v-for="(section, index) in sectionList"  class="card card-color list-complete-item">
        <div class="card-block">
            <h4 class="card-title text-center">{{section.text}}</h4>
        </div>
    </div>
</draggable>

methods: {
    addParagraph() {
        this.$set(this.paragraph, 'key', this.key);
        this.$set(this.paragraph, 'text', this.text);
        this.$set(this.paragraph, 'fontSize', this.fontSize);
        this.$store.commit("appendToDocument", this.paragraph)
    },
    changeView: function () {
        this.$store.commit("changeCurrentView", this.sectionList[this.dragedIndex].component);
        this.show();
    },
    show() {
        this.$modal.show('modalSection');
    },
    hide() {
        this.$modal.hide('modalSection');
    },
    currentIndex(evt) {
        console.log(evt.draggedContext.index);
    },
    moveItem(evt) { // future index of the modal that will be dragged
        console.log(evt.draggedContext.futureIndex);
        this.dragedIndex = evt.draggedContext.index;
    }
},

The setup for the second list is less significant compared to grasping the concept of moving items without immediate addition and having a proper preview display. I have tried using "return false" at the end of my code, but it resulted in issues related to drag previews and movement within the same list.

Any guidance or suggestions on how to achieve this objective would be greatly appreciated.

Answer №1

The Vue.draggable library, on which the sortable is based, does not include a drop event handler (link). However, you can start by using the https://jsfiddle.net/94z81596/10/ example to make use of the change handler:

<draggable 
  id="list2" 
  class="dragArea" 
  :options="{group:'people'}" 
  @change="afterAdd"
  :list="gDocument">
...
</draggable>

An alternative solution could be simpler, where you track the futureIndex, store it in the placeholder, and then return false in checkMove. Afterwards, you can manually insert the item through Vuex. My current approach already implements a similar process.

I chose to keep the preview for user guidance when inserting elements. To achieve this, I filtered the list as follows:

state.document = state.document.filter((item) => item.value);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When attempting to use the POST method with a JSON body in Postgresql, I encounter an error

Encountering an error: There seems to be an issue with a JSON token at position 197 while trying to parse it. It occurs in the following code snippet: at JSON.parse () at parse (C:\Users\hp\empServers\node_modules\body-parser&bso ...

How to pass a JavaScript variable value to a SQL query in PHP on a separate webpage?

I have 2 elements - an auto search bar and Map using mapbox-gl. I want to load the location of the user whose id is searched. I am getting the same marker (location) for every user. I am loading "get_pin.php" through xmlHttpRequest from the main page "m ...

Using dynamic colors in Vue.js

I'm currently trying to bind multiple colors to a spinner component, but I'm facing some difficulties with it and my mind is starting to feel a bit foggy. Below is the spinner component itself, bound to a computed value. <fingerprint-spinner ...

Guide to configuring the overflow-y property for individual cells or rows in a Bootstrap-Vue table

Hello there, I am currently using Bootstrap-vue to display data that has been queried from a database. My goal is to have it displayed with an overflow-y style similar to this image. If you know how to achieve this effect, please share your solution. Here ...

How to disable event.preventDefault on a hyperlink with a delay using jQuery or JavaScript

I need to disable event.preventDefault after a certain period of time on a hyperlink using jQuery or JavaScript. When I click on the link, it should redirect me to the specified href link after a delay because I need to send an AJAX request during that tim ...

What is the process for rendering a Nested Component?

Let's develop a component that is used within another component on a webpage. Here's an example - MainComponent.tsx import NestedComponent from '../components/nestedComponent'; const MainComponent = () => { const { value, se ...

Retrieving an array value by key from user input in HTML

<input type='number' name="productId" id="productId" value="5"> <input type="number" name="quantity[]" id="quantity"> In order to retrieve the quantity value, I utilized var values = $("input[name='quantity[]']") ...

Tips for managing console warnings in React 16.9 - muting or resolving the issue

Recently upgraded to React 16.9, but encountering these console errors: Warning: componentWillMount has been renamed and is not recommended for use. as well as Warning: componentWillReceiveProps has been renamed and is not recommended for use. I am awa ...

Using jQuery to implement tiered radio buttons styled with Bootstrap

I am currently in the process of learning jQuery, so please excuse my lack of knowledge in this area. I am working on a web form using Bootstrap that involves tiered radio buttons. When the user selects "Yes" for the first option, it should display the ne ...

Is there a way to modify the orientation of the bootstrap modal when it opens?

I am trying to modify the way my bootstrap reveal modal opens. I have followed the code instructions provided in this resource, but unfortunately my modal is not opening. I am using angularjs. Can someone assist me with troubleshooting the code? Here is m ...

How should event listeners be unbound and child elements removed in single page applications?

I've recently delved into the world of memory leaks in JavaScript while working on a large single-page application. After using Chrome's Profiles (Snapshot) function, I noticed an abundance of detached DOM elements indicating a possible memory le ...

It appears that when importing from a shared package in lerna, the name must include "src" at the end for Typescript or Javascript files

I am currently working on a straightforward lerna project structure as shown below: Project | +-- packages | | | +-- shared | | | | | +-- src | | | | | +-- index.ts | | +-- someDir | | | +-- usesShared | ...

Having trouble retrieving JSON with crossDomain jQuery AJAX

The process I followed was creating a rails 3.0 scaffold and exposing it using json, keeping it running. When accessing http://localhost:3001/objects.json I can view json data in the browser Next, I had a simple html file with code.jquery.com/jquery-1.7 ...

Navigate through the list of options by scrolling and selecting each one until the desired element is

Hey, I've got this AngularJs directive that selects an item based on the "key-pressed" event, and it's working perfectly. The issue arises when there is a scroll bar since the elements get hidden, making it difficult for me to see them. You can ...

Disappear the popup box when the document is clicked anywhere

My goal is to create a component that displays a list of items. When I click on an item, it should show an edit popup. Clicking on the item again should hide the edit popup. Additionally, I want to be able to click anywhere on the document to hide all edit ...

Tips on accessing internal functions within a single module.exports

I'm trying to integrate the getCursor function into the getOffsetCustom function in my code. While I have both functions exported, I can't seem to nest getCursor inside getOffsetCustom successfully. This file is being used for running node witho ...

Launching a Node.js script with pm2 and implementing a delay

Utilizing the amazing pm2 package to maintain the longevity of my node.js applications has been extremely helpful. However, I have encountered a problem that I am unsure how to resolve. One of my applications involves multiple scripts, a server, and sever ...

Displaying a specific item in react js with the help of bootstrap version 5.0

I am currently working with Bootstrap v5.0 and facing an issue with displaying the selected item from a drop-down menu. Ideally, when I click on "Action," it should show "Action" instead of the default "Dropdown Link" text. Can anyone provide guidance on ...

Utilize Angular 2 to upload and access files directly on the client side

How can I obtain log file(s) from a user so that I can analyze them? Is there a way to create a drop area where the user can upload a file, and then I can read it using JavaScript? I am currently using Angular2 rc5 and have node.js running on the backend, ...

using JavaScript to send numerous text box values to various views

I have a dilemma with passing values between two views. In View1, there are text boxes for entering basic information. After the customer enters this data and clicks on 'add more details', I want to transfer these details to the text boxes in Vie ...