What is the best way to assign a value to an attribute in my Combobox with Vue.js?

My question pertains to my plugin vue-select@latest. I have been able to successfully retrieve data from the object cities, but I am struggling with how to add a value, such as

<span class="selected-tag" value="Moscow" or value="New York">

image description here

As a newcomer to Vue.js, I am attempting to utilize the plugin vue-select. However, I am encountering difficulties in adding the value attribute. Despite multiple attempts, I have not been successful.

<v-select :options="locations" label="name" v-model="country" value="value" ></v-select>

I am generating a dynamic combobox from the object cities: ['Moscow', 'Sp', 'Tomsk', 'Voronezh', 'Novossibirsk']. My goal is to assign all the data from the combobox to the value attribute, for instance, value="Moscow".

let sample = new Vue({
  el: '#app',
  data: {
    country: null,
    city: null,
    locations: [
      {
        name: 'Russia',
        cities: ['Moscow', 'Sp', 'Tomsk', 'Voronezh', 'Novossibirsk'],
        value: ['Moscow', 'Sp', 'Tomsk', 'Voronezh', 'Novossibirsk']
      },
      {
        name: 'USA',
        cities: ['LA', 'Pensacola', 'New York'],
        value: ['LA', 'Pensacola', 'New York']

      }
    ]
  },

codepen link My cdn library

<script src="https://unpkg.com/vue-select@latest"></script>

Answer №1

First and foremost, ensure that the options property is either a string array or a plain object array containing "label" and "value" properties.

Give this code a try:

<v-select :options="locations.map(item => item.name)" label="name" v-model="country"></v-select>

This will display a select menu with the names of your locations.

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

Retrieve the outcome of an AJAX request in JavaScript within a separate function

My uploadFunction allows me to upload files to my server using a Rest web service called with JQuery ajax. I also use this method for another function and need to determine the result of the web service call in order to add a row with the name of the uploa ...

Filtering out Objection/Knex query results based on withGraphFetched results

I am working with two models in Objection - "brands" and "offers". Brand: const { Model } = require('objection') class Brand extends Model { static get tableName() { return 'brands' } ...

Navigate to a specific line in Vscode once a new document is opened

Currently, I am working on a project to create a VS Code extension that will allow me to navigate to a specific file:num. However, I have encountered a roadblock when it comes to moving the cursor to a particular line after opening the file. I could use so ...

Ways to access information and functions from another component

Creating a timer dashboard where alarms can change the background color of the timer. Looking to display the timer on a different page with the set background color from the dashboard, but struggling to pass data between components successfully. http ...

Iterate through an array and append individual elements to a fresh array - ensuring only a single item is present in the new

I have been working on a project where I need to call a backend API and retrieve a JSON response. I have come across various solutions, but none seem to address my specific problem. The JSON data returned from the API is structured like this: [ { ...

What are the steps to resolve the npm error message "npm ERR! code ELIFECYCLE"?

Whenever I attempt to launch npm run serve for my vue live server, an error occurs. Can anyone explain why this is happening? error code ELIFECYCLE error errno 1 error <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a3bea7ab ...

What is the best way to incorporate a listener into an Angular directive?

I have a custom directive that generates a dynamic Google chart. My goal is to activate an event handler on the controller's scope whenever the directive detects an event from the chart. For example: http://plnkr.co/edit/yn4KuQfrYvlQNbPSWk3Q?p=previe ...

Which behaviors that are typically exhibited by browsers will be stopped by calling `event.preventDefault()`?

While I grasp that using event.preventDefault() stops default actions triggered by events in the browser, I find this explanation too general. For instance, what exactly are these default event behaviors in the browser? It's common to see developers u ...

React Hamburger Menu not working as intended

I am facing an issue with my responsive hamburger menu. It is not functioning correctly when clicked on. The menu should display the navigation links and switch icons upon clicking, but it does neither. When I remove the line "{open && NavLink ...

Is there a way to verify the results of a Python script within a PHP webpage?

For my school project, I am creating a PHP website where I want to implement a Python code Quiz. I envision a scenario where users can input Python code in an on-page editor/IDE and the output is checked automatically using PHP If-function to determine cor ...

Hiding a specific tag with vanilla JavaScript based on its content

I am facing a challenge with my code that is supposed to hide div elements containing a specific word along with additional text. I have tried multiple solutions but none seem to work effectively. Any assistance on how to hide divs properly will be greatl ...

Verify the functionality of the input fields by examining all 6 of them

I'm facing a challenge with a validation function in my project that involves 6 input fields each with different answers. The inputs are labeled as input1 to input6 and I need to verify the answers which are: 2, 3, 2, 2, 4, and 4 respectively. For e ...

What will the relationships be like between the models using Sequelize?

Hello there, I'm seeking assistance in establishing the correct associations between models using Sequelize. In my project, I have three types of products, each with unique attributes as well as shared attributes. ...

Tips for moving text within a Canvas using a button to trigger the function that displays the text in a Javascript environment

Within my HTML, there is a canvas element with the id="myCanvas". When a button is clicked, it triggers this particular function: function writeCanvas(){ var can = document.getElementById("myCanvas"); var ctx = can.getContext("2d"); va ...

Node.js express version 4.13.3 is experiencing an issue where the serveStatic method is not properly serving mp3 or

I am currently utilizing Express 4.13.3 along with the serve-static npm module to serve static assets successfully, except for files with mp3 or ogg extensions. Despite reviewing the documentation, I have not come across any information indicating that thi ...

Nodemon failing to trigger auto-refresh

My journey with learning node.js using nodemon has hit a roadblock. Despite following tutorials and installing everything exactly as shown, I'm facing an issue. Whenever I enter 'nodemon index.js' in the terminal, it hosts properly but any c ...

Setting a background image as a variable in Vue.js

I am currently working on a vue.js component that includes a div.icon: Vue.component('obj', { props: ['img', 'name'], template: '<div><div class="icon"></div> {{ name }}</div>' }) While ...

Creating a TypeScript generic type for the "pick" function to define the types of values in the resulting object

I am facing an issue while writing the type for the pick function. Everything works smoothly when picking only one key or multiple keys with values of the same type. However, if I attempt to pick a few keys and their values are of different types, I encoun ...

Looking for a way to choose an Object using the key value in VueJS?

In my Vuejs setup, there is an object containing various fields including an email value which looks like this: "[email protected]". { "fields": [ { "label": "email", "value": "<a href="/cdn-cgi/l/email-protection ...

Alternative to Webpack for modifying global variables in preloaded modules

Is there a way to shim moment.js in webpack similar to how it is done in browserify? I have developed a widget using npm and webpack, which will be included in another application. The other app already has moment.js loaded via a script tag. I want to av ...