Troubleshooting why Vue.js isn't updating the DOM with two-way binding

I'm currently utilizing Vue.js for implementing two-way binding.

One issue I am facing is with an edit button that should convert a text field into an input field upon clicking. However, the DOM does not update immediately as expected.

For instance:

When I click on the edit button, there are no visible changes. Refer to the screenshot below:

https://i.stack.imgur.com/ir4oP.png

However, when I remove the other image from the screen, the DOM updates and displays the input field as desired.

https://i.stack.imgur.com/QqhU4.png

I would appreciate any assistance with this problem as I have been struggling with it for quite some time now.

This is my current code snippet:

    <div class="row" id="app">
        <div v-for="image in images" class="col-md-3">
            <div class="well">
                <div class="card">
                    <div class="text-center">
                        <img class="card-img-top"
                             :src="image.data"
                             alt="Card image cap"
                             height="100">
                    </div>
                    <div class="card-block">

                        <template v-if="image.editing">
                            <h4 class="card-title">
                                <input type='text' v-model="image.title" class="form-control input-sm">
                            </h4>
                        </template>

                        <template v-else>
                            <h4 class="card-title">Image title</h4>
                        </template>

                        <p class="card-text">Some quick example text to build on the card title and make up
                            the
                            bulk of the card's content.</p>

                        <template v-if="image.editing">
                            <a @click.prevent="update(image)" href="#" class="btn btn-primary">
                                <i class="fa fa-floppy-o"></i>
                            </a>

                            <a @click.prevent="cancel(image)" class="btn btn-default" href="#">
                                <span class="btn-label-icon left fa fa-ban"></span>
                            </a>
                        </template>

                        <template v-else>
                            <a @click.prevent="edit(image)" href="#" class="btn btn-info"><i
                                        class="fa fa-pencil"></i></a>

                            <a @click.prevent="destroy(image)" href="#" class="btn btn-danger">
                                <i class="fa fa-trash"></i>
                            </a>
                        </template>
                    </div>
                </div>
            </div>
        </div>
    </div>


</div>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94e2e1f1d4a6baa6baa">[email protected]</a>"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            url: '/users/<%= user._id %>/images?token=<%= user.token %>',
            image: {},
            images: []
        },
        mounted: function () {
            this.fetch();
        },
        methods: {
            fetch(){
                axios.get(this.url)
                    .then(function (response) {
                        this.images = response.data;
                    }.bind(this));
            },
            edit(image){
               image.editing = true;
            },
            toggle(){
            },
            destroy(image){
                axios.delete('/users/<%= user._id %>/images/' + image._id + '?token=<%= user.token %>')
                    .then((response) => {
                        this.images.splice(this.images.indexOf(image), 1);
                    }, (response) => {

                    });
            },
            update(image){

            },
            cancel(image){
                image.editing = false;
            }
        }
    })
</script>

Answer â„–1

The issue arose due to the absence of an editing property in the JSON data.

Resolution:

 Upon receiving a response from the axios GET request to the specified URL, the code snippet below will loop through the data and add an 'editing' property set to false for each image:

axios.get(this.url).then((response) => {

    this.images = response.data.map(function (image) {
        image.editing = false;
        return image
    })

}, (response) => {

});

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 disable text selection in AngularJS when double-clicking using ng-dblclick?

My element has ng-dblclick='doSomthing()' functionality that is functioning correctly, however, it also selects the text in the element which is not visually appealing. Is there a way to avoid this issue? ...

Having trouble setting the select value with JavaScript in the Selenium web driver

I am working on a web page that includes a cascaded dropdown feature. The data in the second dropdown appears to be generated via ajax based on the selection made in the first dropdown. Here is the code for the first select option: <select class="form- ...

The pop-up menu appears in a location different from where the anchor element is positioned

Having an issue with the menu placement when clicking on an Avatar. The menu is appearing in the wrong position: The avatar button "OB" on the right side is where the issue occurs. No console errors present and inspecting the Popover element shows that it ...

npm encountered an error or issue during the installation process

I have configured my proxy settings in the .npmrc file, but I am encountering errors when running the npm install command: $ npm install npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program File ...

