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

keep an ear out for happenings in dynamic vue modules

What is the method to listen to an event emitted by a dynamically generated component instance? In this scenario, the first component is inserted into the DOM statically, while the second one is created dynamically using JavaScript. Vue.component("butt ...

Executing a secondary API based on the data received from the initial API call

Currently, I am diving into the world of RxJS. In my project, I am dealing with 2 different APIs where I need to fetch data from the first API and then make a call to the second API based on that data. Originally, I implemented this logic using the subscri ...

Steps for regularly executing 'npm run build' on the server

My website doesn't require frequent content updates, as users don't always need the latest information. As a result, most pages are generated server-side and served as static pages. There will be occasional database updates that need to be visib ...

Issue with Django: Unable to fetch data from server response in Ajax

Just starting out with Django and trying to figure out how I can dynamically add content from a python script without reloading the page. In my views.py file, I have two functions - one for uploading a file (home) and another for calling a python script t ...

Adjust the div's height to 0

I need to dynamically set the height of a div element with a height of 34px to 0px. However, this div does not have a specific class or ID, so I can't use traditional DOM manipulation methods like .getElementById(), .getElementByClassName, or .getElem ...

Is it not possible to include Infinity as a number in JSON?

Recently, I encountered a frustrating issue that took an incredibly long time to troubleshoot. Through the REPL (NodeJS), I managed to replicate this problem: > o = {}; {} > JSON.stringify(o) '{}' > o.n = 10 10 > JSON.stringify(o) ...

Executing a Component function within an "inline-template" in VueJS

VueJS version 1.9.0 app.js require('./bootstrap'); window.Vue = require('vue'); Vue.component('mapbox', require('./components/mapbox.js')); const app = new Vue({ el: '#app' }); components/mapbox.js ...

Position the buttons in the react children's application

**Looking for help on positioning Black Widow in the center-left and Hulk at the bottom left of the screen. I'm having trouble figuring it out, does anyone have any tips? Also, I only want to isolate the buttons to each picture. Not sure if I need to ...

JavaScript: Learn how to simultaneously open two links from the current webpage

I am trying to create a browser bookmark that will open a link with the id: window.getElementById('updateOk').click() followed by opening a small window with another button: document.getElementsByClassName('rmit-save-details btn btn-inlin ...

React Material UI Select component is failing to recognize scrolling event

Having some difficulty understanding how to detect a scroll event with a Select component using Material-UI. The Select has MenuProps={...}, and I want to listen for the scroll event inside it. I've tried putting onScroll within MenuProps={...}, but ...

Discover the importance of Node.js integration with HTML

I am trying to display a Node-js value in an HTML file using the pug engine. In my app.js file: const express=require('express'); const app=express(); var bodyParser = require('body-parser'); app.set('views','views&apo ...

Update the image links to automatically refresh every half a second based on the values inputted in the text bars

Is there a better and simpler way to define AAAAAAAAAA, BBBBBBBBBB, and CCCCCCCCCC using the links provided in the text bars named chart1, chart2, and chart3? This learning project is being done through Notepad++ for personal use only, with no need to sa ...

Verify whether the value is considered false, true, or null

When dealing with variables in JavaScript, I often need to determine if a variable is false, true, or null. If the variable is null or undefined, I want to assign an array to it by default. While this syntax works well in other languages, in JS assigning a ...

Updating content in AngularJS based on the sidebar can be achieved by following these steps:

Yesterday, I posed a question about how to implement multiple views with Angular in order to have a header and sidebar. After receiving some helpful insights, I was able to make progress on creating a header and sidebar for my AngularJS App. You can view ...

What could be causing my if statement to fail even though the condition is met?

I'm attempting to generate dynamic fields based on my chosen attributes. I have two array objects called addAttributes and fakeAttributes. The fakeAttributes contain the details of the selected attributes. I have a dropdown select component that displ ...

Troubleshooting TextField malfunctioning after button click within Dialog on Material UI

My main objective is to focus on a Material UI TextField after closing a Dialog by clicking a button inside the Dialog. The code snippet below successfully accomplishes this task when triggered from a button that is not within a dialog component: focusOn ...

Preserve the scroll location while adding new content to a list in AngularJS

I have been attempting to add new items to a list within a scrollable container using ng-repeat, with the most recent item appearing at the top. It is important that I am able to preserve the scroll position if the scrollbar is not at the very top when add ...

displaying and concealing elements with jquery

Is there a way to hide a div if the screen size exceeds 700px, and only show it when the screen size is less than 700px? Below is the jQuery code I'm attempting to use: jQuery(document).ready(function() { if ((screen.width>701)) { $(" ...

Get rid of the unnecessary vertical line that is located at the end of the string

I have come across a situation similar to the one demonstrated in this code snippet: http://plnkr.co/edit/eBjenGqgo3nbnmQ7XxF1?p=preview Currently, I am working with AngularJS 1.5.7. The directives used in my input - ( first ) textarea are the same as tho ...