5

I wonder if I use array as model for multiple-checkbox list, how can I check which items is checked and which are unchecked efficiently rather than compare one by one with that array?

<ul>
<li v-for="task in tasks">
<input type="checkbox" :id="task.title" :value="task" v-model="selectedTasks" @change="handleTasks(task)">
<label :for="task.title">{{task.title}}</label>
</li>
</ul>


new Vue({
  data: {
      tasks: [
        { title: 'Go to the store' },
        { title: 'Clean the house' },
        { title: 'Learn Vue.js' }
      ],
      selectedTasks: []
  },
})
2
  • 1
    the selected task objects will be in the array. I am unsure exactly what you are really trying to ask or do. Commented Jul 20, 2018 at 0:02
  • @JacobGoh In my example, that selectedTasks can hold value that I checked, but I also want to know if one item is checked or unchecked when I click on it( that current design can not tell me that by giving me one variable ) Commented Jul 20, 2018 at 17:58

3 Answers 3

4

You could add a property to each task (e.g., checked) and bind that to each input's v-model, making it trivial in code to check whether a task is checked/selected:

new Vue({
  el: '#app',
  data() {
    return {
      tasks: [
        { title: 'Go to the store', checked: false },
        { title: 'Clean the house', checked: false },
        { title: 'Learn Vue.js', checked: false }
      ]
    };
  },
  methods: {
    clearCheckedTasks() {
      this.tasks = this.tasks.filter(x => !x.checked);
    }
  }
})
<script src="https://unpkg.com/[email protected]"></script>

<div id="app">
  <ul>
    <li v-for="task in tasks">
      <input type="checkbox" :id="task.title" v-model="task.checked">
      <label :for="task.title">{{task.title}}</label>
    </li>
  </ul>
  
  <button @click="clearCheckedTasks">Clear checked tasks</button>
  
  <h3>tasks (live)</h3>
  <pre>{{tasks}}</pre>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, in this way, how can I keep that model array "selectedTasks"? What I want is more like I want to keep that selectedTasks array to hold all selected items and when I click any item, I still able to get if it is checked or unchecked
4

based on your comment that "I also want to know if one item is checked or unchecked when I click on it", I would use the DOM Event object to detect if it's checked.

demo: https://jsfiddle.net/jacobgoh101/m480bupd/6/

add @click="clickHandler" to the input

<input type="checkbox" :id="task.title" :value="task" v-model="selectedTasks" @change="handleTasks(task)" @click="clickHandler">

use e.target.checked to get the checked

methods: {
    clickHandler(e) {
        console.log(e.target.checked);
    },
    // ...
  }

Comments

3

Use the loop index:

<li v-for="(task, index) in tasks">
  <input type="checkbox" :id="task.title" :value="task" v-model="selectedTasks[index]" @change="handleTasks(task)">
  <label :for="task.title">{{task.title}}</label>
</li>

1 Comment

Thanks, in this way, how can I keep all the value I selected and remove unselected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.