Node.js refuses to launch - the dreaded error 404, signaling that it has mysteriously vanished

I am brand new to node.js, so please be patient with me as I learn. Currently, I am using the express framework and attempting to create a basic application that displays content as HTML. Below is the essentials of my app.js: var express = require(' ...

Find the mean of three numbers stored in an array of objects

I am currently working on a school assignment that requires me to develop a function for calculating the average mark of 3 students using an existing object in an array. Below, you can see the array and function that I have been working on as part of this ...

The jQuery Deferred feature on Ajax is failing to properly pass the array in the data option as an array, instead converting

I am facing an issue in my application where I want to use jQuery deferred to handle Ajax success and error uniformly from a central location. It works perfectly fine when I pass strings in the data option, but when I try to pass an array, it gets sent as ...

Exploring the capabilities of Redux Toolkit's createEntityAdapter in designing versatile data grids

Seeking guidance on utilizing createEntityAdapter from Redux Toolkit. In my application, I display package information and details using the master/detail feature of the AG Grid library. Packages are loaded initially, followed by fetching detailed data as ...

How can I convert duplicate code into a function in JavaScript?

I have successfully bound values to a view in my code, but I am concerned about the duplicate nested forEach loops that are currently present. I anticipate that Sonarcube will flag this as redundant code. Can anyone advise me on how to refactor this to avo ...

Is it better to store data individually in localStorage or combine it into one big string?

When it comes to keeping track of multiple tallies in localStorage, one question arises: Is it more efficient to store and retrieve several small data points individually or as one larger chunk? For example: localStorage.setItem('id1', tally1); ...

Organize JSON data in Angular 6 from an observable based on a specific key

Note: While I am familiar with sorting a regular array of objects using .sort(), I am facing a challenge with an observable object that I am not accustomed to. My task involves retrieving a JSON array of objects with a service: import { Injectable } from ...

Is it possible to use two onChange functions in a single text field with ReactJS?

Is it possible to have two onChange functions in a single Textfield? In my code, "onChange={ showDiv}" is a handler that shows "Direct 2" when clicked and hides upon selecting option1. The second onChange={(e) => exhandleChange(e)} is used for another ta ...

Waiting for state changes in React by using the UseState Hook

I am currently working on a function that manages video playback when clicked, and I need to set consecutive states using the useState hook. However, I want to ensure that the first state is completed before moving on to the next setState without relying ...

The synergy of Redux with scheduled tasks

In order to demonstrate the scenario, I have implemented a use-case using a </video> tag that triggers an action every ~250ms as the playhead moves. Despite not being well-versed in Flux/Redux, I am encountering some challenges: Is this method cons ...

Incorporating an unique identification code within the input field

I have a HTML code that displays geolocation information: <span id="currentLat"></span>, <span id="currentLon"></span> When combined with JavaScript, it works perfectly. However, I am trying to display the above value in a text fi ...

Transforming the setting into redux using setTimeout

I am currently working with the following context: interface AlertContextProps { show: (message: string, duration: number) => void; } export const AlertContext = createContext<AlertContextProps>({ show: (message: string, duration: number) =&g ...

Disassociate all globally linked modules in npm network

If I want to display a list of all npm modules linked globally using npm link ..., I can execute the following command: npm ls -g --depth=0 --link=true But is there a way to remove or unlink all of these linked modules at once? With over 10 dependencies, ...

What is the best way to showcase a div on top of all other elements in an HTML page?

As a newcomer to html and css, I have successfully created a div that contains city names. The issue I am currently facing is when I click on a button to display the div, it gets hidden behind the next section of my page. Take a look at the image below for ...

"Troubleshooting a JSON structure containing a complex array of objects with multiple layers

I've structured a complex array with JSON objects to allow users to customize background images and create unique characters. Below is the main code snippet: var newHead; var newBody; var newArm; var masterList = [ { {"creatureName":"Snowman ...

I am facing difficulties in installing the necessary node modules for my Angular project

After attempting to run npm install, an error message is displayed towards the end: error syscall unlink 22396 error The operation was rejected by your operating system. 22396 error It's possible that the file was already in use (by a text editor or ...