2017-07-20 21 views
3

別の選択肢の選択に基づいて1つの選択肢にデータをフィルタリングする方法を教えてください。何かのように、私はある選択から州を選択し、第2の選択はその特定の州のすべての都市をロードします。 私はフレームワークとしてvue.jsとvuetifyを使用しています(v-selectコンポーネントを使用)。私は計算されたプロパティを設定しようとしましたが、最初のselectから選択した場合でも、2番目のものはデータをロードしません。 https://jsfiddle.net/vcmiranda/tkkhwq6m/Vue.js/Vuetify - 別の選択肢に基づいて選択データをフィルタリングする

ありがとう:

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>

は、ここではjsfiddleリンクです。

答えて

3

あなたがしたいのはfilter都市のオプションです。

fiddleを更新

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

にごfilteredData計算プロパティを変更

+0

偉大な男、それは完璧に働いた。ありがとう。 –

+0

@VitorMirandaあなたは大歓迎です:)これはあなたの最初の質問です。将来的に、回答があなたの質問に役立つ、または答えた場合は、アップヴォート(担当者がいるとき)または同意を示すことができます。 – Bert

+0

警告のおかげで、私は知らなかった。 –

関連する問題