最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue multi select limit option count - Stack Overflow

programmeradmin4浏览0评论

I have a Vue application that uses multiselect, I want to have it so there are 5 options (a,b,c,d,e) and a user can select up to a max of 3. I checked the docs and this doesn't seem to be a supported param since the limit it mentions just limits how many are visable after selecting.

Here is my current code but it doesn't do what I want

Data:

selectedOptions: [],
optionsLimit: 3,
optionsList: ["a","b","c","d","e",]

Template:

<multiselect v-model="selectedOptions" :multiple="true" :options="optionsList" :hide-selected="true"></multiselect>

Current behaviour:

Will allow selecting of all 5 options instead of limiting to 3.

How can I make it so a user can select a max of 3 options?

I have a Vue application that uses multiselect, I want to have it so there are 5 options (a,b,c,d,e) and a user can select up to a max of 3. I checked the docs and this doesn't seem to be a supported param since the limit it mentions just limits how many are visable after selecting.

Here is my current code but it doesn't do what I want

Data:

selectedOptions: [],
optionsLimit: 3,
optionsList: ["a","b","c","d","e",]

Template:

<multiselect v-model="selectedOptions" :multiple="true" :options="optionsList" :hide-selected="true"></multiselect>

Current behaviour:

Will allow selecting of all 5 options instead of limiting to 3.

How can I make it so a user can select a max of 3 options?

Share Improve this question asked Oct 1, 2020 at 22:59 joshk132joshk132 1,1131 gold badge18 silver badges51 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can use the max prop for this:

Template:

<multiselect v-model="selectedOptions" :multiple="true" :options="optionsList" :hide-selected="true" :max="3"></multiselect>

You can use the disable feature to disable multiselect using :disabled="selectedOptions.length>=optionsLimit". The code below:

// register globally
Vue.ponent('multiselect', window.VueMultiselect.default)

new Vue({
  el: '#app',
  data: {
    selectedOptions: [],
    optionsLimit: 3,
    optionsList: ["a", "b", "c", "d", "e", ]
  },
})
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg./[email protected]"></script>
<link rel="stylesheet" href="https://unpkg./[email protected]/dist/vue-multiselect.min.css">

<div id="app">

  <multiselect v-model=selectedOptions :multiple="true" :options="optionsList" :hide-selected="true" :disabled="selectedOptions.length>=optionsLimit"></multiselect>

</div>

发布评论

评论列表(0)

  1. 暂无评论