E
E
Evgeny Romashkan2018-05-11 22:12:36
Vue.js
Evgeny Romashkan, 2018-05-11 22:12:36

How to select checkboxes by clicking on vue.js?

Here is an example on pure js:
https://codepen.io/anon/pen/xjroxX?editors=0010
How can I do this in vue, or how can I implement something similar using vue?
I know about v-model, but checkboxes are created in a cycle, and it seems to me that generating a model for each is not an option.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-05-11
@EvgeniiR

How can I screw this thing into vue, or how can I implement something similar using vue?

There are different options - you would tell why you need this.
If completely on the forehead, then
methods: {
  onMouseOver(e) {
    if (e.buttons) {
      e.target.checked = !e.target.checked;
    }
  },
},

<input
  type="checkbox"
  v-for="i in 3000"
  @mouseover="onMouseOver"
>

for each, it seems to me that it is not an option to generate a model

Quite an option:
data: () => ({
  checkboxes: Array(3000).fill(false),
}),
methods: {
  onMouseOver(e, index) {
    if (e.buttons) {
      this.$set(this.checkboxes, index, !this.checkboxes[index]);
    }
  },
},

<input
  type="checkbox"
  v-for="(n, i) in checkboxes"
  v-model="checkboxes[i]"
  @mouseover="onMouseOver($event, i)"
>

True, it will work noticeably slower than without v-model(if there are a lot of checkboxes).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question