Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify v-select onchange event returns previously selected value instead of current

I have a <v-select> dropdown that I'm wanting to use as a list of URLs to navigate to other pages. The issue I'm running into is the onchangeevent I'm using returns the previously selected value instead of the current selected value.

I have tweaked the code to print to console instead for testing. The :hint functionality works fine so I'm sure it's something to do with the onchange function.

Codepen

Here's the code:

<template>
  <v-app>
  <v-container fluid>
    <v-layout row wrap>
      <v-flex xs6>
        <v-select
          :items="items"
          v-model="select"
          label="Select"
          single-line
          item-text="report"
          item-value="src"
          return-object
          persistent-hint
          v-on:change="changeRoute(`${select.src}`)"
          :hint="`${select.src}`"
        ></v-select>
      </v-flex>
    </v-layout>
  </v-container>
</v-app>
</template>

<script>
/* eslint-disable */
    new Vue({
  el: '#app',
  data () {
      return {
        select: { report: 'Rep1', src: '/rep1' },
        items: [
          { report: 'Rep1', src: '/rep1' },
          { report: 'Rep2', src: '/rep2' }
        ]
      }
    },
    methods: {
      changeRoute(a) {
        //this.$router.push({path: a })
        console.log(a)
      }
    }
})
</script>
like image 973
Matthew Snell Avatar asked Apr 28 '18 07:04

Matthew Snell


3 Answers

You don't need to specify the data because that's what, I'm guessing, the change event passes by default.

So change:

v-on:change="changeRoute(`${select.src}`)"

to just

v-on:change="changeRoute"

and in the function call:

  changeRoute(a) {
    this.$router.push({path: a.src })
    console.log(a)
  }
like image 148
A. L Avatar answered Nov 12 '22 16:11

A. L


I have no idea why change doesn't work properly. But input does work.

https://codepen.io/jacobgoh101/pen/erBwKa?editors=1011

v-on:input="changeRoute(`${select.src}`)"

Perhaps you can open a new bug report for Vuetify

like image 24
Jacob Goh Avatar answered Nov 12 '22 14:11

Jacob Goh


I don't exctly know why ${select.src} is holding previous value on change event. You can give a try with below code:

<v-select @change="changeRoute" ></v-select>

methods: {
      changeRoute(selectObj) {
        console.log(selectObj)
        console.log(selectObj.src)
     }
}
like image 6
Karthik S Avatar answered Nov 12 '22 16:11

Karthik S