Choosing a default selection in a nested v-for loop for a select box

Currently, I have a list of items that users can add new items to. Each item is required to have a select box, and the selected value from the select box should be assigned as the item's value.

In an attempt to bind the select box to the item using the v-for key directive, I implemented the following code:

<div id="app">

{{message}}
<span v-on:click="addNewItem">Add New item</span>
<section v-for="(item, key) in items">
{{item.val}}
  <select v-modal="items[key].val">
    <option v-for="default in defaults.selectBox">{{default}}</option>
  </select>
</section>
</div>

JavaScript:

new Vue({
el:'#app',
data: {
message: 'testing',
        defaults:{ selectBox : ['VUE','REACT','ANGULAR']},//some default vaues
    items: [{val:'VUE'},{val: 'REACT'}] //intial two items

   },
  methods:{
  addNewItem:function(){ //add new dynamic items
  this.items.push({val:''});
  }
  }
});

Despite my efforts, the select box is not even displaying.

I am at a loss for what mistake I may be making here. How can I achieve the desired outcome (where the selected value is assigned to the item val)?

Visit this link for reference

Answer №1

Altering the initial variable in setting's v-for to something different resolves the problem

default is considered a reserved keyword in JavaScript and VueJS prohibits its use as a property name

new Vue({
  el:'#app',
  data: {
  message: 'testing',
defaults: { 
    selectBox : ['VUE','REACT','ANGULAR'] //some default vaues
    },
    items: [{val:'VUE'},{val: 'REACT'}] // intial two items
  },
  methods: {
    addNewItem: function() { // add new dynamic items
    this.items.push({val:'TEST '+this.items.length});
  }
  }
});
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c4c7d7f2809c879c8381">[email protected]</a>/dist/vue.js"></script>
<div id="app">
{{message}}
<span v-on:click="addNewItem">Add New item</span>
<section v-for="(item, key) in items">
{{item.val}}
  <select>
    <option v-for="defaultx in defaults.selectBox">{{ defaultx }}</option>
  </select>
</section>
</div>

Answer №2

It seems there was a small error in the code. Instead of v-modal, it should actually be v-model

You can check out the correct version in this JSFiddle link

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

Maintaining state value during client-side navigation in NextJs with Next-Redux-Wrapper

Currently, I am working on resolving the hydration issue that occurs when using wrapper.getServerSideProps. The problem arises when I reroute with the existing setup and the store gets cleared out before adding new data. This leads to a blank page as essen ...

Is it possible to terminate an active server process triggered by an HTTP post request in Node.js prior to returning a response?

I developed an application where I utilized Ajax to make calls to a Node server. The issue is that even if the user navigates to another page, the server persists in processing the initial request made by the Ajax call. It then proceeds to process the new ...

Listening to messages in a Vue.js template

Here is the template named "template-main": ... <template> ... <slot></slot> ... </template> ... Next, there is a simple page that utilizes this template: <template> <template-main> . ...

Energetic flair for Vue animations

I am currently developing a VueJS sidebar component. The objective is to allow the parent to define a width and display a toggle button that smoothly slides the sidebar in and out. Here is an example: <template> <div class="sidebarContainer ...

Issues encountered with Nextjs 13.4 and Next-Auth 4.2 regarding the signIn("credentials", { }); functionality not functioning as expected

When using next-auth credentials in my current project, I encountered an issue with the signIn() function from next-auth/react. It appears that the redirection to the admin page persists regardless of whether the login details are correct or not. {error: n ...

Firebase Issue in Next JS Authentication Flow: Unconfirmed Email Causes "auth/email-already-in-use" Error to Trigger

I have encountered a perplexing issue while setting up user registration with Firebase Authentication in my Next.js application. The problem arises when I try to register a new user with an unverified email address. Instead of following the correct flow to ...

Sharing data between child and parent components, and then passing it on to another child component in React Js

I have a scenario where I am passing props from a child component B to parent component A, and then from parent component A to child component C. Everything works fine when I pass the data from component B to A, but I encounter an issue when I try to set a ...

I am unable to locate the type definition file for 'core-js' at the moment

Currently, I am in the process of developing an application using angular2 along with angular-cli. Surprisingly, angular-in-memory-web-api was not included by default. In order to rectify this, I took the initiative to search for it and manually added the ...

Displaying properties in Vue using a bound component

Offspring Vue.component('xms',{ template : `<div> {{failureMsg}} </div>`, props : { failureMsg : '', minimumLength : '', } } ) Forefather html <xms errorMsg="Is ...

An issue has occurred: Cannot access the properties of an undefined object (specifically 'controls')

I encountered an error message stating "TypeError: Cannot read property 'controls' of undefined" in the code that I am not familiar with. My goal is to identify the source of this error. Below is the HTML code snippet from my webpage: <div ...

Utilizing a jQuery variable within an .html() method

Can a Jquery if statement be used to display different content inside a div based on a variable? For example, if the variable is set to "cats", the displayed content might say "I like cats", and if it changes to "dogs", it would read "I like dogs". Is this ...

Automated pagination in Jquery running seamlessly

I have successfully created pagination using jQuery. Although the script is functioning properly, I now want it to automatically switch between different pages: <script> $(document).ready(function(){ $("#article_load_favourites").load("indexer_favo ...

Tips for adjusting the color of a <a> tag upon clicking, which remains unchanged until the URL is modified

My goal is to maintain the color of a link when it is clicked. Currently, hovering over the navbar link changes it to greyish, but I want it to remain a different color after clicking on it. I am implementing this using the react-router-dom library for the ...

Having trouble with the CSS positioning of divs created with JavaScript: it's not behaving as anticipated

Let me start by saying I have always struggled with CSS positioning. It seems like I am missing something simple here... So, I have a JS script that generates divs within a parent container called #container which is set to absolute position. Here is the ...

Using media queries in VueJS style tags may not have the desired effect

I've encountered an issue with using @media in the style tags of a VueJS component. Surprisingly, styling within the @media query works consistently, while applying styles based on width rules does not seem to have any effect. <template> &l ...

React - Render an element to display two neighboring <tr> tags

I'm in the process of creating a table where each row is immediately followed by an "expander" row that is initially hidden. The goal is to have clicking on any row toggle the visibility of the next row. To me, it makes sense to consider these row pa ...

Is there a way for me to create a clickable link from a specific search result retrieved from a MySQL database using an AJAX

Currently, I am attempting to create an ajax dropdown search form that provides suggestions based on results from a MySQL database. The goal is for the user to be able to click on a suggestion and be redirected to the specific product. The code I am using ...

Avoid unnecessary renders by only updating state if it has changed from the previous state

Is there a specific React lifecycle method that can trigger a re-render only when the updated state differs from the previous state? For instance, consider the code snippet below: class App extends Component { constructor() { super(); this.state ...

Challenges related to the placement of the speed dial in Vuetify are causing

I'm trying to incorporate the vuetify speed dial component into my app, but I'm encountering odd positioning issues. What I want is for the button to be clicked and then the additional options slide out to the left of it. The image above shows t ...

Is it possible to generate a "pop-up" window upon clicking on the register button?

As a backend programmer, I'm looking to create a popup window that appears in front of the current window when users click "register", eliminating the need for redirection to another page. I believe you understand the concept. How can I achieve this? ...