Vue 2 draggable does not maintain reactivity when the v-model value consists of a parent tag's iterable

Utilizing Vue 2 alongside Vuex, the incoming object gets organized into distinct sub-objects according to the classCategory value. Could it be failing because the v-model value in draggable is a key sourced from the parent tag object?

<div class="class-draggable__group" v-for="group in groupedClasses" :key="group.categoryLocalised">
                    <label class="class-draggable__label__category">{{ group.categoryLocalised }}</label>
                    <draggable v-model="group.classes"
                        :options="{ group: 'classes', dragClass: 'drag', ghostClass: 'ghost' }">
                        <div v-for="classItem in group.classes" :key="classItem.class" class="class-draggable__draggable__item">
                            <div class="d-flex align-items-center">
                                <svg-icon name="drag" class="mr-8 move item"></svg-icon>
                                <div>{{ classItem.classLocalised }}</div>
                            </div>
                            <div class="class-info d-flex align-items-center">
                                <checkbox class="class-draggable__draggable__item__checkbox" :view="1"
                                    v-model="classItem.enabled" @select="toggleClassEnabledValue(classItem.class, $event)" />
                                <div class="icon-wrapper">
                                    <svg-icon v-if="classItem.boolVal1Enabled" name="boolVal1Icon"></svg-icon>
                                </div>
                                <div class="icon-wrapper">
                                    <svg-icon v-if="classItem.boolVal2Enabled" name="boolVal2Icon"></svg-icon>
                                </div>
                                <btn :content="{ icon: 'edit' }" class="class-draggable__draggable__item__button mr-8"
                                    round="circle" @click="editClassHandler(classItem.class)" />
                            </div>
                        </div>
                    </draggable>
                </div>
groupedClasses() {
            const groups = {};
            this.zoneInfoClasses.forEach(classItem => {
                if (!groups[classItem.categoryLocalised]) {
                    groups[classItem.categoryLocalised] = [];
                }
                groups[classItem.categoryLocalised].push(classItem);
            });
            return Object.keys(groups).map(categoryLocalised => ({
                categoryLocalised,
                classes: groups[categoryLocalised],
            }));
        },

This is the zoneInfoClasses object:

[
    {
        class: "classa"
        classLocalised: "classA"
        categoryLocalised: "category1"
        enabled: true
        boolVal1Enabled: false
        boolVal2Enabled: false
    }
    {
        class: "classb"
        classLocalised: "classB"
        categoryLocalised: "category2"
        enabled: false
        boolVal1Enabled: false
        boolVal2Enabled: false
    }
    {
        class: "classc"
        classLocalised: "classC"
        categoryLocalised: "category1"
        enabled: false
        boolVal1Enabled: false
        boolVal2Enabled: false
    }
    {
        class: "classd"
        classLocalised: "classD"
        categoryLocalised: "category2"
        enabled: false
        boolVal1Enabled: false
        boolVal2Enabled: false
    }
]

The drag feature works perfectly visually. However, upon releasing the dragged item, all items revert back to their initial positions.

Answer №1

After some investigation, I devised a solution. By updating the new positioning within groupedClasses while maintaining the original positions, I was able to revert back to the initial state and finally commit the changes to the store value used to reconstruct groupedClasses itself.

To implement this, I included the following code in the draggable opening tag:

@change="handleDragEnd($event, group)"

and added the following method:

handleDragEnd(event, group) {
            this.groupedClasses.forEach(oldGroup => {
                if (oldGroup.categoryLocalised === group.categoryLocalised){
                    oldGroup.classes = [...group.classes]
                }
            });

            const ungroupedClasses = [];
            
            this.groupedClasses.forEach(group => {
                group.classes.forEach(classItem => {
                    ungroupedClasses.push(classItem);
                });
            });
            this.setZoneInfoValue({ prop: 'classes', value: ungroupedClasses });
        },
    

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

Creating a responsive design for mobile apps in Ionic using CSS

I need help with making my Ionic app responsive across all mobile devices. The design on the page was created using CSS, but it is not displaying properly on every device. How can I ensure that it adapts to different screen sizes? <template> <Io ...

Utilizing HTML data retrieved from an ajax request

I am utilizing a load more button to display content fetched from a database via ajax. Here is how the ajax call is structured: // JavaScript Document // Function to load more builds $(document).ready(function(){ var pageIndex = 1; $('#loadmorebuild ...

How can I automatically update the content of a specific Div element when the page is loading using the Ajax load() function

This is the HTML code I have: <body onload="fun1()"> <ul id="nav" class="nav" style="font-size:12px;"> <li><a href="#" id="m_blink" onclick="fun1()">Tab1</a></li> <li><a href="#" id="d_blink" onclick="f ...

