最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - populate vuetify select from an array of json - Stack Overflow

programmeradmin0浏览0评论

Am a Newbie to VUEJS, am having trouble populating a vuetify select element with a names ofcountries from a local JSON file containing an array of a json objects. Instead of populating the options it creates single select objects for each country.

Here is my Form....

<form>
  <v-select v-validate="'required'" v-bind="countryData"
  v-for="item in countryData" :key="item.name" :items="item.name"
  v-model="select" :error-messages="errors.collect('country')"
  label="Country" data-vv-name="country" prepend-icon="mdi-flag"
  required></v-select>
 </form>

This is my script.....

<script>
import Vue from "vue";
import VeeValidate from "vee-validate";
import countryData from '@/ponents/countryData.json';
Vue.use(VeeValidate);
export default {
  name: "signup",
  $_veeValidate: {
    validator: "new"
  },
  data: () => ({
    countryData: countryData,
    country: ''
    })
</script>

Here JSON file Structure...

[
    {
        "id": 1,
        "name": "Afghanistan",
        "iso3": "AFG",
        "iso2": "AF",
        "country_code": "4",
        "phone_code": "93",
        "capital": "Kabul",
        "currency": "AFN",
        "states": ["Badakhshan","Badgis"...]
    },
    {
        ...
    }
]

Output of my codes

Am a Newbie to VUEJS, am having trouble populating a vuetify select element with a names ofcountries from a local JSON file containing an array of a json objects. Instead of populating the options it creates single select objects for each country.

Here is my Form....

<form>
  <v-select v-validate="'required'" v-bind="countryData"
  v-for="item in countryData" :key="item.name" :items="item.name"
  v-model="select" :error-messages="errors.collect('country')"
  label="Country" data-vv-name="country" prepend-icon="mdi-flag"
  required></v-select>
 </form>

This is my script.....

<script>
import Vue from "vue";
import VeeValidate from "vee-validate";
import countryData from '@/ponents/countryData.json';
Vue.use(VeeValidate);
export default {
  name: "signup",
  $_veeValidate: {
    validator: "new"
  },
  data: () => ({
    countryData: countryData,
    country: ''
    })
</script>

Here JSON file Structure...

[
    {
        "id": 1,
        "name": "Afghanistan",
        "iso3": "AFG",
        "iso2": "AF",
        "country_code": "4",
        "phone_code": "93",
        "capital": "Kabul",
        "currency": "AFN",
        "states": ["Badakhshan","Badgis"...]
    },
    {
        ...
    }
]

Output of my codes

Share Improve this question edited Dec 14, 2018 at 16:42 Boussadjra Brahim 1 asked Dec 14, 2018 at 16:23 joekenpatjoekenpat 3874 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You shouldn't use v-for to add data to the v-select ponent, you need only to pass contryData as value of items property :

 <v-select v-validate="'required'"  
    :items="countryData"
    item-text='name'
    item-value='id'
    v-model="country" 
    :error-messages="errors.collect('country')"
  label="Country" data-vv-name="country" prepend-icon="mdi-flag"
 required></v-select>

Note 'item-name' will control which field in your item object will be displayed, while 'item-value' will be the field that controls the selected option value. I chose 'id' for that but depending on your use case you might pick 'country_code' or something else.

发布评论

评论列表(0)

  1. 暂无评论