vue實現(xiàn)全選和反選功能
更新時間:2017年08月31日 14:54:44 作者:mystraight
這篇文章主要為大家詳細介紹了vue實現(xiàn)全選和反選功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)全選反選功能的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script type="text/javascript" src = "vue.js"></script>
<body>
<div id = "test">
<input type='checkbox' v-model="checkBox.checked" class='input-checkbox' @click='checkedAll'>全選
<div v-for='checkb in checkboxData'>
<input type='checkbox' class='input-checkbox' @click="checkItem" v-model='checkBox.items[checkb.id]'>
{{checkb.value}}
</div>
</div>
</body>
<script>
var vue = new Vue({
el:"#test",
data:{
checkboxData:[
{
id:'1',
value:'蘋果'
},{
id:'2',
value:'荔枝'
},{
id:'3',
value:'香蕉'
},{
id:'4',
value:'火龍果'
}
],
checkBox:{
checked:false,
items:{}
}
},
methods:{
checkedAll: function() {
var _this = this;
console.log(_this.checkboxData);
console.log(this.checkBox.items);
this.checkboxData.forEach(function (item) {
console.log(item.id);
_this.checkBox.items[item.id] = _this.checkBox.checked;
console.log(_this.checkBox.items);
});
//實現(xiàn)反選
},
checkItem:function(){
var unchecked = 0;
var _this = this;
this.checkboxData.forEach(
function(item) {
unchecked += (! _this.checkBox.items[item.id]) || 0;
});
_this.checkBox.checked = unchecked > 0 ? false : true;
}
}
})
</script>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實現(xiàn)的雙向數(shù)據(jù)綁定操作示例
這篇文章主要介紹了vue實現(xiàn)的雙向數(shù)據(jù)綁定操作,結(jié)合完整實例形式較為詳細的分析了vue.js進行數(shù)據(jù)雙向綁定操作的常見實現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
基于Echarts圖表在div動態(tài)切換時不顯示的解決方式
這篇文章主要介紹了基于Echarts圖表在div動態(tài)切換時不顯示的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue?cli2?和?cli3去掉eslint檢查器報錯的解決
這篇文章主要介紹了vue?cli2?和?cli3去掉eslint檢查器報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue+Element-ui彈窗?this.$alert?is?not?a?function問題
這篇文章主要介紹了Vue+Element-ui彈窗?this.$alert?is?not?a?function問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

