Using v-model and multiple select with an array of objects

Currently, I am utilizing Vue.js 2.0 in combination with the Element UI library.

My focus is on a multiple select component. The v-model attribute allows for pre-selection of options. For instance, if you have model: ['Option4'], Option4 will be preselected in the select field.

I am interested in being able to use v-model with an array of objects rather than just an array containing option labels.

Instead of using model: ['Option4'], I would like to use something like

model: [{name:'Option4'}, {name:'Option5'}]
.

However, this approach does not properly handle pre-selecting the options.

Is it feasible to achieve this functionality? If so, how can it be accomplished?

http://jsfiddle.net/3ra1jscx/

<div id="app">
<template>
  <el-select v-model="model" multiple placeholder="Select">
    <el-option v-for="item in options" :label="item.label" :value="item.value">
    </el-option>
  </el-select>
</template>
</div>

var Main = {
    data() {
      return {
        options: [{
          value: 1,
          label: 'Option1'
        }, {
          value: 2,
          label: 'Option2'
        }, {
          value: 3,
          label: 'Option3'
        }, {
          value: 4,
          label: 'Option4'
        }, {
          value: 5,
          label: 'Option5'
        }],
        model: ['Option4']
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

Answer №1

Remember to include the value of the object inside the options array.

var Menu = {
  items() {
    return {
      options: [{
        value: 1,
        label: 'OptionA'
      }, {
        value: 2,
        label: 'OptionB'
      }, {
        value: 3,
        label: 'OptionC'
      }, {
        value: 4,
        label: 'OptionD'
      }, {
        value: 5,
        label: 'OptionE'
      }],
      selection: [3]
    }
  }
}

Check out this working demo.

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

How can I style and manipulate an array in Vue.js to only show the active element?

I have an array of cards with buttons on each card. When I click a button, all buttons change their style. How can I apply the style to only the active button and not all buttons? <template> <div> <div v-for="i in cards" id= ...

Developing pledges in AngularJS

I am currently working on implementing a promise in Angular using the $q service to retrieve an object from a web service. The unique aspect of this implementation is that if the object is already cached, it should be returned without making a call to the ...

Implementation of Repetition Function

When working with the TypeScript example above, I encountered 2 errors. The first error was related to `i` in the second console.log due to the use of the 'let' keyword. The second error was regarding 'test' in the first line, stating i ...

Implement a validation function in JavaScript that compares two dates to ensure they are valid

In my search for the distinction between two dates, I have encountered a scenario where, upon a user's request to modify the arrival date, the system must retain the number of days between the arrival and departure dates before the alteration. Subsequ ...

Div element to animate and vanish in a flash

I'm attempting to create a smooth slide effect for a div element before it disappears. Below is the code I am currently using: function slideLeft(element) { $("#" + element).animate({ left: "510" }, { duration: 750 }); document.getEle ...

Toggle the sliding menu drawer option (upward/downward motion)

When it comes to my simple menu, I'm using jQuery to toggle the visibility of a few DIVs. The code is straightforward as shown below, and I could really use some assistance with adding extra functionalities. <div id="one" class="navLinks"> cont ...

Initiate an "ENTER" command through programming

Is it possible to automatically trigger the "Enter" keypress after a value is added to an input text box? If so, I am in need of assistance with my current code, as it does not seem to be working correctly. Here is the code snippet: $('#clo_cart&ap ...

Using AngularJS to showcase an organized $http reply in a segment of a template

I have a current project where I am looking to organize responses by category and subcategory using Angular. My controller code currently appears as follows: function($http, $stateParams) { let vm = this; $http({ method: ...

Using Next.js to pass fetched data as props to components

I am currently working on integrating a leaflet map into my Next.js project. The map display is already functioning with predefined latitude and longitude in the component. However, I now need to show data retrieved from my API as the longitude and latitu ...

I am encountering a situation in which the controller file is returning empty data when logging the contents of req

User Model const mongoose = require("mongoose"); const userSchema = mongoose.Schema( { name: { type: String, required: true }, email: { type: String, unique: true, required: true }, password: { type: String, required: true }, pic: { ...

What could be causing the form body to return null in a PUT request?

What could be causing the form data to not be stored in req.body? EJS/HTML <form onsubmit="EditJob()" class="editForm"> <div class="form-group-edit"> <label for="position">Position</label> <input type="pos ...

Utilize the Discord SDK to broadcast a message to every channel within a server whenever a command is utilized

In the process of development, I have crafted a new command: ! sir snd all -hello Essentially, this particular command instructs to dispatch the message specified after " - " to every channel located on the server where the command is initiated. Here&apo ...

How do you populate a dropdownlistfor in ASP.NET MVC after a form

My issue is that <form> @Html.DropDownListFor(x => x.City, provinces, "--Select City--", new { @class = "dropdownList" }) @Html.DropDownListFor(x => x.district, Enumerable.Empty<SelectListItem>(), "--Select district--") < ...

Using percentages to position Div elements

Currently, I am working on an HTML page that requires a div element spanning the full width of the page. My goal is to arrange other divs within this full-width div using percentages rather than pixels. The class associated with this div is .question. Thi ...

How can Prisma be used to create a model for a database table with a hyphen in its name?

One of the tables in my database has a hyphen in its name, for example, "user-cars". Unfortunately, I am unable to change the name to "user_cars". Is there any way for me to name the model as "user_cars" while still making it reference the "user-cars" ta ...

Tips for creating an array in a <script> tag within an hbs view template

Currently, I am delving into the world of full stack web development with a focus on Node/Express. The project at hand is to create a voting app as part of a challenge from FreeCodeCamp, which you can find here. To display user votes in pie charts on the f ...

What is the best way to refresh the slick jQuery plugin for sliders and carousels?

I am currently facing an issue with two buttons that have the same function. The purpose of these buttons is to retrieve data from an API, convert it to HTML, and then append it to a <div> using jQuery. Finally, the data is displayed using the slick ...

Validation of forms using ajax and javascript

I have implemented ajax on my webpage to dynamically change the content of a main div when different fields in the navigation bar are clicked. However, I am encountering a problem with form validation using JavaScript on elements loaded via ajax. For insta ...

Preventing page reload by using event.preventDefault() does not work with Ajax Form

I've been working on a code snippet to submit a form using Ajax without any page reloads: $( document ).on('submit', '.login_form', function( event ){ event.preventDefault(); var $this = $(this); $.ajax({ data: ...

Leveraging router-link within a singular File Component

I am currently facing an issue with routing in my single file component. I am attempting to include another component within a single file component named thirdtemplate.vue. In order to do this, I used Vue.component("newTemplate",{template:"..."}) as dem ...