Using the v-for directive to loop through a list of items and adding a v-autocomplete with

I am facing a challenge with using a dropdown menu within a loop to select the region for each office in my list of offices. The problem lies in passing the index value to the updateRegion method so that I can correctly associate the selected region with the respective office.

Is there an alternative approach to solve this issue? I'm open to suggestions as I currently do not have a solution.

<template v-for="(office, index) in offices">
        <v-flex xs12 class="mt-3" :key="index">
            <v-card class="white--text">
                <v-card-text>
                    <v-layout row wrap>
                        <v-flex xs12 sm4 pr-2>
                            <v-autocomplete
                                :value="office.region.id"
                                :items="regions"
                                :label="Region"
                                @change="updateRegion"
                            ></v-autocomplete>
                        </v-flex>
                    </v-layout>
                </v-card-text>
            </v-card>
        </v-flex>
    </template>

    <script>
        methods: {
            updateRegion(value) {
                // How can I access the index value here?
            }
        }
    </script>
    

Answer №1

Check out the code snippet below:

<template v-for="(office, index) in offices">
    <v-flex xs12 class="mt-3" :key="index">
        <v-card class="white--text">
            <v-card-text>
                <v-layout row wrap>
                    <v-flex xs12 sm4 pr-2>
                        <v-autocomplete
                            v-model="office.region.id"
                            :items="regions"
                            :label="Region"
                            @change="(event) => updateRegion(event, index)"
                        ></v-autocomplete>
                    </v-flex>
                </v-layout>
            </v-card-text>
        </v-card>
    </v-flex>
</template>

<script>
methods: {
    updateRegion(value, index) {
       console.log(value);
       console.log(index); 
        // how can I have here the index value?
    }
}
</script>

Answer №2

If you are looking to retrieve the index of the office and the chosen value from the v-autocomplete within your updateRegion function, you can achieve this by passing the office.regio.id and index as parameters.

I have not had a chance to test it yet. If it does not work as expected, please inform me.

<template v-for="(office, index) in offices">
    <v-flex xs12 class="mt-3" :key="index">
        <v-card class="white--text">
            <v-card-text>
                <v-layout row wrap>
                    <v-flex xs12 sm4 pr-2>
                        <v-autocomplete
                            :value="office.region.id"
                            :items="regions"
                            :label="Region"
                            @change="updateRegion(office.region.id, index)"
                        ></v-autocomplete>
                    </v-flex>
                </v-layout>
            </v-card-text>
        </v-card>
    </v-flex>
</template>

<script>
    methods: {
        updateRegion(value, index) {

            // You can access the index value here.
        }
    }
</script>

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

Steps for launching a hyperlink in a browser on Vue Native

I'm having trouble finding information on how to properly use the Linking component in react-native. The resources available are limited, and I'm not sure if I am implementing it correctly... Below is what I have in my template: <nb-button :o ...

Retrieving ng-model using ng-change in AngularJS

Here is an example of the HTML code I am currently working with: <select ng-model="country" ng-options="c.name for c in countries" ng-change="filterByCountry"></select> This HTML snippet is being populated by the following object containing a ...

How come I am unable to activate the input file using prop?

Attempting to access a selection of files by passing a prop to a component for convenience. Is there a method to achieve this? https://codepen.io/iliyazelenko/pen/RBejRV?editors=1010 The code snippet below does not meet my requirements: <input type ...

Developing client-side components with NextJS

I want to develop a single-page landing page where users can upload videos and there's a file size limit check before the upload. In my src/app/page.tsx file, I have the following code: import React from 'react'; import FileUpload from &apo ...

Send a request for a multidimensional array to a PHP file using AJAX

