``There seems to be an issue with VueJs v-for loop not updating

Having an issue with a form where buttons to add children inputs stop working after adding one child. There seems to be an error in the console, but when checking the data output it appears the new input box is being added. The DOM just isn't updating with the new input box.

var default_item = {
  value: 0,
  reps: 0,
  sets: 0
}

var deafult_exercise = {
    exercise_name: '',
    item_data: [default_item]
};

new Vue({
  el: '#app',
  data: {
    log_data: [
        {
        log_name: 'log #1',
        log_day: 1,
        exercise_data: [
            {
            exercise_name: 'exercise #1',
            item_data: [
                {
                value: 50,
                reps: 5,
                sets: 5
              }
            ]
          }
        ]
      }
    ]
  },
  methods: {
    addLog: function(){
      this.log_data.push({log_name: '', log_day:0, exercise_data: deafult_exercise});
    },
    addExercise: function(index_id) {
    this.log_data[index_id].exercise_data.push(deafult_exercise);
    },
    addItem: function(index_id, log_id) {
    this.log_data[log_id].exercise_data[index_id].item_data.push(default_item);
    }
  }
})

JSfiddle: https://jsfiddle.net/renlok/5mpace1q/3/

Answer №1

It appears that your problem lies in repeatedly pushing and referencing the same items, resulting in only one item being added.

The solution is to utilize:

Object.assign({}, default_item)

This will create new instances of the items. Take a look at the updated example here: https://jsfiddle.net/5mpace1q/4/

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

Use Javascript or Jquery to dynamically change the background color of cells in HTML tables based on their numerical

I am working with a collection of HTML tables that contain numbers presented in a specific style: <table border="1"> <tr> <th>Day</th> <th>Time</th> <th>A</th> <th>B</th> &l ...

Important notice: Warning for stand-alone React 18 page regarding the import of createRoot from "react-dom"

I am in the process of developing a standalone webpage utilizing React 18: <!DOCTYPE html> <html lang="en"> <head> <title>Hello React!</title> <script crossorigin src="https://unpkg.com/react@1 ...

Input field with read-only property enabled

In my scenario, I want the input field fines to become read-only when the value of the status field is set to Paid. How can I make this happen? Here's the form structure: Take a look at the code snippet below: <div class="form-group" ...

Tips on increasing the height of an element that is overflowing

When populating my timeline component with dynamically mapped data from an array, I encountered an issue where if I added more data causing the maximum height to be reached, the overflow-y element didn't display all content. Despite trying various sol ...

Dynamic Character Measurement

I am currently utilizing Datatables to dynamically add rows to a table with 3 columns: Index Text CharCount I am seeking logic to implement a character count for each entry in the 'Text' column and display it in the corresponding 'CharCou ...

What is the reason behind receiving the error "PHP Warning: Trying to access property "nodeType" on null" when using this PHP AJAX search function?

I am working on developing a real-time search feature inspired by an example from w3schools, which can be found at this link: https://www.w3schools.com/php/php_ajax_livesearch.asp. My task involves searching through an xml file containing 1000 different it ...

Having difficulties obtaining sorted results for an e-commerce website API using Node.js

I have implemented logic to sort products based on query parameters sent by the user const getAllProduct = asynchandler( async ( req, res)=>{ try { // filter the products search const queryObj ={...req.query}; const excludef ...

Toggle the visibility of the search Div using Angular UI

When using angular UI buttons, users can select search criteria by choosing between Patient, ID, or Date. If Patient or ID is selected, the searchByText div will be shown. If Date is selected, the searchByText will be hidden and the SearchBydateRange wil ...

Android Chrome users experiencing sidebar disappearance after first toggle

Over the past few days, I've dedicated my time to developing a new project. I've been focusing on creating the HTML, CSS, and Java layout to ensure everything works seamlessly. After completing most of the work, the design looks great on desktop ...

Press the option "Switch to red button" in order to transform the text color to red, and then revert back to its initial color

I am trying to implement a feature where the "convert to red button" changes the text to red in the preview paragraph, and then reverts back to the primary color according to the theme - black in the light theme, white in the blue and black background them ...

What could be preventing the fill color of my SVG from changing when I hover over it?

I am currently utilizing VueJS to design an interactive map showcasing Japan. The SVG I am using is sourced from Wikipedia. My template structure is illustrated below (The crucial classes here are the prefecture and region classes): <div> <svg ...

Error in NodeJS: 'Cannot alter headers once they have been sent.'

My project involves developing an app with Express that fetches tweets from Twitter. Each API call retrieves 200 tweets (or fewer if there are less than 200) and in total, I need to retrieve 1800 tweets. To achieve this, I use a time interval to make multi ...

Interactive links Navbar

My current project involves creating a dynamic Navbar that fetches data from mongodb. Here is the structure I am working with: # src * [components/] * [Navbar.js]] * [Layout.js]] * [pages/] * [index.js] * [_app.js] Unfortunately, it seems like I ...

How can you use JavaScript/jQuery to scroll to the top of a DIV?

When a link is clicked, I have a <div> popup that appears. This popup contains scrollable content when there is overflow. If the same link is clicked again, I want the <div> to be positioned at the top of the scrollable area. While this functi ...

I am struggling to make custom output for jQueryUI Autocomplete menu items function as intended

I am currently facing an issue with passing values from an object retrieved from a JSON string into a jQueryUI Autocomplete element. Specifically, I need to extract three different values from a 2-dimensional array format like this: [{1:a1, 2:b1,3:c1,4:d1 ...

Numpad functionality in JQuery malfunctioning post-ajax request

Using the jQuery numpad plugin has been flawless until after an AJAX call. I have tried various functions like on('click') and others, but unfortunately, none of them worked as expected. Apologies for my poor English! You can find the extension l ...

Navigate a user upon completion of an update

My webpage requires users to click an update link, which triggers a process of fetching data from an API that takes a few minutes. I don't want users to have to constantly refresh the page to know when the process is complete and they can continue. Is ...

How can I efficiently store and access DOM elements in my React application to avoid constant re-fetching?

Is there a known pattern for storing references to both regular DOM elements and jQuery elements within a class? Despite the general advice against using jQuery with React, I needed the jQuery functions for my menu to function properly and did not have ti ...

How can you create a sticky navigation bar specifically designed for horizontal scrolling on a website?

I am facing a challenge with a large table that extends beyond the screen, requiring both horizontal and vertical scrolling. My goal is to create a sticky navbar that remains at the top when I scroll horizontally, but hides when I scroll vertically, only t ...

Refresh the three.js Raycaster when the window size changes

Currently, I am implementing a ray caster to choose objects within my three.js scene. The specific objects I am working with are box geometries shaped like flooring. After a successful selection of an object, I encountered an issue where resizing the wind ...