Modifying the input's value dynamically alters the selection choices in Vuetify

Choose the First option Fpo, when you select the first item, the first object in the list is assigned to the variable test. I have used test.name as a model for that input field. Strangely, when I try to modify the input field, the select option also changes. How does this work? In Angular, I have never encountered this issue before. Is there any way to make sure that changing the input field does not affect the select box option?

new Vue({
  el: '#app',
  data: () => ({
    items: [{name: 'Fpo', value:'foo'},{name:'bar', value:'bar'}],
    test: {}
  }),
  methods: {
    assignValue: function () {
      this.test = this.items[0]
    }
  }
})
<div id="app">
  <v-app id="inspire">
    <v-container fluid grid-list-xl>
      <v-layout wrap align-center>
        <v-flex xs12 sm6 d-flex>
          <v-select
            :items="items"
            label="Standard"
                    item-text="name"
                    item-value="value"
                    @change="assignValue"
          ></v-select>
        </v-flex>
        <v-flex>
                    <v-text-field v-model="test.name">
            </v-text-field>
          </v-flex>
      </v-layout>
    </v-container>
  </v-app>
</div>

Answer №1

If you want to make a deep clone (a completely independent copy) of your object, follow these steps:

this.test = this.items[0];

Instead of simply assigning the value like above, use the following method:

this.test = JSON.parse(JSON.stringify(this.items[0]));

Check out how it's done in the modified pen

Answer №2

This topic pertains to Vue reactivity. You can explore more about it at https://v2.vuejs.org/v2/guide/reactivity.html

There are situations where you may need to assign multiple properties to an existing object using methods like Object.assign() or _.extend(). However, new properties added won't trigger changes. To handle this, create a new object incorporating properties from both the original and mixin objects:

// Instead of `Object.assign(this.someObject, { a: 1, b: 2 })`
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })

When you set values as you did with the equal sign, instead of copying the value, it points to the same location. Therefore, any changes made will affect both variables.

To address this issue, make adjustments in your code like so:

assignValue: function () {
  this.test = Object.assign({name: this.items[0].name})
}

Take a look at the modified codepen via this 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

JavaScript Switch Open/Close Mobile Navigation Menu

I'm currently facing a challenge with a specific issue. I want to implement a toggle menu for mobile devices on a website that I'm developing. It's functioning smoothly, and you can see it in action on this CodePen. The JavaScript code for ...

Enhancing the tooltip inner content in Bootstrap with a description:

Can you help me troubleshoot the code below? I am trying to add a description to numbers in my tooltip, but it doesn't seem to be working. Here is the HTML: <input id="contract-length" class="slider slider-step-2 slider-contract-length" ...

Guide on exporting a dynamically imported class instance using ES6 modules in NodeJS

Currently, I am engrossed in a book on NodeJS that illustrates a simple web application example. In this example, the prerequisite is to have various data store classes housed in their respective modules and dynamically selecting the data store by configur ...

Calculate the total price from a combination of HTML and JavaScript options without altering the values

I'm currently facing an issue in my form where the final price needs to be updated when a different option is selected. I've searched extensively for the problem without success. I've used similar code before, just with different variables a ...

What is the process for creating a folder using Firebase Cloud Functions with Storage?

How do I create a folder named "posts"? Storage bucket path --> gs://app.appspot.com/posts Code to generate thumbnail from storage object exports.generateThumbnail = functions.storage.object() .onChange(event => { const object = event.data ...

Encountering a build error with Ubuntu, Node-Gyp, and Canvas – help needed!

Hey there, I'm having some trouble with installing the Canvas package. I've tried Package Versions 6.1.13 and 6.1.3 Here are my system details: Ubuntu 18.04.5, Node 12.18.4 LTS, Python 2.7, g++ installed, pkg-config installed, libjpeg installed ...

Implement Dynamic Filters in a Vue Component

How can filters be programmatically applied to select values? During each iteration of records and columns, I am checking if the column ID starts with 'd', indicating it's a datetime field. In such cases, I want to apply the humanize filter ...

Retrieve an uninitialized variable in Laravel using axios.post method within the Vue.js module

Hello partners! I'm collaborating with stackOverflow to develop a module using Laravel for the backend and Vue.js for the frontend. Currently, I am facing an issue where the controller is not receiving values from the form when creating a new entity. ...

Implementing Vue syntax highlighting in a JSP file

I've encountered an issue while using Vue in JSP files with Eclipse STS. The JSP Editor doesn't seem to support Vue syntax. Even after trying the Codemix plugin, I wasn't able to resolve the issue. It appears that Codemix only supports vue ...

Adjust the node_modules path tailored to your project

My current project structure looks like this: |-app |-... |-libs |- package.json |- index.html I am interested in organizing my project such that the 'node_modules' folder is inside the 'libs' folder. The desired structure is as fo ...

When a specific button is clicked, the loader of that button should begin loading in Vue.js

When I click on a particular product button in a product component that has "accept" and "reject" buttons, the API response should trigger the loading state for that specific button. However, the issue is that when I click on one product button, all prod ...

Material-UI Scroll Dialog that begins scrolling from the bottom

While attempting to incorporate a scrolling div with the ref tag inside Dialog Material-UI Design, I encountered an error stating Cannot read property 'scrollHeight' of undefined When running my code outside of the Dialog, it functions correctly ...

Error encountered when attempting to upload image on Twitter: missing media parameter

According to the latest Twitter media upload API documentation, it is recommended to first utilize either POST multipart/form-data or base64 encoded files when interacting with . However, encountering an error with code 38 stating "media parameter is mi ...

The unexpected postbacks caused by the ASP.NET MVC DropDownList appear to stem from JavaScript, catching me by surprise

In my MVC project, there's a standard form with a dropdown list. Each time I change the dropdown value, the entire page reloads due to a postback. This behavior is unexpected, and I want to prevent the dropdown from triggering a postback. Strangely, ...

Is there a way to add a fade-in and slide-in effect to this dropdown JavaScript, as well as a fade-out and

Although I lack the necessary knowledge of Javascript, I am aware that my request may be a bit much. The code I currently have is directly from the w3school dropdown-list demo. Would it be possible for you to help me implement a fade in and slide in effect ...

Notify when the button value changes to "submit"

I recently added a jQuery form wizard to my website. I want users to receive an alert in JavaScript when they reach the end of the form. To achieve this, I inserted a simple JavaScript code at the beginning of my page within <head>..some code</hea ...

Performing the AJAX request prior to the document being fully loaded

What I need is for an AJAX call to be made before the document is ready, and then use the response from the AJAX call to update certain HTML elements. This is what I currently have: var ajaxget = $.ajax({ type: 'GET', url:'/xxx ...

Exporting SVG to image in Ionic is successful on Android devices, but the image gets cut off when viewed on

I am facing an issue with exporting an SVG as a base64 encoded image and sending it to the server for storage on Google Cloud Storage. The process works fine on Android and in browsers, but fails when attempted on a physical device running IOS. On IOS, t ...

Executing a group query for Mongoose in a node.js/express route

Hey there, I have a query that works perfectly fine in Robomongo. Here's the code: db.av.group( { key: { roomId: 1}, cond: { dateOfDay: { $gte: new Date('12/01/2014'), $lt: new Date('12/30/2014') } }, reduce: function( curr, re ...

Where can I find the @types for a specific lodash package?

Seeking to utilize a specific function from lodash - assignin. I have successfully installed lodash.assignin and incorporated it into my project: import assignIn = require('lodash.assignin'); However, when compiling, an error occurs: "error TS2 ...