I am in need of an array containing subarrays. Within my ProcessWire page, there are pages and subpages with various fields. I have organized this information into an array of arrays as shown below. <?php $allarticles = $pages->find("template=a ...

Retrieving the value from a concealed checkbox

I have been searching everywhere, but I can't seem to find a solution to this particular issue. There is a hidden checkbox in my field that serves as an identifier for the type of item added dynamically. Here's how I've set it up: <inpu ...

Is the auto-import feature in the new VSCODE 1.18 compatible with nodelibs installed via npm?

Testing out the latest auto-import feature using a JS file in a basic project. After npm installing mongoose and saving an empty JS file for editing, I anticipate that typing const Schema = mongoose. will trigger an intellisense menu with mongoose nodelib ...

Steps to obtain an Element binding from a Svelte component

Is it possible to retrieve the bounding client rect of an anchor element? I typically mark this using the syntax bind:this={someVariable}. However, when I add this code to a Svelte component, I receive a different object that Svelte uses to represent a c ...

Deciphering the Results of AngularJS

What sets 'template' apart from 'templateUrl' in AngularJS directive? index.html: <!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta charset="utf-8"> <title>Index</title> </he ...

Using JQuery to Refresh a Div and Trigger an API Call with a Click Event

Currently, I am working on developing a web application that generates random quotes dynamically. Using JQuery, I can successfully make an API call and retrieve JSON data to display a quote. To allow users to fetch new quotes with the click of a button, I ...

When delving into an object to filter it in Angular 11, results may vary as sometimes it functions correctly while other times

Currently, I am working on implementing a friend logic within my codebase. For instance, two users should be able to become friends with each other. User 1 sends a friend request to User 2 and once accepted, User 2 is notified that someone has added them a ...

When using sequential jQuery 'pages', an error referencing the third frame occurs

I am new to using javascript/jquery and have been experimenting with the w3schools tutorials and jquery documentation. I created a page where user input is accepted and javascript prints output based on that input. I tried modifying it to work sequentially ...

An Ajax GET request will not be able to locate the JSON file

I am having issues retrieving Key and Values from a JSON file using the function provided. Despite placing the 'datafile.json' in the same directory, the code fails to execute the alert(weblink) function, while the alert('test 1') works ...

Excess space noted at the bottom of tablet and mobile versions of the page

I'm struggling to troubleshoot an issue with the mobile and tablet versions of a web-shop I'm designing for university. Some pages have excessive space after the HTML tag, but only on certain pages. I've tried commenting out CSS lines relate ...

A step-by-step guide on upgrading or transferring from Vite 2 to Vite 3 using NPM while harnessing the power of Vue

While it may seem like a straightforward question, I found myself unable to locate the specific process or commands for upgrading from Vite 2 to Vite 3 using npm. Despite reading the announcement and the migration guide (along with other resources), I ha ...

Sorting columns containing English, Japanese entries, and null values using a customized sorting algorithm in MUI Data Grid is a straightforward process

I am working with an MUI data grid and I am looking to implement a custom sorting algorithm for columns that can override the default options provided by MUI. The data in my fields is in English, Japanese, as well as empty/null values. My desired output sh ...

avoiding the duplication of effects on an object when new objects are added via ajax

Currently, I am facing a minor issue with an application that I am working on. The problem arises on a particular page where the user can edit a document by dragging modules onto the canvas area. Upon loading the page, JavaScript causes the modules to be ...

Vue JS console displays an error stating "the <li> tag is missing a closing tag."

Below is the code snippet I am currently using to change the color based on the character's name: I encountered an error indicating that the li tag was not properly closed, although it appears to be closed. However, there seems to be an issue during ...

Incorporating graphics into a React component

Currently exploring React JS and looking to dive into the practical side of things. Following a documentation tutorial that constructs a basic comment system. I've replicated the component structure outlined in the tutorial: PostBox PostList Pos ...

Tips for incorporating <ios> or <android> components into your Nativescript Vue project

Can someone help me with this issue? <android> <NavigationButton text="Go Back" android.systemIcon="ic_menu_more" @tap="$refs.drawer.nativeView.showDrawer()"/> </android> ...