Tips on how to modify a select option in vue based on the value selected in another select

My Web Api has methods that load the first select and then dynamically load the options for the second select, with a parameter passed from the first selection.

The URL structure for the second select is as follows: http://localhost:58209/api/Tecnico/Tanque/?estacionid=@parameter

I am using Vuetify for the front end, and to load the options for the second v-select, I have implemented the following code:

<v-flex xs12 sm8 md6>
   <v-select autocomplete :items="itemsEstacion" item-text="numeroEstacion" item-value="estacionID" prepend-icon="local_gas_station" :loading="loading" label="Seleccionar Destino" v-model="selectEstacion"></v-select>
</v-flex>

   <v-flex xs12 sm8 md6>
     <v-select autocomplete :items="itemsTanque" v-model="selectTanque" item-text="NumTanque" item-value="ID" v-on:change="cargarTanques(itemsEstacion.estacionID)" prepend-icon="opacity" label="Seleccionar Tanque"></v-select>
   </v-flex>

In my data() return{}, I have defined:

  selectEstacion: "",
  selectTanque: null,
  itemsEstacion: [],
  itemsTanque: [],

These are the methods I have implemented: - For loading the options for the first select

    cargarEstaciones() {
  this.loading = true;
  axios
    .get("GetEstaciones", {
      headers: {
        Authorization: "Bearer " + localStorage.getItem("token")
      }
    })
    .then(response => {
      this.loading = false;
      console.log(response);
      this.itemsEstacion = response.data;
    })
    .catch(error => {
      this.loading = false;
      if (error.response.status != null) {
        switch (error.response.status) {
          case 204:
            this.snackbar = true;
            this.textSnackbar = error.response.data;
            break;
        }
      }
    });
},

-For loading options for the second select:

    cargarTanques(estacionID) {
  axios
    .get("Tecnico/Tanque/?estacionID=" + estacionID, {
      headers: {
        Authorization: "Bearer " + localStorage.getItem("token")
      }
    })
    .then(response => {
      console.log(response);
      this.itemsTanque = response.data;
    })
    .catch(error => {
      if (error.response.status != null) {
        switch (error.response.status) {
          case 404:
            this.snackbar = true;
            this.textSnackbar = error.response.data;
            break;
          case 500:
            this.snackbar = true;
            this.textSnackbar = error.response.data;
        }
      }
    });
},

To handle the event, I have added this under watch:{}

    selectEstacion: function() {
  this.cargarTanques(this.itemsEstacion.estacionID);
}

However, I am facing issues where the parameter is always undefined. Additionally, the v-on:change attribute is not functioning properly so I had to resort to using watch() instead.

Thank you

Answer №1

itemsEstacion is an array, which is why itemsEstacion.estacionID is always undefined.

I have made a change to your code: I moved the v-on:change="cargarTanques" to the first select.

<v-flex xs12 sm8 md6>
    <v-select 
        autocomplete 
        :items="itemsEstacion" 
        item-text="numeroEstacion" 
        item-value="estacionID" 
        prepend-icon="local_gas_station" 
        :loading="loading" 
        label="Seleccionar Destino" 
        v-on:change="cargarTanques"
        v-model="selectEstacion">
    </v-select>
</v-flex>

<v-flex xs12 sm8 md6>
    <v-select 
        autocomplete 
        :items="itemsTanque" 
        v-model="selectTanque" 
        item-text="NumTanque" 
        item-value="ID" 
        prepend-icon="opacity" 
        label="Seleccionar Tanque">
    </v-select>
</v-flex>

cargarTanques() {
  axios
    .get("Tecnico/Tanque/?estacionID=" + this.itemsEstacion.estacionID, {
      headers: {
        Authorization: "Bearer " + localStorage.getItem("token")
      }
    })
    .then(response => {
      console.log(response);
      this.itemsTanque = response.data;
    })
    .catch(error => {
      if (error.response.status != null) {
        switch (error.response.status) {
          case 404:
            this.snackbar = true;
            this.textSnackbar = error.response.data;
            break;
          case 500:
            this.snackbar = true;
            this.textSnackbar = error.response.data;
        }
      }
    });
}

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

Is there a way to adjust the timepicker value directly using the keyboard input?