What causes jQuery results to resemble a function?

When I create a jQuery wrapped set and insert it into console.log, the output appears as follows: I am aware that we can manipulate the console to display objects or arrays. For example, when we have: var obj = { 0: 'some', 1: 'dat ...

Encountering the issue of "TypeError: Cannot read property 'file' of undefined" when trying to upload a video with Multer

The objective is to select a video file from the desktop and upload it to the platform. The uploaded video file will then be automatically posted to a specified Youtube channel using GCD. An error message I encountered is:"react-dom.development.js:40 ...

What could be causing my Leaflet popup to suddenly close?

My feature allows users to search for a marker and zoom to its location, triggering the popup to open. Everything is functioning correctly, except that the popup closes after the function executes. I am struggling to pinpoint what is causing the popup to c ...

The characteristics of angular js Service/Factory and how they influence functionality

I'm seeking clarification on the behavior of AngularJS, especially in terms of JavaScript. In my code, I have a controller that has an Angular factory injected into it. Here's a snippet from the controller: $scope.localObjCollection = myObjFac ...

Troubleshooting a React Node.js Issue Related to API Integration

Recently, I started working on NodeJs and managed to create multiple APIs for my application. Everything was running smoothly until I encountered a strange issue - a new API that I added in the same file as the others is being called twice when accessed fr ...

Angular version 4 is used to retrieve deeply nested JSON data

How do I extract data from a nested JSON file? Here is an example of the JSON structure: { "user1": { "name": "john", "surname": "johnsson" }, "user2": { "name": "Jacob", "surname": "Jacobsson" } } I want t ...

A guide on adding a personal library to Ember using npm

Despite the abundance of blog posts discussing it, I am still facing challenges in utilizing a custom library as a dependency for my ember application through npm. I have developed a WebGL library and successfully imported it into my Ember app by installi ...

Protecting your CakePHP 3 application with the Security component and enhancing form fields with VueJS

Currently, I am developing a software application that utilizes Vue.js and CakePHP 3.6. Whenever I try to submit data using the POST method, the security component triggers a 400 error due to missing _Token fields. While CSRF token implementation is worki ...

Use joi to validate the entire request object

In order to ensure the validity of request input before calling the controller logic, I developed a middleware for that purpose. Let's suppose we have a route for "getting user by id". const usersController = require('../controllers/users.js&ap ...

Issue with VueJS beforeRouteEnter hook not triggering

I have been following the steps outlined on this page: https://github.com/vuejs/vue-class-component#adding-custom-hooks Although no errors are appearing, the beforeRouteEnter hook is not triggering. I am not getting any of the expected console output. In ...

Vue Router is failing to match a URL that contains numerous dynamic parameters

I've been working on adding a nested url to my routes and have encountered an issue with the last route in my code. Every other route seems to be functioning properly. I attempted to nest the urls using the children property, but it wasn't succe ...

Deactivate the underscore and include the fiscal year in AngularJS

I am currently faced with a scenario where the back end is returning the value as follows: 123222_D1.123 However, I need to display the date from the database (12-Jun-2020) as 2020-D1.123 in a drop-down menu. Currently, I am displaying the above value i ...

Controller experiencing issues with Ajax passing null value

My webpage features a dropdown menu with a list of ID's to choose from. When a customer selects an option, it should update the price total displayed on the page. To achieve this functionality, I'm working on implementing an AJAX call that will u ...

Troubleshooting: Unable to preview Facebook Page plugin

When I visit the Facebook developer site to get the code for a Facebook page plugin, I use this link. The preview feature works perfectly with pages like "facebook.com/Nike", but when I try it with my own page, "facebook.com/BargainHideout", it doesn&apos ...

Challenges with browsing navigation in Selenium WebDriver

Recently, I began my journey of learning selenium WebDriver. In an attempt to automate the task of logging into an account using the Firefox browser, I encountered a discrepancy. Manually opening the browser and clicking on the login link from the homepag ...

Encountering an issue with vue-test-utils setup where a TypeError is thrown because a property '_Ctor' cannot be created on a string

Looking for ways to set up jest testing with vue-test-utils and vue 2 on Rails 5.1 with webpacker? I've been following this guide and this guide. Basic tests without vue components are running smoothly, but encountering an error when attempting to mou ...

AngularJS experiencing issues with Bootstrap multiselect functionality

I am currently integrating bootstrap-multiselect into my AngularJS application. To do this, I've included the following scripts in my master page (index.html). <script src="/bower_components/jquery/dist/jquery.min.js"></script> <scrip ...