简体   繁体   English

如何在 vuetify v-select 的列表和菜单中使用不同的值?

[英]How to use different value in list and menu in vuetify v-select?

How can I change this example so that in the menu I will get things like "Florida, FL" but in the actual v-select I get "FL" only.我怎样才能改变这个例子,以便在菜单中我会得到像“Florida, FL”这样的东西,但在实际的 v-select 中我只得到“FL”。

 new Vue({ el: '#app', vuetify: new Vuetify(), data () { return { select: [{ state: 'Florida', abbr: 'FL' }], items: [ { state: 'Florida', abbr: 'FL' }, { state: 'Georgia', abbr: 'GA' }, { state: 'Nebraska', abbr: 'NE' }, { state: 'California', abbr: 'CA' }, { state: 'New York', abbr: 'NY' }, ], } }, })
 <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet"/> <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script> <div id="app"> <v-app id="inspire"> <v-container fluid> <v-row align="center"> <v-col cols="6"> <v-subheader>Custom items</v-subheader> </v-col> <v-col cols="6"> <v-select v-model="select":items="items" item-text="state" item-value="abbr" label="Select" persistent-hint multiple ></v-select> </v-col> </v-row> </v-container> </v-app> </div>

You don't have to change the settings, only add @change handler to the component, and handle it with a method .您不必更改设置,只需将@change处理程序添加到组件,并使用方法进行处理。 Watch for passing the emitted event to your method ( $event ).注意将发出的事件传递给您的方法 ( $event )。 By passing the values of the emitted event to your method, you can work with the object that was selected (the event actually passes that object to your method).通过将发出的事件的值传递给您的方法,您可以使用选定的对象(事件实际上将该对象传递给您的方法)。

 new Vue({ el: '#app', vuetify: new Vuetify(), data() { return { select: [{ menuText: 'Florida, FL', state: 'Florida', abbr: 'FL' }], items: [{ state: 'Florida', abbr: 'FL' }, { state: 'Georgia', abbr: 'GA' }, { state: 'Nebraska', abbr: 'NE' }, { state: 'California', abbr: 'CA' }, { state: 'New York', abbr: 'NY' }, ], } }, computed: { menuItems() { return this.items.map(e => { return { menuText: e.state + ', ' + e.abbr, ...e } }) } }, methods: { logValue(e) { this.select = e console.log(e) } } })
 /*.v-select__selection.v-select__selection--comma { display: none; }*/.v-text-field__prefix { color: #242729; }
 <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script> <div id="app"> <v-app id="inspire"> <v-container fluid> <v-row align="center"> <v-col cols="6"> <v-subheader>Custom items</v-subheader> </v-col> <v-col cols="6"> <v-select v-model="select":hint="`${select.state}, ${select.abbr}`":items="menuItems" item-text="abbr" item-value="menuText" return-object single-line @change="logValue($event)" multiple> <template v-slot:item="{ item }"> <strong>{{item.menuText}}</strong> </template> </v-select> </v-col> </v-row> </v-container> </v-app> </div>

OTHER HINT其他提示

You had an extre Vue script reference in your snippet - I removed it in mine.您的代码段中有一个 extre Vue 脚本引用 - 我在我的代码中删除了它。

EDIT编辑

To have access to the dropdown items, you have to use the item slot v v-select offers.要访问下拉项目,您必须使用项目插槽v v-select offers。 There you can modify the appearance of the items however you want.您可以在那里随意修改项目的外观。

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

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