Loop through options in Vue.js and set a specific option as selected

When iterating through a list of objects, I am struggling to set the status of each object as selected by default.

<template>
    <table class="table is-striped">
        <thead>
            <tr>
                <th>
                    Client
                </th>
                <th>
                    Status
                </th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="client in filteredClients">
                <td>
                    <router-link :to="{ name:'client-show' , params:{clientId: client.uuid}}"
                                 v-on:click.native="selectClient(client)">
                        {{ client.name }}
                    </router-link>
                </td>
                <td>
            <div class="select is-rounded">
                <select v-model="selectedStatus" @change="statusChange($event, client)">
                    <option v-for="status in statuses" > {{status}}</option>
                </select>
            </div>
                </td>
            </tr>
        </tbody>
    </table>
</template>
<script>
    export default {
        props: [
            'filteredClients'
        ],
        data: function() {
             return  {
               statuses: [ 'Started', 'Awaiting Payment', 'Paid', 'Onboarding', 'Live']
             }
        },
        computed: {},
        mounted() {},
        methods: {}

    }
</script>

I have all the necessary statuses to display, but setting a default value for each object during iteration is proving to be a challenge.

Answer №1

To ensure that the new status is assigned to a specific client object, you should emit the status change to the parent Component. Avoid mutating props directly in the child component.

    <select :value="client.status" @change="$emit('change', $event.target.value, client.uuid)">
        <option v-for="status in statuses" :selected="status == client.status">{{status}}</option>
    </select>

In your parent Component, capture this emit event and update the client accordingly.

<ParentComponent @change="(status, uuid) => clients.find(c => c.uuid === uuid).status = status">
    // Child component with props: filteredClients
</ParentComponent>

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

Refreshing JWT tokens using Vue resource interceptor

I am currently working on creating a custom vue-resource interceptor that handles the refreshing of JWT access tokens upon expiration. My approach involves implementing a post-request callback to detect token expiration errors (status == 401). The idea is ...

Difficulty loading AJAX with autocomplete feature. Any suggestions?

I have created a jQuery autocomplete feature that works correctly, but when the value is removed using the Backspace key, the 'LOADING...' message remains visible instead of hiding. How can I make it so that after removing the value with the Back ...

"Utilizing Promises in AngularJS Factories for Synchronous API Calls

Attempting to implement synchronous calls using a factory pattern. $scope.doLogin = function (username, password, rememberme) { appKeyService.makeCall().then(function (data) { // data = JSON.stringify(data); debugAlert("logi ...

I'm having trouble getting jquery css to function properly in my situation

I am trying to implement a fallback for the use of calc() in my CSS using jQuery CSS: width: calc(100% - 90px); However, when I tried running the code below, it seems like the second css() function is not executing. I suspect that there might be an issu ...

Is there a way to effectively eliminate an array of objects in JavaScript or TypeScript and alter the object structure simultaneously?

I am seeking solutions to restructure an object that has multiple arrays of objects so that I can access the object directly. console.log(value.data.summary[0].data) Instead of accessing arrays in this manner, I want to modify my data structure. Is there ...

Mastering the art of simultaneously running multiple animations

Utilizing two functions to apply a Fade In/Fade Out effect on an element. Confident in the functionality of both functions, as testing them sequentially in the Console proves their effectiveness. However, executing: fadeIn() fadeOut() seems to result in ...

What is the process for transferring an object to a different file?

Currently, I am working on a web application using Node.js. In my project, I have two main files. The first one is Server.js, where I handle server actions such as going online. The second file contains a large object filled with data. I successfully impo ...

Is there a way to replicate ajaxStart and ajaxStop functions without using jQuery?

After reviewing the extensive jQuery code, I'm wondering if this task would be simple to accomplish. Any suggestions on how to approach it? I'm interested in using this not for a webpage, but for a C# application that needs to monitor ajax activ ...

Having trouble with jest mocking a function - it's not functioning as expected

I decided to create a simple test using jest to simulate a date change function. Here is the code snippet: import React from 'react'; import '@testing-library/jest-dom'; import { render, screen } from '@testing-library/react' ...

Transform a PDF document into a Google Doc utilizing the capabilities of the Google Drive API version 3

I successfully uploaded a PDF file to Google Drive using a node server. However, I am struggling to find a way to convert it to a Google Doc format so that it can be downloaded later as a DOCX document. Here is the code snippet I've been working with ...

What is the best way to protect old documents when selecting new files in a multi-file uploader?

I created a file upload feature with file previews using HTML5 and the file reader, which is functioning well. However, I'm facing an issue where old files selected by the user get removed from the input file field if a new file is selected in a singl ...

Experiencing problems with images in vue-cli? Look no further, as we dive into troubleshooting with

During development, I encountered an issue with my code that uploads images to a server folder and stores references in a mysql database. When transitioning to production, the images became undefined, resulting in 404 errors. I came across the VUE Static ...

Mastering React: Implementing default values in components using Ajax

I am facing an issue while trying to create a form for editing existing values. The problem arises with the initial data retrieved from an ajax request, causing my component to render twice - first with empty values and then again when the data is populate ...

Stopping CSS animations at a specific point along the path without using timing functions can be achieved by implementing a targeted

Is there a way to pause an animation at the 50% mark for 2 seconds and then resume? P.S. The setInterval method is not recommended! function pauseAnimation() { document.getElementById("0").style.animationPlayState = "paused"; }; var pauseInterval = set ...

Exploring new classes with jQuery's .not() and :not()?

I am working on a memory game where I need to flip cards and check if two are equal. My issue is that I do not want the function to run when clicking on a card that is already flipped, or on another flipped card. I tried using jQuery's .not() and :no ...

The Angular route seems to be unresponsive to a single click, but it starts functioning properly when clicked

I am utilizing a data service to handle all my asynchronous data operations. Whenever I click the ERASE button, a function is triggered to erase all data and return an object indicating the operation status (Success: true/false). If the value returned is ...

When deciding between setup() and onMounted() in Vue.js, it is important to consider where to place the code for fetching data from the server to be displayed initially when the page loads

When using the options API, it's clear that we write code for fetching data from the server in the mounted method. However, with the composition API, I find myself a bit confused because the setup method is executed before the onMounted hook. ...

Rails 4 application encountering issues with rendering views when making a $.ajax request

I am a beginner in Rails and I am in the process of sending model data to a controller method from JavaScript for rendering a list of results... function submitResults() { resultURL = "/result"; resultData = JSON.stringify(results); $.ajax({ typ ...

Can I employ a PHP script as a "server" for a React application?

As I am using shared hosting without Node installed, I can't utilize Express as a server (or any other node-related features xD). The issue arises when fetching data from the Behance API in my app and encountering a CORS error. To address this probl ...

Discovering the process to create an onclick function with node.js code

index.html: <html> <h2>Welcome To User Profile Page !!!</h2> <button type="button">Edit Profile</button> <button type="button">Logout</button> </html> On my webpage, I have two buttons - Edit Profile and Lo ...