distinctive information bound to a v-model in Vue

Within my template, I have a select field:

li(v-for="user in users", :id="'user_' + user.id")
  .user-management(@click="toggleShowUser(user)", :id="'user' + user.id")
    select(v-model="selected" :options="options")

In my Vue component:

export default {
  data() {
    return {
      options: [
        {
         value: 'option1',
         label: 'Option 1',
         iconLeft: 'user'
        },
      ],
      selected: ['option1']
    },
    toggleShowUser(user) {
      this.$set(this.selected, 0, "5ad0ac33a8abbc3fe4ce2863")
    }
  }
}

The issue is that the this.selected value is being set in all user select fields within the template.

I am looking to set this value specifically in a user's select field with a specific id.

Appreciate any help on this!

Answer №1

You have the ability to easily pass the index to the toggle function by modifying your code:

li(v-for="person in people", :id="'person_' + person.id")
  .person-management(@click="toggleShowPerson(person))", :id="'person' + person.id")
    select(v-model="selected.includes(person.id)" :options="choices")

Don't forget to update the function code accordingly:

export default {
  data() {
    return {
      choices: [],
      selected: []
    },
    toggleShowPerson(person) {
      if (this.selected.includes(person.id)) {
        // Remove
        this.selected = this.selected.filter(id => id !== person.id);
      } else {
        // Insert
        this.selected.push(person.id);
      }
    }
  }
}

Answer №2

If you're looking to locate the chosen value for each instance, I recommend establishing a new attribute during every loop iteration. You have two potential approaches at your disposal -

li(v-for="user in users", :id="'user_' + user.id")
  .user-management(@click="toggleShowUser(user)", :id="'user' + user.id")
    select(v-model="selected[user.id]" :options="options")

    export default {
      data() {
        return {
          options: [],
          selected: []
        },
        toggleShowUser(user) {
          this.$set(this.selected[user.id], "5ad0ac33a8abbc3fe4ce2863")
        }
      }
    }

Alternatively,

li(v-for="user in users", :id="'user_' + user.id")
  .user-management(@click="toggleShowUser(user)", :id="'user' + user.id")
    select(v-model="user.selected" :options="options")


export default {
  data() {
    return {
      options: [],
    },
    toggleShowUser(user) {
      this.$set(user.selected, "5ad0ac33a8abbc3fe4ce2863")
    }
  }
}

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

The menu remains open at all times

Currently, I am developing a web-accessible menu that needs to comply with 508 standards. However, I encountered an issue where the menu does not close when I reach the last item using the TAB key on the keyboard. Additionally, I am looking for a solution ...

Issue: Unspecified error when trying to access object attribute in Vue (Nuxt)

I am facing an issue with my 'region' object. It appears to be displayed correctly with all its attributes, but when I try to access specific attributes, it shows up as 'undefined'. const region = { "id": 7, "name": ...

The x-axis is represented by JSON keys, while the y-axis is represented by

I am currently attempting to replicate a graph that was originally made in Excel. Here is the code I have written so far: var data = [ { 'FB':4, 'Mv':4, 'CB':5, 'SL':3, ...

Error Encountered When Running JavaScript Code

Running the code on my Localhost:3000 is resulting in an error message: Unhandled Runtime Error TypeError: Cannot read properties of undefined (reading 'id') The specific section of the code causing this error is highlighted in the Source part: ...

Monitor $localStorage for any modifications

Utilizing the ngStorage module, which can be accessed at this link: https://github.com/gsklee/ngStorage In order to maintain a lastSeenId for a real-time social feed in my hybrid app (Laravel/AngularJS), I am storing it in localStorage instead of $scope. ...

How can mongoose be used to modify user information in a database?

Hey there, I'm currently working on a personal project and came across an issue. I've successfully set up my (steam) user data to save in mongodb through mongoose. Right now, I have it set to only save a new user if they are not already in the da ...

The object fails to initialize using data retrieved from the JSONP API

As a newcomer to the world of jQuery and Javascript, I am currently working on creating an object that will store information obtained from an API. The objective is to have this object hold basic info for each user in the users array. My approach involves ...

Using axios with async/await to handle unresolved promises in Javascript

I'm facing a challenge with a piece of code I've been working on. Despite my efforts to find a solution online, I haven't had any success so far. Here is the code snippet in question: const fetchSidebarData = async () => { let da ...

Retrieve the value of a JSON string

I'm having trouble accessing a json value in this example. No alert is being produced. Can you help me figure out what the problem is? This is the JSON data I am working with: { "response": [ { "id": "0", "elementName": "osname", "isEqual": t ...

How to Use JQuery to Disable Controls or Background on an ASPX Page

I have implemented a code to display a dialog box for confirming a delete action. The code works fine, but the issue is that when the dialog box pops up, it doesn't disable any controls on the ASP.Net page. This allows me to interact with other contro ...

Vue shallow mounting not effective, issues with stubs

I am currently working on writing a Jest test for a Vue component that includes rendering a subcomponent. Here is the structure of the component: <template> <div class="add-to-cart-position"> <a :href="item.url"> <picture- ...

Transition smoothly between HTML pages by using a script to fade in and out while changing images from an array

I am facing a small issue. I have successfully implemented a fadeIn and fadeOut effect on an entire page by clicking HTML links. However, I now want to dynamically change the images associated with these links using an array. Here is the code for transiti ...

How to rotate a SVG transformation matrix around its center point

CSS .square { background-color: green; height: 40px; width: 40px; } JS var square = { sizeReal : { "width": 40, "height": 40 } , position : { "x": 100, "y": 100 } }; $(". ...

Tips for collapsing a child accordion when the parent accordion is collapsed?

Here is the code I have for a functional parent/child accordion div setup: <div class="accordion"> <h3>Part 1</h3> <div class="accordion"> <h3>Sub-Div1</h3> <div> <p>This ...

Calculator built with HTML, CSS, and JavaScript

Hi there, I'm experiencing some issues with my calculator. The buttons seem to be working fine and lining up correctly, but for some reason, nothing is showing up on the monitor or getting calculated when I press the buttons. Here's the code that ...

Exploring the integration of dynamic URLs in Nuxt.js using DRF

After receiving guidance on integrating information from DRF into Nuxt.js, I find myself eager to learn more. I followed the basic instructions but still struggle to grasp the concept. Can anyone offer a solution to this issue? I am particularly intereste ...

I have a string in base64 that contains = symbols. What is the best way to remove them from the

What is the best method for removing "=" characters when concatenating base64 strings? I transmitted a byte stream[] of data from the servlet as an http response, and on the client side I am attempting to open the pdf viewer. However, I am unable to view ...

Tips for positioning and resizing images within a changing DIV dimension

My DIV element is adjusting its size based on the browser window. Inside this DIV, there is an image that should fill up the entire browser window without stretching out of proportion when the window dimensions change. In addition, I want the image to be ...

Tips for creating a cookie for an alternate website upon launching a new browser tab

Hey there, I'm facing an issue that I could really use some help with. To give you some context, I'm working on a project using Angular and TypeScript. My goal is to implement single sign-on functionality for multiple websites within one applica ...

Update the image path in Vue depending on the size of the screen

I'm looking to dynamically change the image src based on screen size in my Nuxt project. The current code I have is as follows: <div v-for="(item, index) in aside" :key="index""> <img :src="item.svgIconDark.fi ...