The Vue dragula plugin - drop event listener being triggered repeatedly

I am currently working on developing a web application that incorporates draggable elements utilizing the Vue.js framework and vue-dragula plugin. The main goal of the app is to allow users to drag elements across multiple containers.

Within App.vue

<template>
   <div v-for="container in containers">
      <container/>
   </div>
</template>

Inside Container.vue

<template>
   <div v-dragula="elements" bag="first-bag">
      <div v-for="element in elements" :key="element.id">
         <element v-bind="element"/>
      </div>
   </div>
</template>
export default {
   mounted() {
      Vue.$dragula.$service.eventBus.$on('drop', () => {
         console.log('Dropped');
      });
   }
}

I am looking to implement an event listener that can recognize when an element has been dropped. However, the current method for the event listener is triggering multiple times. Each time it is called once per container in the array. For instance, if there are 6 containers, 'Dropped' will be logged 6 times. How can I ensure that the drop event listener is only triggered once?

Answer №1

Here is my approach.

MainComponent.vue

<template>
   <div v-for="(item, index) in items">
      <subcomponent :index="index" />
   </div>
</template>

In SubComponent.vue

<template>
   <div v-draggable="listItems" :bag="'bag-'+index">
      <div v-for="item in listItems" :key="item.id">
         <item-component :data="item"/>
      </div>
   </div>
</template>
<script>
   export default {
      props: ['index'],
      mounted() {
         Vue.$dragula.$service.eventBus.$on('drop', ([bag, currentItem, allItems]) => {
            if (bag == 'bag-'+this.index) {
               console.log('Item Dropped');
            }
         });
      }
   }
<script>

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

Is there a way to prevent SignalR from disconnecting when the settings window is moved?

I am currently developing a webpage that utilizes SignalR events to trigger ajax requests to our servers. These requests return a URL with a custom URI scheme that, when accessed, opens up a specific program on the client's device without leaving the ...

What causes the appearance of echo messages in my JSON response and what steps can I take to stop them from displaying in the browser console?

I have the following PHP code: <?php header("Access-Control-Allow-Origin: *"); include_once('../includes/mysql_connect.php'); include_once('../db/tables/search.php'); // SAVE TO DB $search = new search($mysql); //$search- ...

When pressed, the button changes color to a vibrant shade of blue, and not just a simple outline

I'm experiencing an issue with a button that turns blue when the user holds onto it for a while on mobile. You can see an example in the image below: https://i.sstatic.net/VmnZ8.png Adding ::selected or outline did not resolve the problem as the ent ...

Utilize TypeScript generics in Vue mixins by incorporating them into class components

After transitioning my Vue project to TypeScript, I encountered a situation that requires some management. To handle paginated tables in my application, I developed a Table mixin that manages pagination for my collection of records: @Component export defa ...

Deactivate the ability to print charts exclusively on HighCharts

I am currently working with a DotNetHighchart that has features like Print Chart, Download as PDF, etc. My goal is to remove only the print chart option. In earlier versions of Highcharts, this was easily achieved by using: .SetExporting(new Exporting { ...

Misconception about the usage of jQuery's .each() function clarified with an illustrative example

Problem Description (See Fiddle): When clicking on the red boxes, they transform into kittens and display an alert with the current value of i. Clicking on a large fading kitten will reset everything. I am puzzled as to why alert(i) is triggering multipl ...

Tips for positioning a highcharts pie chart and legend in the middle of a page

I'm struggling to center my highchart data in a node/react single page application. Currently, it appears off-center like this: https://i.stack.imgur.com/ccR6N.png The chart is floating to the left and I would like to have everything centered within ...

What causes the circular progress bar to disappear when hovering over the MUI React table?

My goal was to create a table with a CircularProgressBar that changes its background color from orange to dark blue when hovering over the row. However, despite my efforts, I couldn't get it to work as intended. Additionally, I wanted the progressBar ...

Issue encountered while adding a value from MongoDB to a list

Encountering an issue when attempting to add an element to an array using a for loop MY CODE router.get('/cart', verifyLogin, async (req, res) => { var products = await userHelpers.getCartProducts(req.session.user._id) console.lo ...

Insert a division into the table following every row

I'm working with a table that can be found here: https://codepen.io/anon/pen/bjvwOx Whenever I click on a row (for example, the 1st row 'NODE ID 1'), I want the div with the id #divTemplate to appear below that particular row, just like it d ...

Eliminate any properties with values that exceed the specified number in size

:) I'm trying to create a function that removes properties with values greater than a specified number. I've searched through multiple resources like this question on how to remove properties from a JavaScript object and this one on removing pro ...

The rendering of the list is taking an unexpectedly long time due to random factors

I have encountered a strange issue with one of my components. The component fetches a list of objects from a service in the ngOInit(). The problem I am facing seems to occur randomly, where sometimes it takes a considerable amount of time to display this l ...

Using javascript to eliminate a block of HTML code

In my AngularJS project, I am using owl-carousel and attempting to reinitialize the carousel after a change in data by using $(element).data('owlCarousel').destroy(); while also removing this block: <div class="owl-wrapper"> <div class= ...

Decode a JavaScript string without the need to save it as a file

Imagine a situation where I allow a client to input a JavaScript script that needs to be sent to my server. This script is meant to export an object. Here is the frontend code snippet: <form action="/blabla.js" method="post"> ...

What could be the reason for my failing express test?

I'm in the process of configuring a server using Node/Express and I want to write ES6 code, so I've incorporated babel into my setup. Currently, the server is operational, and I can successfully make the necessary requests. However, I am facing ...

SVG polyline animation that persists seamlessly

In my TypeScript code, I have implemented a function that creates a polyline based on 4 different points (x1y1, xCy1, xCy1, x2y2), where Xc represents the half distance between x1 and x2 on a plane. This polyline does not form a circular shape. private cre ...

Adjusting font size, contrast, brightness, and sound levels on a website with the help of jQuery

My client has a new requirement: adding functionality to increase font size, adjust contrast/brightness, and control sound on his website. When clicking on any of the control images, a slider should appear for the user to make adjustments accordingly. Ho ...

When using classList.replace, it should swap out every instance of the class xxx1yyy with xx

I attempted to swap a class and came across this helpful example here: javascript: replace classList that is inside a conditional Unfortunately, my attempt at modification (shown below) did not work. The use of classList.replace should change all instanc ...

Opt for conventional inputs when incorporating Angular Material checkboxes

Is it possible to use the normal input checkbox in .NET forms, or do I need to create a custom directive or modify the existing one to make it work? <md-checkbox ng-checked="$ctrl.checked = !$ctrl.checked"> <input type="checkbox" name="test" ...

Is there a way for me to come back after all child http requests have finished within a parent http request?

I am currently utilizing an API that provides detailed information on kills in a game. The initial endpoint returns an ID for the kill event, followed by a second endpoint to retrieve the names of both the killer and the killed player. Due to the structur ...