V
V
Vitalionus2019-12-10 00:27:16
Vue.js
Vitalionus, 2019-12-10 00:27:16

How to check a checkbox in a nested component through its incoming $event.target?

Has a parent:

<CheckboxComponent
  v-model="myDatas"
  :value="1"
  @click="onCheckbox($event)"
>
// В нем метод:
onCheckbox($event) {
  // Тут проверки, если сняты все чекбыксы, то включить последний снятый
  $event.target.checked = true;
    
},

In the CheckboxComponent
<input
  :value="value"
  :checked="isChecked"
  type="checkbox"
  @click="handleClick($event)"
>
// И тут есть метод:
handleClick($event) {
  this.$emit('click', $event);
}

$event comes to the parent, everything is ok.
But I have a lot of such checkboxes in the parent, I want to make a check. To prevent the last checkbox from being removed.
For example, so that any one would always be included. And it was impossible to remove everything.
I try to make a check on click in the parent and if all checkboxes are cleared
Then include the last one removed via $event.target.checked = true. But from Parent

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-12-10
@Vitalionus

You don't need to set checked directly, that's not how vue does things.

prevent unchecking the last checkbox

Add the -disabled parameter to your component. In the parent, make a method to calculate its value: if the checkbox is selected and the number of selected checkboxes is equal to one - true, otherwise false. For example .

A
Aetae, 2019-12-12
@Aetae

No need to drive the state back and forth, just do it:
But in general - all the data obviously needs to be changed through the model, and not in a roundabout way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question