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": "pfeffer.matt@yahoo.com",
{ "id": "2", "name": "Duane Metz", "email": "mohammad.ferry@yahoo.com"},
{ "id": "3", "name": "Myah Kris", "email": "evolkman@hotmail.com"},
{ "id": "4", "name": "Dr. Kamron Wunsch", "email": "lenora95@leannon.com"},
{ "id": "5", "name": "Brendon Rogahn", "email": "verlie.buckridge@yahoo.com"}
],
selected: []
},
methods: {
selectAll: function() {
// ?
}
}
})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…