Vue select is causing the selected choice of another selector to be influenced

Currently, I am facing an issue with a table displaying a specific Nova resource. The problem lies in the selectors within each row of the table - when one selector is changed, all other selectors automatically mirror that change. This behavior seems to be caused by having the same v-model binding for all selectors. However, I am unsure of how to resolve this.

Below is the code for the table and the problematic selector:

<table id="favorites">
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Source</th>
                <th>Description</th>
                <th>Date of Creation</th>
                <th>Posted Status</th>
                <th>Actions</th>
            </tr>
    
            <tr v-for="(favorite, index) in favorites" :key="index">
                <td>{{favorite.id}}</td>
                <td>{{favorite.title}}</td>
                <td>{{favorite.source}}</td>
                <td>{{favorite.description}}</td>
                <td>{{favorite.created_at}}</td>
                <td>
                    <div v-if="favorite.posted_status === 2">
                        <button class="button-posted button4">Posted</button>
                    </div>
                    <div v-else>
                        <button class="button-not-posted button4">Not Posted</button>
                    </div>
                </td>
                <td>
                    <select @change="toggle_posted(favorite)" v-model="selected_state" class="form-control form-control-lg">
                        <option selected disabled>Choose an action </option>    
                        <option v-for="(state, index) in posted_states" :key="index" :value="state.id">{{state.name}}</option>
                    </select>
                </td>
            </tr>
        </table>

I aim to rectify this by separating the selectors so they no longer sync with each other. Additionally, it's important to note that while the other selectors reflect the changes made, they do not trigger the toggle_posted method. Only the initially chosen selector does.

Answer №1

To address this issue, a simple solution would involve introducing a new attribute called selected_state for each item in the favorites list:

