我看到了一些关于v-select和插槽的文档,但是我并不真正理解我是否可以将它应用到我的示例码页中。
我只需要获得所选的文本(而不是值),并在代码中的某个地方使用它:
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
state: {},
selectedText: "",
states: [
{ value: "a", text: "alpha" },
{ value: "b", text: "beta" },
{ value: "g", text: "gamma" }
]
},
methods: {
change: (newValue) => {
// do something with the text
// "alpha", "beta", or "gama"
console.log(newValue);
}
}
});<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.2.20/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.js"></script>
<div id="app">
<v-app id="inspire">
<v-container fluid>
<label>my selected text is: {{state}}</label>
<v-row align="center">
<v-col cols="3">
<v-select v-model="state" :items="states" @change="change" :text="selectedText"></v-select>
</v-col>
</v-row>
</v-container>
</v-app>
</div>
发布于 2020-04-01 16:18:14
您需要将return-object支柱添加到<v-select>中
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
state: null,
selectedText: "",
states: [
{ value: "a", text: "alpha" },
{ value: "b", text: "beta" },
{ value: "g", text: "gamma" }
]
},
methods: {
change: (newValue) => {
// do something with the text
// "alpha", "beta", or "gama"
console.log(newValue.text);
}
}
});<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.2.20/dist/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.js"></script>
<div id="app">
<v-app id="inspire">
<v-container fluid>
<label>my selected text is: {{state && state.text}}</label>
<v-row align="center">
<v-col cols="3">
<v-select :items="states" v-model="state" @change="change" item-text="text" return-object></v-select>
</v-col>
</v-row>
</v-container>
</v-app>
</div>
编辑: Ok因此根据您的方法,解决方案是使用国家代码在国家列表中找到适当的国家对象并设置它。
下面是解决这个问题的方法:
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
country: "c",
countries: [{
code: "a",
name: "Ameriga Fatela"
},
{
code: "b",
name: "Bolivia Grande"
},
{
code: "c",
name: "Comore Potentia"
}
]
},
methods: {
getCountryCode() {
return "b"; // have no c.name here!
},
change() {
var newCode = this.getCountryCode();
// Since we were getting objects when changing options, we must also set objects
this.country = this.countries.filter(country => country.code === newCode)[0];
}
}
});<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.2.20/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.js"></script>
<div id="app">
<v-app>
<v-container>
<div>current code is >{{country.code}}<</div>
<div>current name is >{{country.name}}<</div>
<v-row>
<v-col cols="12">
<v-select v-model="country" :items="countries" item-text="name" item-value="code" return-object></v-select>
<v-btn @click="change">change by script to 'b'</v-btn>
</vcol>
</v-row>
</v-container>
</v-app>
</div>
发布于 2020-04-01 16:39:49
states对象同时包含value和text属性。如果将value更改为key,则v-select会识别更改,并且可以通过this.state访问text属性。就像这样:
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
state: {},
selectedText: "",
states: [
{ key: "a", text: "alpha" },
{ key: "b", text: "beta" },
{ key: "g", text: "gamma" }
]
},
methods: {
change: (newValue) => {
// do something with the text
// "alpha", "beta", or "gama"
console.log(newValue); // Also returns text attribute instead of key
}
}
});<div id="app">
<v-app id="inspire">
<v-container fluid>
<label>my selected text is: {{state}}</label>
<v-row align="center">
<v-col cols="3">
<v-select v-model="state" :items="states" @change="change"></v-select>
</v-col>
</v-row>
</v-container>
</v-app>
</div>发布于 2020-04-02 10:59:52
computed编辑:编辑:--我发现的最佳选择是对所选文本使用一个属性,而不对当前代码进行更改(在读取片段后进入FullPage以正确查看输出):
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
countries: [
{ code: "a", name: "Ameriga Fatela" },
{ code: "b", name: "Bolivia Grande" },
{ code: "c", name: "Comore Potentia" }
],
country: "b"
},
methods: {
getCountryCode() {
return "c"; // have no c.name here!
},
change() {
this.country = this.getCountryCode();
}
},
computed: {
countryName() {
return this.countries.find((c) => c.code === this.country).name;
}
}
});<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.2.20/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.js"></script>
<div id="app">
<v-app>
<v-container>
<div>current code is >{{country}}<</div>
<div>current name is >{{countryName}}<</div>
<v-row>
<v-col cols="12">
<v-select v-model="country" :items="countries" item-text="name" item-value="code"></v-select>
<v-btn @click="change">change by script to '{{getCountryCode()}}'</v-btn>
</vcol>
</v-row>
</v-container>
</v-app>
</div>
另一个选项是(科德芬),AnuragSri伐use建议使用return-object,我返回了这个对象。但是,它也有一些缺点,因为实际上我无法通过代码正确地更改值:
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
country: "c",
countries: [
{ code: "a", name: "Ameriga Fatela" },
{ code: "b", name: "Bolivia Grande" },
{ code: "c", name: "Comore Potentia" }
]
},
methods: {
getCountryCode() {
return "b"; // have no c.name here!
},
change() {
var newCode = this.getCountryCode();
this.country = newCode;
}
}
});<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.2.20/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.2.20/dist/vuetify.min.js"></script>
<div id="app">
<v-app>
<v-container>
<div>current code is >{{country.code}}<</div>
<div>current name is >{{country.name}}<</div>
<v-row>
<v-col cols="12">
<v-select v-model="country" :items="countries" item-text="name" item-value="code" return-object></v-select>
<v-btn @click="change">change by script to 'b'</v-btn>
</vcol>
</v-row>
</v-container>
</v-app>
</div>
然而,在这两种情况下,我们都应该重新计算国名。这可不妙。想象一下要建造组合框我们得做个大手术..。每次重新计算都很费时,并不是最优的.
https://stackoverflow.com/questions/60975352
复制相似问题