Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check all checkboxes vuejs

I'm displaying a list of users in a table, each row has a checkbox to select the user and the checkbox value is the user's ID. The selected IDs are in turn displayed in a span below the table.

How can I select all checkboxes and deselect all checkboxes on the click of a "select all" checkbox that I have in the header of my table? Do I interact with the DOM to do this or through the vue object, I'm thinking it should be the latter but quite unsure how to approach what appears to be an easy task?! Any help would be appreciated!

HTML

<div id="app">     <h4>Users</h4>     <div>         <table>             <tr>                 <th>Name</th>                 <th>Select <input type="checkbox" @click="selectAll"></th>             </tr>             <tr v-for="user in users">                 <td>{{ user.name }}</td>                 <td><input type="checkbox" v-model="selected" value="{{ user.id }}"></td>             </tr>         </table>     </div>      <span>Selected Ids: {{ selected| json }}</span> </div> 

Javascript/Vuejs

new Vue({     el: '#app',     data: {         users: [              { "id": "1", "name": "Shad Jast", "email": "[email protected]",              { "id": "2", "name": "Duane Metz", "email": "[email protected]"},              { "id": "3", "name": "Myah Kris", "email": "[email protected]"},              { "id": "4", "name": "Dr. Kamron Wunsch", "email": "[email protected]"},              { "id": "5", "name": "Brendon Rogahn", "email": "[email protected]"}         ],         selected: []     },     methods: {         selectAll: function() {             // ?         }     } }) 
like image 580
haakym Avatar asked Nov 06 '15 16:11

haakym


People also ask

How to check all checkboxes in VueJs?

To check all checkboxes with Vue. js, we can add a change event listener to the checkbox that checks all the other checkboxes. We have the users array that's rendered in a table. In the template, we have a table row for the select all checkbox.

How do you check checkbox is checked or not in VUE JS?

To watch for checkbox clicks in Vue. js, we can listen to the change event. to add the @change directive to the checkbox input to listen to the change event. Then we can get the checked value of the checkbox with e.

How do I submit a form using Vue?

In VueJs, To work with form and submit a form with proper data you need to use v-model. This will bind your form data. Let's see the below example where we build a simple form submit option in which a student can send his name and age the the information will be visible in the browser also.

How do I make a checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

I think @Jeremy's answer is cleaner way, but it require for checked property on each user object which is makes no sense if the data come from an API request.

Here is working and cleaner code for select/deselect all rows without having to add checked property on user object:

new Vue({      el: '#app',      data: {          users: [               { "id": "1", "name": "Shad Jast", "email": "[email protected]" },              { "id": "2", "name": "Duane Metz", "email": "[email protected]" },               { "id": "3", "name": "Myah Kris", "email": "[email protected]" },               { "id": "4", "name": "Dr. Kamron Wunsch", "email": "[email protected]" }          ],          selected: []      },      computed: {          selectAll: {              get: function () {                  return this.users ? this.selected.length == this.users.length : false;              },              set: function (value) {                  var selected = [];                    if (value) {                      this.users.forEach(function (user) {                          selected.push(user.id);                      });                  }                    this.selected = selected;              }          }      }  });
<script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script>    <div id="app">  <h4>User</h4>  <div>      <table>          <tr>              <th><input type="checkbox" v-model="selectAll"></th>              <th align="left">Name</th>          </tr>          <tr v-for="user in users">              <td>                  <input type="checkbox" v-model="selected" :value="user.id" number>              </td>              <td>{{ user.name }}</td>          </tr>      </table>  </div>  </div>

Please note that the number attribute on row's checkbox is required, otherwise you have to push the user id selectAll method as a string, like selected.push(user.id.toString());

like image 98
Rifki Avatar answered Sep 25 '22 05:09

Rifki


Adding my own answer as edits on the answer by nhydock weren't accepted (I think?).

Solution selects and selects all.

HTML

<div id="app">     <h4>User</h4>         <div>             <table>                 <tr>                     <th>Name</th>                     <th>Select <input type="checkbox" @click="selectAll" v-model="allSelected"></th>                 </tr>                 <tr v-for="user in users">                     <td>{{ user.name }}</td>                     <td><input type="checkbox" v-model="userIds" value="{{ user.id }}"></td>                 </tr>             </table>         </div>          <span>Selected Ids: {{ userIds | json }}</span> </div> 

Javascript/Vuejs

new Vue({     el: '#app',     data: {         users: [              { "id": "1", "name": "Shad Jast", "email": "[email protected]"},              { "id": "2", "name": "Duane Metz", "email": "[email protected]"},              { "id": "3", "name": "Myah Kris", "email": "[email protected]"},              { "id": "4", "name": "Dr. Kamron Wunsch", "email": "[email protected]"},              { "id": "5", "name": "Brendon Rogahn", "email": "[email protected]"}         ],         selected: [],         allSelected: false,         userIds: []     },     methods: {         selectAll: function() {             this.userIds = [];              if (!this.allSelected) {                 for (user in this.users) {                     this.userIds.push(this.users[user].id);                 }             }         },     } }) 

Working fiddle: https://jsfiddle.net/okv0rgrk/3747/

like image 43
haakym Avatar answered Sep 24 '22 05:09

haakym