new Vue({
  el:"#app",
  data: () => ({
    favorites: [
      { id:1, title:'1', source:'1', description:'1', created_at:'1', posted_status: 1, selected_state:null },
      { id:2, title:'2', source:'2', description:'2', created_at:'2', posted_status: 2, selected_state:null }
    ],
    selected_state: null,
    posted_states: [
      { id:1, name:'1' }, { id:2, name:'2' }
    ]
  }),
  methods: {
    toggle_posted(favorite) {
      console.log(favorite)
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <table id="favorites">
    <tr>
      <th>ID</th>
      <th>Title</th>
      <th>Source</th>
      <th>Description</th>
      <th>Date of Creation</th>
      <th>Posted Status</th>
      <th>Actions</th>
    </tr>
    <tr v-for="(favorite, index) in favorites" :key="index">
      <td>{{favorite.id}}</td>
      <td>{{favorite.title}}</td>
      <td>{{favorite.source}}</td>
      <td>{{favorite.description}}</td>
      <td>{{favorite.created_at}}</td>
      <td>
        <div v-if="favorite.posted_status === 2">
          <button class="button-posted button4">Posted</button>
        </div>
        <div v-else>
          <button class="button-not-posted button4">Not Posted</button>
        </div>
      </td>
      <td>
        <select @change="toggle_posted(favorite)" v-model="favorite.selected_state" class="form-control form-control-lg">
          <option selected disabled>Choose an action </option>    
          <option v-for="(state, index) in posted_states" :key="index" :value="state.id">{{state.name}}</option>
        </select>
      </td>
    </tr>
  </table>
</div>

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

What is the best way to handle waiting for an API call in JavaScript export?

In my Vue app, I've set up my firestore app initialization code as shown below: if (firebase.apps.length) { firebase.app() } else { firebase.initializeApp(config) } export const Firestore = firebase.firestore() export const Auth = firebase.auth() ...

Avoid having Vue CLI delete all files in the dist folder

As I work on my Vue project, I am facing a challenge in syncing the dist folder with Git. Previously, this process ran smoothly when using webpack. However, after transitioning to @vue/cli and creating my project with vue create myProject instead of vue in ...

The functionality of jQuery date picker and time picker is compromised when the fields are generated dynamically

I am currently utilizing the jQuery code below to dynamically create multiple input fields, which include time pickers and date pickers. However, I am encountering an issue where they are not functioning as expected. $('#add_another_event').clic ...

Enhancing SEO Performance with React Server Components

Exploring the new topic of React Server Components, which has recently been released, how does it impact SEO compared to SSR/Next.js? Unlike Next.js and traditional SSR where components are rendered statically on the server, React Server Components are re ...

`Can controllers be included in route or template definitions?`

When it comes to defining a route with a template, there are two main ways to set the controller for the view: In the route: $routeProvider .when('/phone/:phoneId', { controller: 'PhoneDetailController', templateUrl: &ap ...

Creating methods that are shared, privileged, and publicly accessible: A guide

Currently, some methods in one of my classes are public but can access private variables due to being privileged. This is because they are generated in the class constructor, allowing their closure to have access to the object closure. However, I am conce ...

Unable to display Bootstrap 5 modal

I am currently in the process of constructing a basic webpage that includes a navigation bar and a table. The table rows are being dynamically generated through a simple JavaScript script that fetches data asynchronously from a database. I opted to utilize ...

Difficulty in adding a simple return to render an array in React for list creation

After establishing a basic object, I noticed that it contained an internal object named "orders" with an array of toppings like "Cheese" and "Bacon". To further explore this structure, I segregated the array and directed it to a function called renderToppi ...

Move to the following array when clicking PHP (using Ajax, perhaps?)

How can I trigger php code when a button is clicked? (I understand the distinction between client-side and server-side. However, I believe I have encountered a similar scenario before.) Consider the following array : $test_id = [ 1 => '15 ...

Is it possible to use the .focus() event on an entire form in JQuery? If so, how

Take a look at this HTML snippet: <form ....> <textarea>....</textarea <input .... /> </form> I'm trying to set up a help section that appears when the user focuses on any form element, and disappears when they lose ...

Having trouble reaching external methods in Vue with Echarts OnClick Methods?

Hey there! I've been working on integrating Echarts into a Vue application, and I've encountered a bit of a roadblock. Specifically, I'm trying to capture the item clicked on one of the charts and then pass that data back to the parent compo ...

interactive symbols in a reorganizable tree structure

I have been developing a customizable tree list for our users to arrange their website content. It allows them to add and rearrange pages by dragging and dropping. Each list item includes the page name and three icons (lock, visible, and edit). The challe ...

What are some ways to provide the find() method in JavaScript with a specific search argument?

I've been exploring ways to search within an array while iterating through it. One method I stumbled upon is the find() method. Take a look at this example: var inventory = [ {name: 'apples', quantity: 2}, {name: 'bananas&apos ...

Using Vue.js and the fetch() API to update a Directus collection

I've been attempting to modify a row in a Directus collection. Although I reviewed the documentation on this subject, I'm still uncertain about how to accomplish this using the fetch() function within the <script> section of my Vue.js code. ...

Struggling with retrieving the $id variable from the view in both the controller and the database through Ajax

While checking my view, I noticed that the variable $id is visible. However, when I send it through Ajax, it ends up as NULL in the database. The way I'm sending the variable $id from the view using Ajax is like this: $.ajax({ url:'{ ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...

Protecting an API with passport-local authentication

Let me get right to the point. I've developed a secure application using passport-local, with all routes well-covered. The purpose of my app is to retrieve data from MongoDB and serve it as an API that feeds into d3 charts. While all my webpages are s ...

Generating a JavaScript object from a string to optimize its compatibility with datatables

This inquiry pertains to the plugin available at: var hidecols = '{"sClass": "Hide", "aTargets": [0]},{"sClass": "asdf", "aTargets": [1]},{"sClass": "qwer", "aTargets": [2]}'; var hidecolsobj = eval('(' + hidecols + ')'); ...

Unable to access socket.io after modifying the application's URL

There's been a lot of discussion surrounding this topic, but most of it doesn't apply to my situation since I am using express 4.16.4 and socket.io 2.2.0. Also, my example is already functional on both localhost and remote hosting. When setting ...

Are NPM and Eslint supposed to be this confusing, or am I missing something?

Recently, I've started delving into the world of JS and have been eager to learn more about linting. Following a tutorial, we set up the lint stage in our package.json file. The configuration looks like this: "lint": "./node_modules/.bin/eslint ." U ...