gpt4 book ai didi

javascript - 如何使用 vue-multiselect 的 v-model 值选中复选框

转载 作者:行者123 更新时间:2023-12-04 14:54:46 25 4
gpt4 key购买 nike

问题:创建的选定选项复选框未被选中,我希望复选框被选中,而不考虑checked true/false

注意:我总是想要没有checked状态的值(value)模型

下图显示了我的问题(请看黄色区域)

enter image description here

这是我尝试过的:

new Vue({
components: {
Multiselect: window.VueMultiselect.default
},
data: {
value: [],
options: [
{ language: 'JavaScript', library: 'Vue.js', checked: false },
{ language: 'JavaScript', library: 'Vue-Multiselect', checked: false },
{ language: 'JavaScript', library: 'Vuelidate', checked: false }
]
},
methods: {
customLabel (option) {
return `${option.library} - ${option.language}`
},
onSelect (option) {
console.log("Added");
let index = this.options.findIndex(item => item.library==option.library);
this.options[index].checked = true;
console.log(option.library + " Clicked!! " + option.checked);
},

onRemove (option) {
console.log("Removed");
let index = this.options.findIndex(item => item.library==option.library);
this.options[index].checked = false;
console.log(option.library + " Removed!! " + option.checked);
}
},
created(){
this.value = [{ language: 'JavaScript', library: 'Vue.js',checked:true }];
}
}).$mount('#app')
* {
font-family: 'Lato', 'Avenir', sans-serif;
}

.checkbox-label {
display: block;
}

.test {
position: absolute;
right: 1vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link href="https://unpkg.com/vue-multiselect@2.0.2/dist/vue-multiselect.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/vue-multiselect@2.0.3/dist/vue-multiselect.min.js"></script>
<div id="app">
<multiselect
select-Label=""
selected-Label=""
deselect-Label=""
v-model="value"
:options="options"
:multiple="true"
track-by="library"
:custom-label="customLabel"
:close-on-select="false"
@select=onSelect($event)
@remove=onRemove($event)
>
<span class="checkbox-label" slot="option" slot-scope="scope" @click.self="select(scope.option)">
{{ scope.option.library }}
<input class="test" type="checkbox" v-model="scope.option.checked" @focus.prevent/>

</span>
</multiselect>
<pre>{{ value }}</pre>
</div>

请提前帮助我谢谢!!

最佳答案

要理解您真正想要的东西有点困难,但假设:

  1. 您想呈现复选框
  2. 您不需要 v-model 中的 checked 字段(因此添加它只是为了帮助呈现复选框)

您根本不需要 checked 属性,因为可以通过针对 v-model 的简单检查轻松替换该值。这样您就可以删除很多不必要的代码。

请参阅下面的示例:

new Vue({
components: {
Multiselect: window.VueMultiselect.default
},
data: {
value: [],
options: [{
language: 'JavaScript',
library: 'Vue.js'
},
{
language: 'JavaScript',
library: 'Vue-Multiselect'
},
{
language: 'JavaScript',
library: 'Vuelidate'
}
]
},
methods: {
customLabel(option) {
return `${option.library} - ${option.language}`
},
isSelected(option) {
/* unfortunatelly following line does not work as VueMultiselect for some (strange) reason
fills the v-model array with copies instead of original objects contained in options
*/
// return this.value.includes(option)
return this.value.some((op) => op.library === option.library)
}
},
created() {
this.value.push(this.options[0])
}
}).$mount('#app')
* {
font-family: 'Lato', 'Avenir', sans-serif;
}

.checkbox-label {
display: block;
}

.test {
position: absolute;
right: 1vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link href="https://unpkg.com/vue-multiselect@2.0.2/dist/vue-multiselect.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vue-multiselect@2.0.3/dist/vue-multiselect.min.js"></script>
<div id="app">
<multiselect select-Label="" selected-Label="" deselect-Label="" v-model="value" :options="options" :multiple="true" track-by="library" :custom-label="customLabel" :close-on-select="false">
<span class="checkbox-label" slot="option" slot-scope="scope">
{{ scope.option.library }}
<input class="test" type="checkbox" :checked="isSelected(scope.option)" @focus.prevent :key="scope.option.library" />
</span>
</multiselect>
<pre>{{ value }}</pre>
</div>

关于javascript - 如何使用 vue-multiselect 的 v-model 值选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68280166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com