
<template>
<view class="aaa">
<br />
<view class="f1"> 请选择服务人员服务区域 </view>
<view class=""> 四川 </view>
<view class=""> 已认证8个城市 </view>
<br />
<view class="">
<my-tree
:tree-data="testJson"
@xuanzhong="xuanzhong"
@bindPickerChange="bindPickerChange"
></my-tree>
</view>
</view>
</template>
<script>
import myTree from "./myTree.vue";
export default {
components: {
myTree,
},
data() {
return {
testJson: "",
};
},
onLoad() {
this.aaa();
this.testJson = require("./data.json");
this.testJson = [
{
code: "51",
name: "四川省",
show: true,
children: [
{
code: "5101",
name: "成都市",
show: false,
children: [
{
code: "510107",
name: "武侯区",
show: true,
},
{
code: "510116",
name: "双流区",
show: true,
},
],
},
],
},
];
},
methods: {
aaa() {
this.$post(
"/Public/GetAreaList",
{},
{
isLoading: true,
}
).then((res) => {});
},
},
};
</script>
<style lang="scss" scoped>
.aaa {
}
.f1 {
text-align: center;
color: rgba(16, 16, 16, 1);
font-size: 24px;
}
</style>
<template>
<div class="tree-item">
<div v-for="item in treeData" :key="item.id">
<div class="item-title" @click="nodeClick(item)">
<span :class="item.show ? 'active' : ''">{{ item.name }}</span>
<span v-if="item.children && item.children.length">
[{{ isOpen(item.id) ? "-" : "+" }}]
</span>
</div>
<div
v-if="item.children && item.children.length"
v-show="isOpen(item.id)"
class="item-childen"
>
<my-tree
:treeData="item.children"
@node-click="$emit('node-click', $event)"
></my-tree>
</div>
</div>
</div>
</template>
<script>
export default {
name: "myTree",
props: {
treeData: {
type: Array,
default: () => [],
},
},
data() {
return {
expandedKeys: [], // 当前展开的节点id组成的数组
};
},
methods: {
nodeClick(item) {
this.$emit("node-click", item);
if (item.children && item.children.length) {
let index = this.expandedKeys.indexOf(item.id);
if (index > -1) {
// 如果当前节点id存在数组中,则删除
this.expandedKeys.splice(index, 1);
} else {
// 如果当前节点id不存在数组中,则添加
this.expandedKeys.push(item.id);
}
}
},
isOpen(id) {
// 判断节点id在不在数组中,在则显示,不在则隐藏
return this.expandedKeys.includes(id);
},
},
};
</script>
<style lang="scss" scoped>
.active {
color: red;
}
.tree-item {
cursor: pointer;
.item-title {
padding: 4px 8px;
&:hover {
background: #eee;
}
}
.item-childen {
padding-left: 20px;
}
}
</style>