1. 程式人生 > >vue全選與取消全選

vue全選與取消全選

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="../node_modules/vue/dist/vue.js"></script> </head> <body> <div id="my">   <input type="checkbox" :checked="all" @click="changeAll()"/> 全選 <ul> <li v-for="(v,i) in items"> <input type="checkbox" :checked="v.status" :index="i" @click="changeItems(i)"/> {{v.name}} </li> </ul> </div> <script> let vm = new Vue({ el:"#my", data:{ all:true, items:[ {name:'手機',status:false}, {name:'電腦',status:false}, {name:'平板',status:true}, {name:'包包',status:false} ], selectValue:[] }, methods:{ changeAll(){ let self = this; this.all = !this.all this.items.forEach((val)=>{ val.status = self.all }) }, changeItems(i){ this.items[i].status = !this.items[i].status; let itemsStatus = this.items.every((val)=>{ return val.status }) console.log(itemsStatus) if(itemsStatus){ this.all = true }else{ this.all = false } } }, mounted(){ let itemsStatus = this.items.every((val)=>{ return val.status }) console.log(itemsStatus) if(itemsStatus){ this.all = true }else{ this.all = false } }   }) </script> </body> </html>