Nested dropdown menus in Vue.js with multiple levels of nesting

I am currently in the process of developing a Vue single page application and have encountered a challenge with a Vue element I am working on.

  var appCataloghi = new Vue({
  el: '#appCataloghi',
  data: {
    cataloghi: oggettoCataloghi.cataloghi,
    viaggi: oggettoViaggi, 
  },
  methods: {
    popolaViaggi: function() {
  
    }
  }

})

The JSON object for 'oggettoViaggi' is structured as follows:

'{"viaggiGuidati":[{"nazione":"Spagna","viaggi":[{"id":"1423","nome":"primoviaggioGSpagna"},{"id":"1424","nome":"secondoviaggioGSpagna"}]},{"nazione":"Giappone","viaggi":[{"id":"1523","nome":"scopertadelGiappone"},{"id":"1524","nome":"Giappone"}]}],"itinerari":[{"nazione":"Cina","viaggi":[{"id":"2423","nome":"primoviaggioCina"},{"id":"2424","nome":"secondoviaggioCina"}]},{"nazione":"Lettonia",&qu...

The JSON object for 'oggettoCataloghi' is structured as follows:

'{"cataloghi":[{"id":"1000","nome":"mete
europee","viaggi":[{"id":"1423","nome":"primoviaggioGSpagna","nazione":"Spagna",
"tipologia" : "Viaggi guidati"},{"id":"1424","nome":"secondoviaggioGSpagna","nazione":"Spagna",
"tipologia" : "Viaggi guidati"}], "descrizione" : "Viaggi per clienti
amanti del giappone"},{"id":"1001","nome":"viaggi
giappone","viaggi":[{"id":"1523","nome":"scopertadelGiappone","nazione":"Giappone",
"tipologia" :
"Itinerari"},{"id":"1524","nome":"Giappone","nazione":"Giappone",
"tipologia" : "Itinerari"}]}]}'

I aim to create a multiple dependent dropdown menu as illustrated in the image below

https://i.sstatic.net/QYiv0.jpg

My current HTML structure is as follows:

<div class="viaggi">
    <div class="row viaggio" v-for="viaggio in catalogo.viaggi"> 
        <div class="col-xs-12 col-sm-3"> 
            <label class="label">Tipologia:</label> 
            <select class="inputCatalogo greyInput tipologiaViaggio" v-model="tipologia">
                <template  v-if="typeof viaggio.id !== 'undefined'">
                    <option selected>{{viaggio.tipologia}}</option>
                </template >
                <template  v-else>
                    <option disabled selected>--Choose typology--</option> 
                    <option value="Guided tours">Guided tours</option> 
                    <option value="Tailored itineraries">Tailored itineraries</option> 
                </template >
            </select> 
        </div> 
        <div class="col-xs-12 col-sm-3"> 
           ...
        </div> 
       ...
    </div>
</div>

In order to determine which "Typology" has been selected for each nested dropdown within my Vue object 'appCataloghi', I believe I need to create a property for each dropdown. However, I am unsure how to approach this.

My line of thinking involves iterating with a v-for like this:

v-for="SecondOptions in list.firstOption"

If there are multiple first options within the same 'appCataloghi' variable, how would I save the first option?

Answer №1

My solution involved utilizing an index to access the array in this manner:

<div class="row viaggio" v-for="(viaggio, index) in catalogo.viaggi" :key=viaggio> 
 <i class="fa fa-times fa-2x eliminaViaggio" @click="eliminaViaggio($event, index, 
 indiceViaggio)" aria-hidden="true"></i> 
 ...
</div>

The corresponding function in my JavaScript file is as follows:

eliminaViaggio: function(event, indiceCatalogo, index) {
     this.cataloghi[index].viaggi.splice(index, 1);
     var clickedElement = event.target.closest('.catalogo');
...

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

Transforming a plain text field into an input field upon clicking a button or icon using Angular/Typescript

I am a beginner with Angular 6 and I'm trying to implement functionality where clicking a button or icon will change a text field into an input field. See the snippet of code and expected output below. Thank you in advance. <div> <mat-for ...

"Viewed By" aspect of Facebook communities

I'm working on implementing a feature that shows when a post has been seen, similar to Facebook groups, using JS and PHP. I've been able to track the number of times a post has been seen through scrolling actions, but now I want to determine if u ...

The chosen options are not appearing. Could this be a problem related to AngularJS?

I am working on setting up a dropdown menu in HTML that includes only the AngularJS tag. This dropdown menu will be used to populate another AngularJS dropdown menu. Below is the code for the first dropdown menu: <select class="form-control" ng-model=" ...

Vue component fails to recognize updates to data made by parent component's mounted() method

Recently, I created a basic application that alters the background color of a box using a setInterval function within the mounted() lifecycle hook of the main instance: What seems to be the problem? If you click on the button labeled "click me," the colo ...

I'm experiencing difficulties with a JavaScript slideshow in Vegas

After installing the vegas jQuery plugin to my project using npm, I encountered issues when attempting to use it according to the documentation. Despite linking the required vegas.min.js and vegas.min.css files in my HTML, the plugin doesn't appear to ...

What is the best way to display only the host and pathname of the links generated by Array.map()?

I am currently utilizing Array.map() to display a list of files in cache: <div data-offline></div> <script> var version = 'v1:'; if (navigator && navigator.serviceWorker) { caches.open(versio ...

The PHP post method is unable to retrieve the value of a button tag

In my code file login.php, I have the script to validate if the user exists or not, as well as an AJAX call. Inside the form, there is a button tag; When I set type='submit', the PHP works but the AJAX does not; and when I use type='button&a ...

Tips for crafting services using $q and $http requests while avoiding redundancy

Is there an elegant way to write AngularJS services without repetitive $q syntax? I currently write services like this: (function() { function ServiceFactory($q, $timeout, $http) { return { getFoo: function() { va ...

Submitting late leads to several consecutive submissions

I am working on a jQuery code that aims to introduce a short time delay to allow for the proper execution of an AJAX request: $('#form_id').submit(function(e) { e.preventDefault(); $submit_url = $(this).data('submitUrl'); ...

Encountered an error while trying to filter markers and list using Knockout and Google Maps - Uncaught TypeError: Unable to locate property 'marker' of undefined

I am looking to enhance user experience by implementing a feature where clicking on a marker or a city name will display the info.window specific to that marker. HTML: <!DOCTYPE html> <html> <head> <title>Exploring Mag ...

Confusion surrounding ESLint error in Vue Cli

I am currently working on a project using Vue Cli that includes a .eslintrc.js file which I need to adhere to. While implementing the code below, an error is being detected. getTags (url) { axios.get('http://example.test/api' + url).then(res ...

Only scroll the div if it is not within the visible window

I've been looking at this snippet: The sidebar division is what I'm focusing on right now. I want the div to scroll along as I scroll down the page, but I need it to stop scrolling when its bottom is in view. The same should apply when scrollin ...

Unit tests are successful, but an error occurs stating "headers cannot be set after they have been sent."

Currently, I am working on writing unit tests for the API endpoints of my very first express app. I am using a data structure as a placeholder for a database and all the tests are passing successfully. However, I encountered an error in the console stating ...

Stop the flow of data in the RxJS stream depending on a specific value within the stream

I developed a straightforward component featuring a single button that initiates and halts a sequence of numbers generated by RxJS timer. import { Component, OnInit } from '@angular/core'; import { BehaviorSubject, Observable, timer, merge } fro ...

Ways to showcase INPUT TYPE when making a Selection?

I've been struggling with a simple issue and despite trying multiple solutions, I can't seem to get it right. I have a form where I'm using the <select> tag with two options: coo and uh. What I want is for an additional input type fiel ...

Merge two arrays of objects containing Google Map coordinates

I am facing a challenge with merging two object arrays that have similar values. var Cars = [{ lat: 42, lng: -72 }, { lat: 40.7127837, lng: -74.0059413 }, { lat: 40.735657, lng: -74.1723667 }]; var Trucks = [{ lat: 43, lng ...

Tips for dynamically passing a URL to a function using the onload event

I require assistance with passing a dynamic URL to a function. code onload="getImage('+ val.logo +');" onclick="loadPageMerchantInfo('+ val.merchant_id +'); The value of val.logo contains a URL such as: https://www.example.com/upload ...

Concern raised about the challenge of removing an element from an array and its potential

When attempting to remove an element from an array without altering the state, I typically use the following code snippet: const tempArray = [ ...originalArray ]; tempArray.splice(index, 1); setOriginalArray(tempArray); After some experimentation, I deci ...

Creating a dynamic drag-and-drop layout in React using react-grid-layout

Utilizing the react-grid-layout package for implementing drag-drop and resize components, I have successfully achieved the functionality outlined in the documentation. Description of My Issue My challenge lies in creating a dynamic UI where the layout is ...

Error: The value of 'v4' property is not defined and cannot be read

I am encountering an issue with Vue.js: vue.esm.js?efeb:628 [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'v4' of undefined" found in ---> <AddTodo> at src/components/AddTodo.vue <App> at src/Ap ...