Is there a way to control the material-ui-time-picker input using the keyboard instead of clicking on the clock? This is my current code: import React, { Component } from "react"; import { TimePicker } from "material-ui-time-picker"; import { Input as Ti ...

Combining and arranging numerous items in a precise location in three.js

I am currently working on a web application using three.js with Angular and facing some challenges when trying to set the precise positions of objects. The requirement is to load various objects and position them in specific locations. In order to load di ...

One effective way to transfer state to a child component using function-based React

My goal is to pass an uploaded file to a child component in React. Both the parent and child components are function-based and utilize TypeScript and Material-UI. In the Parent component: import React from 'react'; import Child from './ ...

Navigating production mode in nuxtjs and uncovering errors

There seem to be numerous inquiries regarding this matter. Unfortunately, there doesn't appear to be a definitive solution. Is there any way to view detailed error logs in production mode? https://i.stack.imgur.com/prXUt.jpg ...

The error message "Type Error: val.toString is not a function - mysql npm" indicates

I'm encountering an issue with the 'mysql' package in NPM on a nodeJS backend and I'm puzzled by this error message: TypeError: val.toString is not a function at Object.escape (/Applications/MAMP/htdocs/nodeJS_livredor/node_modules/sql ...

Is there a way to include values in the body of an HTTP GET request using Angular?

I've created a function in my service that looks like this: /** * Retrieve all data * @param sendSelectedValues string */ getAllActPlanBalanceYearData(sendSelectedValues: any): Observable<any> { const url = `/yearlyvalues/act-and ...

Discover and eliminate the style attribute through a click action

I've been struggling to find a solution to remove the style attribute from a specific tr tag within a table when it is clicked. I've tried several lines of code but none seem to work. Here's the link to the fiddle for reference: http://jsfi ...

Troubleshooting Vue.js dynamic image path loading issues

Struggling to dynamically load images in a single file component, I keep encountering an error stating that the module cannot be found. It seems like I'm attempting something similar to what was discussed in this post on Stack Overflow, but I can&apos ...

Eslint is signaling an error when the keyword "async" is used in the most recent version of Node for Firebase Functions

I'm venturing into using Firebase Functions for my project, but as a newcomer to Javascript, I've been struggling for a week now and hit a roadblock with the following issue. Below is the snippet of my code: exports.postcleanup = onSchedule("eve ...

Achieving the highest ranking for Kendo chart series item labels

Currently, I am working with a Kendo column chart that has multiple series per category. My goal is to position Kendo chart series item labels on top regardless of their value. By default, these labels are placed at the end of each chart item, appearing o ...

There is no defined method "response" in the IlluminateViewView class

I am encountering an issue while attempting to retrieve data from a table. Here is my controller : public function admin() { $users = User::with('subs')->get(); return view('admin')->response()->json([ 'us ...

Troubleshooting Bootstrap Select and dynamic rows problem in a Vue.js project

I am facing an issue with my dynamic table/rows in vue.js where I am using bootstrap-select to enhance the dropdown select. My form includes an add/remove line feature for dynamic functionality. However, I am unable to load the options of a select using bo ...

What happens when an AJAX request doesn't have a success field?

Is it possible to execute an ajax call without specifying a success function? $.ajax({ type: "POST", url: "/project/test/auto", data: data, // no success function defined here }); My reasoning for this is that I have PHP code that insert ...

Utilizing the Loess npm module in conjunction with Angular 4

I am attempting to incorporate the Loess package into my project. The package can be found on NPM and offers various regression models for data fitting. I successfully installed it using npm install loess --save, and it now resides in the node_modules dire ...

Is there a way to include all images from a local/server directory into an array and then utilize that array variable in a different file?

I'm currently using Netbeans version 8.0.1 with PHP version 5.3 Here is a PHP file example: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/199 ...

Cannot find the appended element in an AJAX call using jQuery

Within the code snippet, .moneychoose is described as the div in moneychoose.jsp. Interestingly, $(".moneychoose") cannot be selected within the ajax call. $("input[name='money']").on("click", function() { if ($("#money").find(".moneychoose" ...

Using a variable to store the value of the id attribute in HTML code

How can I dynamically add an ID attribute to an HTML element using a variable declared in JavaScript? Using jQuery var table_row = $('table').find('tr#' + pid); var name = table_row.find('td:nth-child(1)').html(); table_ ...

Updating the style sheet of a selected menu item on an ASP.NET master page

I created an asp.net master page with a menu setup like this: <menu id="menu"> <nav id="main_nav"> <ul id="menu-primary"> <li ><a href="./">Home</a></li> <li><a href="staff.aspx"& ...

Importing partial peer dependencies in Npm

I have a custom npm package with a peer dependency on element-ui. This package is importing the Pagination component from element-ui: import {Pagination} from 'element-ui';' However, when I import this component in my project, the entire ...

Display all keys and values in a dynamically populated object on my screen - React

I have a dynamic object with nested objects, and I want to display every key and value. Even if there are objects within the main object, I need to show their keys and values as well. Here is an example of the object: info:{ address:{ city: {__o ...