作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 vue js 中有一个带有选择下拉列表的表单,我正在尝试使用 https://sagalbot.github.io/vue-select/这个库来执行这个,根据文档,我必须将一个数组传递给这个库,并且我正在调用 axios 方法来获取下拉菜单的选项。我获取以下格式的数据:
并具有以下描述:
我只想显示选项中的名称并获取值作为该特定行的 ID,如何实现这一点。
我的代码看起来像这样:https://jsfiddle.net/eemdjLex/1/
<div id="app">
<v-select multiple :options="model.data"></v-select>
</div>
import vSelect from 'vue-select';
Vue.component('v-select', vSelect)
const app = new Vue({
el: '#app'
router: router,
data () {
return {
model: {},
columns: {},
}
}
methods: {
fetchIndexData() {
var vm = this;
axios.get('/companies').then(response => {
Vue.set(vm.$data, 'model', response.data.model)
Vue.set(vm.$data, 'columns', response.data.columns)
}
}
});
它无法正常工作,但你明白我想要做什么。
最佳答案
v-select
在使用 v-model
时似乎会返回整个选项作为值,因此我可能会在此处使用一对计算值。
new Vue({
el:"#app",
data:{
serverData,
selected: null
},
computed:{
// serverData is a stand in for your model.data.
// map over that to build your options
selectOptions(){
return this.serverData.map(d => ({label: d.name, value: d.id}))
},
// selectedOption is just a short computed to get the id value
// from whatever option was selected. You could also just use
// "selected.id" in whatever needs the id instead if needed.
selectedOption(){
if (this.selected)
return this.selected.value
else
return null
}
}
})
Example .
关于javascript - 如何在vue js中使用vselect渲染下拉列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140365/
我是一名优秀的程序员,十分优秀!