![](/img/trans.png)
[英]How to give the user the ability to edit already selected data in the vue-select component?
[英]How to limit selected items in vue-select?
我將vue-select用於多個值。 這是一個例子: https : //codepen.io/sagalbot/pen/opMGro
我有以下代碼:
<v-select multiple v-model="selected" :options="options"></v-select>
和JS:
Vue.component('v-select', VueSelect.VueSelect)
new Vue({
el: '#app',
data: {
selected: ['foo','bar'],
options: ['foo','bar','baz']
}
謝謝!
您可以使用v-on:input
偵聽器查看選擇了多少項。
然后將一個簡單的函數傳遞給它:
<v-select multiple v-model="selected" :options="options" v-on:input="limiter"></v-select>
在此之后,在您的方法中創建一個名為limiter
的函數,您將獲得所選輸入的當前列表,如
methods: {
limiter(e) {
if(e.length > 2) {
console.log(' you can only select two', e)
e.pop()
}
},
}
現在,如果您添加了 2 個以上的項目,那么最后一個將被刪除,您將看到控制台日志
您可以簡單地使用內聯條件:
<v-select multiple v-model="selected" :options="selected.length < 2 ? options: []">
在上面的示例中,我僅限於 2 個選項。 如果選擇了 2 個選項,則不會生成這些選項。 刪除選定的一個,然后您將看到選項下拉列表。
這是更新的演示。
你可以使用
:selectable="() => selected.length < 3"
來自官方文檔https://vue-select.org/guide/selectable.html#limiting-the-number-of-selections
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.