Creating a Vue select list by populating it with an array of options and automatically selecting one based on an asynchronous response

I've been experimenting with Vue to create a select list using a predefined array of options. However, when a specific async response or event contains an assignee, I set that as the 'currentAssignee' which is then preselected in the list.

While this approach works to some extent, the select box appears empty or invisible initially. Only upon clicking on it, the options 'Name One', 'Name Two', and the name from the response (like 'John Doe') become visible. However, this does not truly satisfy the 'selected' option requirement since it remains hidden until clicked by the user upon page load.

Is there a better way to handle this situation?

<select class="firstLastNames linkBox" v-model="currentAssignee" @change="changeAssignee()" >
    <option :selected="true">{{currentAssigneeFirst}} {{currentAssigneeLast}}</option>
    <option v-for="assignee in assigneeOptions" >{{assignee.email}}</option>
</select>

data () {
  return {
    currentAssignee: '',
    assigneeOptions: [
          {id: 0, email: "Name one"},
          {id: 1, email: "Name two"}
    ],
  },
}

/**further down, I set currentAssignee based on async event**/
this.currentAssignee = triggerEvent[0].assignee;

Answer №1

Check out the code snippet I've put together to address your problem: https://codepen.io/timfranklin/pen/bGWYggG

Pay attention to what is bound by the v-model. The select's "value" should not be the object itself, but a value within the object.

    <select class="firstLastNames linkBox"  v-model="currentAssignee" @change="changeAssignee($event)" >
        <option disabled >Choose One</option>
        <option v-for="assignee in assigneeOptions" :key="assignee.id" :value="assignee.id">{{assignee.email}}</option>
    </select>

Note the significance of :value="assignee.id" here;

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

Add a jQuery script to the admin panel of a custom WordPress plugin for sending emails through ajax

I've been working on integrating a form into an admin page on WordPress. The goal is to allow users to input their email address and trigger an email to be sent to that address. To achieve this, I'm utilizing a jQuery Ajax function to transmit th ...

Using JQuery to retrieve part of a className results in a null value being returned

I am facing an issue with a piece of code in codesandbox where it returns null when I attempt to retrieve part of the className from a div using JQuery. How can I troubleshoot this and make it work? Check out the Codesandbox Example import React, { Compo ...

Configuring a JavaScript calendar with custom margins

Having trouble selecting a date with the Bootstrap datepicker class. After running the snippet, the calendar appears below the textbox: <input type="text" class="form-control datepicker" name="due_date" id="due_date" onclick="calendar_up()" value="" pl ...

Sophisticated Sidebar Design using Bootstrap 5

Looking to design a responsive sidebar for navigation on my page, with selected and hover properties. Visualizing the selected option to appear like this: Selected (active) option in sidebar The goal is to have the image cover the left side of the backgr ...

How to keep the show/hide effect active in jQuery even when the user presses the backspace

I recently came across this code snippet on a fiddle: https://jsfiddle.net/htz8x1no/ that I'm working with. Here's the code in question: $('.relevant-results').hide(); $('#input').keyup(function() { if ($ ...

Utilizing an EJS variable within a React render function with webpack - A Guide

I am looking for a way to display personalized information stored in a MySQL database to my users. I am using ejs, webpack, and React for my project. While I found some guidance on how to achieve this without webpack, it doesn't quite fit my needs. Th ...

JavaScript-enabled tests encountering issues (Bootstrap 3, Rails 4, Travis CI)

I'm encountering a strange error that only shows up in my CI environment. This error doesn't occur in development, production, or local test environments. ActionController::RoutingError: No route matches [GET] "/fonts/bootstrap/glyphicons-halfli ...

Creating a dynamic JSON structure from an array list of elements: A step-by-step guide

I am faced with the task of transforming an array of employee IDs, listed as follows: empIds: [38670, 38671, 38672, 38673], into a JSON payload structured like this: { "members": [ { "EmployeeId": &quo ...

Order JSON Array Using LoDash Library

Recently I encountered a JSON array with the following structure: var json = [ { key: 'firstName', value: 'Bill' }, { key: 'lastName', value: 'Mans' }, { key: 'phone', value: '123.456.7890&apo ...

Ensure that autocorrect is set to suggest the nearest possible quantity form in WordPress

I'm currently in the process of creating a webshop using WooCommerce. Our quantity system is a bit unique, as we are using WooCommerce advanced quantity, which means that quantities increase by 0.72 increments (e.g. 0.72, 1.44, 2.16 etc). The +/- butt ...

What is the best way to find the index of the smallest value in an array using JavaScript?

I recently created this function that currently outputs -1. function sayHello() { let buildingLevelsArray = [11,10,10]; var smallestIndex = buildingLevelsArray.indexOf(Math.max(buildingLevelsArray)); console.log(smallestIndex); } sayHello() ...

Guide on running the JavaScript class constructor independently

I have encountered a challenging question that I have struggled to find an answer for, even after researching online resources. My query is regarding executing the constructor function of a class (or object) independently without creating a new instance. ...

confirming the phone field's character count

My website has multiple phone fields, each formatted like this: $("#HomePhoneInput").inputmask({ "mask": "(999) 999-9999" }); $("#WorkPhoneInput").inputmask({ "mask": "(999) 999-9999" }); $("#CellPhoneInput").inputmask({ "mask": "(999) 999-9999" ...

What methods can I use to prompt my JavaScript code to generate a third response?

Currently studying JavaScript and I came up with a basic script to inquire about the number of hours you've put in at work. Following that, it will indicate whether you've reached the required threshold, along with displaying the previously enter ...

Exploring a JSON API structure with the help of jQuery

I am currently exploring the world of JSON API and attempting to extract some data. As a beginner, I find myself struggling to identify the exact value to use. Here is an example of the JSON API I am dealing with: [ {"lang":"english","visual":"<span ...

The most efficient method for handling a vast amount of data in NodeJS

My database consists of 4 million numbers and I need to quickly check if a specific number exists in it. Example of the database: [177,219,245,309,348,436,...] I initially tried using a MySQL table for this task, but it took a lengthy 1300ms just to chec ...

Stopping the use of <script> tags within HTML form submissions

After launching my website at , I discovered that a user is attempting to redirect it to different websites using HTML form input tags. While I'm unsure of the exact method they are employing, I am seeking a way to block users from entering these nefa ...

Unable to assign a value to an indexed property in 'FileList': Setter function for index properties is not enabled

angular.module('myApp') .directive('fileModel', ['$parse', 'uploadService','messageService','CONF', function ($parse, uploadService,messageService,CONF) { return { restrict ...

Retrieve a single document using Angularfire2 without setting up a listener

I'm facing an issue with angularfire2 v6 and angular 11. Specifically, I am attempting to retrieve a single document from the users collection based on their email without utilizing valueChanges() or snapshotChanges(). Realtime updates are not necessa ...

Ensure that each component in Angular2 includes a separate JavaScript file

Every time I include a JavaScript file in my index.html and use a function, it only works the first time. When I refresh the page, it stops working. I am looking for a solution to include a JavaScript file for each component to prevent this issue from oc ...