Vue.js implementation of checkbox selection and inverse selection

Source: Internet
Author: User

Summary:

1, listen to the change of the value of the Select all, to change the Checbox array value

2. Listen for changes to the array values of the selected checkbox, and when a checkbox changes, determine if the value of the checkbox is the same as the length of the list data, to change the values of all selections.

Let's take a look at how this is accomplished:

First, the HTML

<template>

<table class= "Table table-bordered table-hover text-center" id= "Tabletlist" >

<thead>

<tr>

<th class= "BgColor" width= "25px" ></th>

<th class= "BgColor" > Award time </th>

<th class= "BgColor" > User id</th>

<th class= "BgColor" > User phone number </th>

<th class= "BgColor" > Prizes </th>

<th class= "BgColor" > User address </th>

<th class= "BgColor" > User name </th>

<th class= "BgColor" > Status </th>

<th class= "BgColor" > Edit </th>

</tr>

</thead>

<tbody>

<tr v-for= "(x, Index) in list" >

<td><input type= "checkbox" class= "checkbox" v-model= "x.checked" @change = "Selectsingle (index, X.userid)" ></td>

<TD v-text= "X.createtime" ></td>

<TD v-text= "X.userid" ></td>

<TD class= "Uerphone" v-text= "X.phone" ></td>

<TD class= "Awardstatus" >

<p v-text= "X.goodsname" ></p>

</td>

<TD class= "Address" v-text= "x.address" ></td>

<TD class= "UserName" v-text= "X.name" ></td>

<TD class= "Useraward" v-text= "X.awardstatusstr" ></td>

<td><a href= "javascript:;" @click = "Modify (Index)" class= "Curedit" > Modify </a></td>

</tr>

</tbody>

</table>


Second, JS

Export Default {

Data () {

Checked: [],

Allchecked:null,

checkedcount:0,

List: [],//JSON data sent back from the background

total:{

num:0,

page:0

},

},

Methods: {

Selectsingle (index, ID) {//single input Click event

Console.log (Index)

Console.log (this.list[index].checked)

this.list[index].checked ^= 1

This.list.forEach (item, i) = = {

if (Item.userid = = = ID && I!== index) {

item.checked = this.list[index].checked

this. $set (this.list, I, item)

}

})

if (this.list[index].checked = = = True) {

This.checkedcount + +

}

},

SelectAll (Event) {//Select all

This.list.forEach (item, i) = = {

if (item.checked = = = True) {

item.checked = i.checked

}

item.checked ^= 1

this. $set (this.list, I, item)

})

if (this.allchecked = = True) {

This.checkedcount = This.total.num

}else{

This.checkedcount = 0

}

},

}

}

That's all. Isn't that super simple.

Vue.js implementation of checkbox selection and inverse selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.