简体   繁体   English

Vuetify v-select 值不返回键

[英]Vuetify v-select value not returning key

I am trying to v-model the keys from this data from my v-select:我正在尝试从我的 v-select 中对这些数据的键进行 v 建模:

{
data: {
IT: "Italy",
PL: "Poland",
AF: "Afghanistan",
AX: "Aland Islands",
AL: "Albania"
     }
}

My v-select and output look something like this (this is as close as i can get:我的 v-select 和 output 看起来像这样(这是我能得到的最接近的:

        {{ this.model[0] }}

        <v-select
        :items="Object.entries(this.items)"
        v-model="model"
        >
      </v-select>

The problem with this, is the value of the v-select will be "IT, Italy", when i just want Italy问题在于,当我只想要意大利时,v-select 的值将是“IT,意大利”

I've been trying to work out ways, such as using a helper function to search the value and return a key, but i keep getting undefined, and wonder if there is an easier way of doing this.我一直在尝试寻找方法,例如使用帮助程序 function 来搜索值并返回一个键,但我一直不确定,想知道是否有更简单的方法来做到这一点。 Or maybe converting the data to a new object.或者可能将数据转换为新的 object。

If anyone has any ideas i would greatly appreciate it如果有人有任何想法,我将不胜感激

It's better to return a countries property from data() (make it a function.) than to iterate through Object entries最好从 data() 返回国家属性(使其成为 function。)而不是遍历 Object 条目

data() {
    return {
      countries: [
        {key: 'IT', name: 'Italy'},
        {key: 'PL', name: 'Poland'} // ...

      ],
      country: null
    }
  }

Then in your template you can use it like this:然后在您的模板中,您可以像这样使用它:

<v-select :items="countries" v-model="country" item-value="key" item-text="name"></v-select>

With the item-value and item-text props you can determine what is the displayed text and the current value使用 item-value 和 item-text 道具,您可以确定显示的文本和当前值是什么

Thanks for your answer.感谢您的回答。 I managed to get it to work without any helper functions:我设法让它在没有任何辅助功能的情况下工作:

  :items="items"
  item-value="[0]" 
  item-text="[1]"

If you're using Vue-select 3.0, using reduce will do the trick for you如果您使用的是 Vue-select 3.0,使用 reduce 将为您解决问题

const options = [{country: 'Canada', code: 'CA'},];

<!-- v2: using index --->
<v-select :options="options" label="country" index="code"  />

<!-- v3: using reduce --->
<v-select :options="options" label="country" :reduce="country 
=>country.code" />

You can find more information about the migration on their website .您可以在他们的网站上找到有关迁移的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM