Why is the second row of inputs not displaying in Vue.js?

I am currently developing a Single Page Application (SPA) that includes options with variants, similar to the image shown below: [1]: https://i.sstatic.net/82I5U.png

However, I have encountered an issue where the second row of inputs for variants is not being displayed.

Here is a snippet of my code:

<template>
    <h4>OPTIONS</h4>
    <hr>
    <div class="row g-3 align-items-center">
        <div v-for="(option, index) in options" :key="index">
            <div class="">
                <label class="col-form-label">Option {{index+1}}</label>
            </div>
            <div class="mb-2">
                <input v-model="option.name" name="option[]" type="text" class="form-control" >
            </div>
            <label class="col-form-label">Variants</label>
            <div class="d-flex">
                <div v-for="(variant,vIndex) in options[index].variants" :key="vIndex">
                    <input v-model="variant.name" type="text" class="form-control">
                </div>
                <button @click="addVariant(index)" type="button" class="btn btn-secondary btn-sm" style="width: 10%">Add</button>
            </div>
            <hr>
        </div>
        <button type="button" class="btn btn-secondary col-4" @click="add()">add option</button>
    </div>
</template>

<script>
export default {
    data(){
        return{
            options: [
                {
                    name: '',
                    variants: [
                        {
                            name: '',
                        }
                    ]
                }
            ],

        }
    },
    methods: {
        add() {
            this.options.push({ name: '' });
        },
        addVariant(index){
            this.options[index].variants.push({name: ''})
            console.log(this.options[index].variants)
        }
    }
}
</script>

If anyone has insights or suggestions on how to resolve this issue, please feel free to share. Thank you for your help!

Answer №1

Make sure to include variants in the options array within the add method, as failing to do so may result in errors when attempting to add variants.

Here is a suggested modification:

add() {
    this.options.push({ name: '',  variants: [] });
},
addVariant(index){
    this.options[index].variants.push({name: ''})
    console.log(this.options[index].variants)
}

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

Execute an AJAX call to remove a comment

Having some trouble deleting a MySQL record using JavaScript. Here is the JavaScript function I am trying to use: function deletePost(id){ if(confirm('Are you sure?')){ $('#comment_'+id).hide(); http.open("get","/i ...

What is the best way to implement conditional styling in Vue.js?

Looking to incorporate a conditional style into my Component. Here is my component: <site-pricing color="primary" currency="$" price="25" to="/purchase" > <template v-slot:title>Complete</templat ...

Encountering a problem while verifying pattern using regular expressions

I'm facing an issue when manually checking if my inputs match the specified patterns. Below is the function I am using for this check: if (!$element.attr("pattern")) return true; let pattern = $element.attr("pattern"); le ...

The build process encountered an error due to the absence of ESLint configuration after the import

Having recently worked on a Vue project created using Vue CLI, I found that eslint was also included in the project. Although I haven't utilized eslint much up to this point, I understand that it is beneficial for catching stylistic errors, semantic e ...

Why does Next.js throw an error when using an additional <div></div>?

I'm new to exploring Next.js and I'm encountering an error when using the html tag twice. It works fine with just one container, but as soon as I add another one, this error pops up: Error: error: Unexpected token "div". Expected jsx i ...

Choosing premier class and modifying characteristics

Looking for a solution to hide a div without modifying the HTML code directly? By using JQuery, you can manipulate the appearance of elements on a page. While it's possible to select the first instance of a class and retrieve its value, adding an attr ...

Combining Material UI with React Form Hook to handle multiple checkboxes with pre-selected defaults

I am working on creating a form with grouped checkboxes using react-form-hook and Material UI. The checkboxes are generated dynamically through an HTTP Request. I am aiming to set the default values by providing an array of object IDs: defaultValues: { ...

Encountering an issue with my node configuration while working on a Discord bot

Attempting to develop my own Discord Bot has presented me with a challenging error that I am struggling to resolve: internal/modules/cjs/loader.js:968 throw err; ^ Error: Cannot find module './commands/${file}' Require stack: - C:\Users&bso ...

Discover a JavaScript table using a for loop

Currently, I'm in the process of setting up a search bar within a table that has been populated by looping data retrieved from an API. My goal is to allow users to search for specific entries either by name or email. Unfortunately, I seem to be encoun ...

Guide to adding content and icons to the top navbar on the right side using vue.js

In the process of developing a fixed top navbar, I encountered an issue while trying to add right side icons and additional content. The goal is to include two icons, as shown in this example: example link, along with profile and cart information on the ri ...

Creating a variable in Node.js to serve as the name of a nested element within an object

Check out the object I have: obj = { "FirstName": "Fawad", "LastName": "Surosh", "Education": {"University": "ABC", "Year": "2012"} } Take a look at my node.js script: var nodeName = 'Education.Year'; obj.nodeName; // ...

The PHP on server could not be loaded by Ajax

Trying to establish a PHP connection, encountering an error and seeking assistance. The error message displayed is as follows: { "readyState": 0, "status": 0, "statusText": "NetworkError: Failed to execute 'send' on 'XMLHttpReq ...

There seems to be a problem with the external JavaScript file not functioning

After dragging and dropping the .js file into my ASP.NET project, I am facing an issue where it remains unresponsive, even though the code works fine when used inline. This problem is occurring while using VS 2017. Here is a snippet of my code: <scrip ...

What sets apart using 'self.fn.apply(self, message)' from 'self.fn(message)', and what are the advantages of using the former method?

Whenever I come across code that looks like thisnewPromise.promiseDispatch.apply(newPromise, message), I can't help but wonder why they didn't simply use newPromise.promiseDispathch(message) ...

Implementing a Beveled Edge on a Shape using ThreeJS

I have put in a lot of effort to find the solution, but unfortunately I have not been successful so far. Currently, I am working on creating a shape using THREE.Shape, and I have the vertices data stored in a file. The shape appears to be straight without ...

Chrome Extension for Extracting Data from Websites

I am in the process of developing my Google Chrome extension that needs to store a variable from another website by passing it over. Below is the snippet of code found in the script.js file of the website: var editorExtensionId = "extension"; & ...

Manipulate properties of objects with React by assigning values from an array

I am working with various objects and values in my code: const [validFilters, setValidFilters] = useState({}); const [endedEvents, setEndedEventsSort] = useState(false); const [joinedEvents, setJoinedEventsSort] = useState(true); const [startDate, setStart ...

Tips for incorporating inline images with gatsby-image

Seeking Solution In my quest to query and showcase images with a maximum width of 350px, I am hoping to have them displayed inline-block for tablets and larger screens. Ideally, each image would sit next to one another and wrap if they exceed the parent d ...

Having an issue with Vue.js and Firebase Storage - encountering an error stating "Uncaught TypeError: storage is not a function"

I'm currently working on a Vuejs & Firebase project where I need to upload images to Firebase storage. Despite following the documentation and tutorials, I keep encountering the same error. Uncaught TypeError: WEBPACK_MODULE_1__firebase_initializer.a ...

Utilize the ng-controller directive with unique aliases across various sections of HTML code

I'm facing an issue with my ng-controllers when multiple controllers are used on the same page. For instance, I have one controller in the page header, another in a different section of the same page, and one in the content of the page. However, all o ...