I am working on a project which is made in laravel 10 and Vue3.
In the form the users can only select 3 checkboxes and after that all checkboxes should get disabled.
I have tried it in this way as you can see below. But the problem is after 3 selected checkboxes all checkboxes are disabled even the ones which are selected so I can not deselect the selected ones.
How can I solve the problem? Thanks in advanced
<div class="form-group group-sm offset-top-20">
<div v-if="professions_data !== null" v-for="(profession, index) in professions_data"
:key="profession.id">
<div class="custom-control custom-checkbox d-inline-block" style="width:350px">
<input class="custom-control-input" :id="profession.name_slug + '-' + profession.id"
name="profession"
:value="profession.id"
v-model="value"
type="checkbox"
@change="updateField"
:disabled="value.length > max && value.indexOf(n) === -1" >
<label class="custom-control-label" :for="profession.name_slug + '-' + rofession.id">{{ profession.name }}</label>
</div>
</div>
</div>
data() {
return {
name: 'profession',
professions_data: null,
value: [],
max:3
}
},
methods: {
updateField() {
this.clearErrors(this.name);
this.$emit('update:field', this.value)
},
}
2
Answers
Use a computed value to hold an array of the inactivated values (or an empty array if none). Something like:
Explanation
In this example the data is simple:
I create a ref to hold the selected values:
and then a computed property that I called
inactivated
to hold the data points that I want to be inactivated. It should return an array, initially an empty array if the number of selected items is less than 3, but as soon as 3 items have been selected, it returns all the remaining values:Then this function can be used by the checkbox edit item to determine if it should be inactivated or not:
i made you a working example.
See here: Stackblitz
but this is the code:
Written in Vue3 Composition API