<template>
<div>
<div v-for="(item,index) in treedata" :key="index">
<!-- itemNodeClick 点击 当前行 -->
<div class="item-title" @click="itemNodeClick(item)">
<span v-text="item.name" ></span>
<span v-if="item.children && item.children.length">
<!-- isOpen 控制箭头 -->
<van-icon name="arrow-down" :class="{ 'negative-rotate': isOpen(item.id)}" style="transition: transform 0.3s"/>
</span>
</div>
<!-- needid === 当前行id 的时候展开 -->
<div v-if="item.children && item.children.length && needid === item.id" class="item-childen">
<!-- 递归组件 -->
<kshBiTree
:treedata="item.children"
/>
</div>
</div>
</div>
</template>
<script>
import { sharesv1 } from '../api'
export default {
name: 'kshBiTree',
props: {
treedata: {
type: Array,
default: () => []
},
needIx: {
type: Number,
default: () => null
},
active: {
type: Number,
default: () => null
}
},
data () {
return {
needIndex: null,
expandedKeys: [],
needid: ''
}
},
mounted () {
this.needIndex = this.needIx
console.log(this.treedata)
},
methods: {
isOpen (id) {
return this.needid === id
},
itemNodeClick (item) {
if (item.children && item.children.length) {
if (this.needid === item.id) {
this.needid = ''
} else {
this.needid = item.id
}
} else {
this.goLink(item)
}
},
goLink (item) {
const data = {
authenticationMode: 'NONE',
expiryDate: '',
roles: [],
rowPermissionBy: '',
users: [],
vizId: item.relId || item.id,
vizType: this.active === 2 ? 'STORYBOARD' : item.relType
}
let key = 'shareStoryPlayer/'
switch (item.relType) {
case 'DATACHART':
key = 'shareChart/'
break
case 'DASHBOARD':
key = 'shareDashboard/'
break
default:
break
}
sharesv1(data).then(res => {
window.location.href = (process.env.NODE_ENV === 'prod' ? 'https://dataenginebi.71360.com/shareChart/' : 'https://predataenginebi.71360.com/' + key + res.data.id + '?type=NONE')
})
},
clickIcon (item) {
console.log(item)
}
}
}
</script>
<style lang="scss" scoped>
.item-title{
height: 0.8rem;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #fafafa;
}
.foldTop{
display: flex;
height: 0.8rem;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #fafafa;
}
.foldText{
height: 0.8rem;
line-height: 0.8rem;
border-bottom: 1px solid #fafafa;
}
.p20{
padding-left: .4rem;
}
/deep/.arrowIcon{
transition: 0.2s;
transform-origin: center;
transform: rotateZ(90deg);
}
/deep/.arrowIconTwo.van-icon{
transition: 0.2s;
transform-origin: center;
transform: rotateZ(-90deg);
}
.negative-rotate{
transform: rotateZ(-180deg);
}
.item-childen{
padding-left: 20px
}
</style>
arrToTree (data) {
const arr = []
const obj = {}
if (!(data instanceof Array)) {
return arr
}
data.forEach(item => {
obj[item.id] = item
})
data.forEach(item => {
if (obj[item.parentId]) {
if (obj[item.parentId].children) {
obj[item.parentId].children.push(item)
} else {
;(obj[item.parentId].children = []).push(item)
}
} else {
arr.push(item)
}
})
return arr
},