vue---tree實(shí)現(xiàn)遞歸組件(多級(jí)下拉菜單)

? <div class="detail-list">

? ? <div class="item" v-for="(item, index)in list" :key="index">

? ? ? <div class="item-title border-bottom" @click.stop="changeStatus(index)" :class="{'active':isActive[index]}">


? ? ? ? <span class="oks">{{ item.title }}

? ? ? <!-- 注意遞歸終止條件 -->

? ? ? <div v-if="item.children" class="item-children">

? ? ? ? <!-- 通過name進(jìn)行遞歸調(diào)用 -->

? ? ? ? <detail-list v-on="$listeners"

? ? ? ? ? v-if="scopesDefault[index]"

? ? ? ? ? :list="item.children"

? ? ? ? >

export default {

name:"DetailList",

? props: {

list:Array,

? },

? data() {

return {

isActive:[],

? ? ? scopesDefault: [], // 第一級(jí)

? ? ? scopes: [], // 第二級(jí)

? ? };

? },

? created() {

this.scope();

? },

? mounted(){

},

? methods: {

changeStatus(index) {

var ods=[]

for(var b=0;b

ods.push(false)

}

this.scopesDefault=ods;

? ? ? this.isActive=ods;

? ? ? if (this.scopesDefault[index] ==true) {

this.$set(this.scopesDefault, index, false);

? ? ? }else {

this.$set(this.scopesDefault, index, this.scopes[index]);

? ? ? ? if(this.list[index].children==undefined){

this.$emit("Eok",true)

//數(shù)據(jù)內(nèi)容

? ? ? ? ? // var value = [{

? ? ? ? ? //? title: "文華酒店自助晚餐",

// }]

//

// this.list[index]['children']=value

? ? ? ? ? this.$set(this.isActive, index, true);

? ? ? ? }else{

this.$set(this.isActive, index, this.scopes[index]);

? ? ? ? }

}

// this.list.push({

? ? ? //? title: "特惠1票",

// })

? ? },

? ? scope() {

this.list.forEach((item, index) => {

this.scopes[index]=false

? ? ? ? this.isActive[index]=false

? ? ? ? this.scopesDefault[index] =false; // 第一級(jí)索引值全都是false

? ? ? ? if ("children" in item) {// 判斷第一級(jí)是否有children

? ? ? ? ? this.scopes[index] =true;

? ? ? ? }else {

this.scopes[index] =false;

? ? ? ? }

});

? ? },

? },

};

<style scoped>

.item-title {

line-height:40px;

? padding:0 10px;

? display:flex;

? color:#666;

}

.item-title span {

cursor:pointer;

? display:block;

? margin:0 auto;

}

.item-title.active span{

border-bottom:2px solid #03a9f4;

? color:#03a9f4;

}

.item-children {

padding:0 20px;

? position:absolute;

? width:100%;

? left:0;

}

.item-title.active{

}

.detail-list{

width:100%;

? display:flex;

? border-bottom:1px solid #ddd;

? position:relative;

}

.detail-list>div{

flex:1;

}

.item{

flex:1;

}

</style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容