Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js/Vuetify - Filter select data based on another select choice

How can I filter data to populate one select based on the choice of another select? Something like, I choose an state from one select and a second select is loaded with all cities from that specific state. I'm using vue.js and vuetify as framework (with v-select component). I've tried to set a computed property, but even when I choose from first select, the second one doesn't load data.

HTML

Vue.use(Vuetify);

var app = new Vue({
  el: '#app',
  data() {
    return {
      items: {
        home_id: null,
      },
      support: {
        home_province: '',
      },
      options: {
        opt_province: [{
          text: 'British Columbia',
          value: 1
        }, {
          text: 'Ontario',
          value: 2
        }],
        opt_city: [{
          text: 'Vancouver',
          value: 1,
          dependency: 1
        }, {
          text: 'Surrey',
          value: 1,
          dependency: 1
        }, {
          text: 'London',
          value: 1,
          dependency: 2
        }, {
          text: 'Toronto',
          value: 1,
          dependency: 2
        }]
      }
    }
  },
  computed: {
    filteredData() {
      var city = this.options.opt_city;
      var province = this.support.home_province;
      for (var i = 0; i < city.length; i++) {
        if (city[i].dependency != province.value) {
          city.splice(i);
        }
      }
      return city;
    },
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vuetify/dist/vuetify.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet" type="text/css">
<div id="app">
  <v-app>
    <v-card class="grey lighten-4 elevation-0">
      <v-card-text>
        <v-container fluid>
          <v-layout row wrap>
            <v-flex xs4>
              <v-flex>
                <v-subheader>Origin Province</v-subheader>
              </v-flex>
              <v-flex>
                <v-select :items="options.opt_province" v-model="support.home_province" label="Select" class="input-group--focused" single-line>
                </v-select>
              </v-flex>
            </v-flex>
            <v-flex xs4>
              <v-flex>
                <v-subheader>Origin City</v-subheader>
              </v-flex>
              <v-flex>
                <v-select :items="filteredData" v-model="items.home_id" label="Select" class="input-group--focused" single-line autocomplete>
                </v-select>
              </v-flex>
            </v-flex>
          </v-layout>
        </v-container>
      </v-card-text>
    </v-card>
  </v-app>
</div>

Here it is jsfiddle link: https://jsfiddle.net/vcmiranda/tkkhwq6m/

Thanks.

like image 883
Vitor Miranda Avatar asked Jul 20 '17 20:07

Vitor Miranda


1 Answers

What you want to do is filter the city options.

Change your filteredData computed property to

filteredData() {
  let options = this.options.opt_city
  return options.filter(o => o.dependency == this.support.home_province)
}

Updated fiddle.

like image 56
Bert Avatar answered Oct 20 '22 06:10

Bert