vue全选与取消全选
Posted mdge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于vue全选与取消全选的主要内容,如果未能解决你的问题,请参考以下文章