JS+Vue實現三級全選單選
本文實例為大傢分享瞭JS+Vue實現三級全選單選的具體代碼,供大傢參考,具體內容如下
HTML
<div class="demand-class"> <div class="demand-class-title">需求分類</div> <div class="demand-check"> <input class="collect-top-checked" type="checkbox" v-model="demandChecked" @change="handledemandChecked" />全選 </div> <div class="package-type package-type2" v-for="(itns, itds) in classiFications" :key="itns.id" > <div class="package-type-title upgrading-title"> <input class="collect-top-checked" type="checkbox" v-model="ficationsCheck[itds]" @change="handleFicationsCheck(itds)" />{{ itns.name }} <div class="title-bor"></div> </div> <div class="package-type-content"> <div v-for="cd in itns.children" :key="cd.id" class="package-type-list" > <input class="collect-top-checked" type="checkbox" :value="cd.id" @change="handlechildrenCheck(itds, cd.id)" v-model="childrenCheck" /> <div>{{ cd.name }}</div> </div> </div> </div> </div>
js
data () { classiFications: [], //需求分類 接口給的集合 demandChecked: false, // 需求分類全選 ficationsCheck: [], //一級分類的單個全選 childrenCheck: [], //二級分類的全選 demandCheckedShow: false, //二級全選不觸發接口 } methods: { // 需求分類全選 handledemandChecked() { if (this.demandChecked) { this.classiFications.forEach((it, is) => { this.ficationsCheck[is] = true; this.handleFicationsCheck(is); }); } else { this.classiFications.forEach((it, is) => { this.ficationsCheck[is] = false; this.handleFicationsCheck(is); }); } }, //一級分類所選 async handleFicationsCheck(id) { this.demandCheckedShow = true; let tmp = this.classiFications[id].childrenIds; //當前選擇的id子集合 let tmpAdd = this.childrenCheck; //當前選擇的id子集合 if (this.ficationsCheck[id]) { tmp.forEach((item) => { for (let i = 0; i < tmp.length; i++) { if (tmpAdd.indexOf(item) === -1) { this.childrenCheck.push(item); } } }); } else { tmp.forEach((item) => { for (let i = 0; i < tmp.length; i++) { if (tmpAdd.indexOf(item) !== -1) { this.childrenCheck.splice(this.childrenCheck.indexOf(item), 1); } } }); } // this.handleType(); this.currentPage = 0; await this.initSolutionAllPage(); this.demandCheckedShow = false; }, //二級分類所選 handlechildrenCheck(ids, cd) { console.log(cd); let cont = 0; // let conts = 0; let tmp = this.classiFications[ids].childrenIds; //當前選擇的id子集合 let tmpAdd = this.childrenCheck; //當前選擇的id子集合 if (this.ficationsCheck[ids]) { tmp.forEach((item) => { for (let i = 0; i < tmp.length; i++) { if (tmpAdd.indexOf(item) === -1) { this.ficationsCheck[ids] = false; } } }); } else { let tmpl = tmp.length === 1 ? 1 : tmp.length - 1; tmp.forEach((item) => { for (let i = 0; i < tmpl; i++) { if (tmpAdd.indexOf(item) !== -1) { // console.log(item); cont = cont + 1; } } }); if (cont === this.classiFications[ids].childrenIds.length) { this.ficationsCheck[ids] = true; } } // this.handleType(); if (!this.demandCheckedShow) { this.currentPage = 0; this.initSolutionAllPage(); } }, }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- vue中table實現真正的跨越全選
- vue中el-checkbox全選、反選、多選的實現
- elementUI實現下拉選項加多選框的示例代碼
- vue項目input標簽checkbox,change和click綁定事件的區別說明
- Vue必學知識點之forEach()的使用