这个是一个子组件 <template> <view> <text class="node" v-html="label"></text> <view class="children"> <tree-node v-for="(child, key) in children" :key="key" :treeData="child" /> </view> </view> </template> <script> export default { name: 'TreeNode', props: { treeData: { type: Object | Array, required: true } }, computed: { label() { return ` <u-row justify="center" gutter="10" style="display: flex;justify-content: center; margin: 10px"> <u-col span="4"></u-col> <u-col span="4"> <view style="color: red; background: orange; border: 1px solid red;padding: 5px;"> id: ${this.treeData.id} </view> </u-col> <u-col span="4"></u-col> </u-row> <u-row justify="center" gutter="10" style="display: flex;justify-content: center;"> <u-col span="4" style="border: 1px solid red; text-align: center; margin: 10rpx;"> <view> extendOne: ${this.treeData.extendOne} </view> </u-col> <u-col span="4" style="border: 1px solid red; text-align: center; margin: 10rpx;"> <view> extendTwo: ${this.treeData.extendTwo}, </view> </u-col> <u-col span="4" style="border: 1px solid red; text-align: center; margin: 10rpx;"> <view> extendThree: ${this.treeData.extendThree} </view> </u-col> </u-row> ` }, children() { const userExtendThree = this.treeData.userExtendThree if (userExtendThree) { return [userExtendThree] } else { return [] } } } } </script> <style lang="scss" scoped> .tree { font-size: 16px; } .node { display: inline-block; margin-right: 10px; padding: 5px; } .children { margin-left: 20px; } </style> 这个是数据结构 { "createBy": "13774", "createTime": "2023-05-22 16:48:21", "updateBy": "13774", "updateTime": "2023-05-22 17:32:17", "remark": null, "id": 13774, "userId": 13774, "extendOne": 13775, "extendTwo": 13776, "extendThree": 13777, "isLeader": null, "extendId": null, "userExtendThree": { "createBy": "", "createTime": null, "updateBy": "", "updateTime": null, "remark": null, "id": 13777, "userId": 13777, "extendOne": 13778, "extendTwo": 13779, "extendThree": 13780, "isLeader": null, "extendId": null, "userExtendThree": null } } 帮我把label()函数里面的css样式进行美化,画成树状结构的,正方形画成圆形。每一层 用 左 右 中 线段展示关系层级
时间: 2023-07-06 10:03:48 浏览: 162
HTML页面不认<script type="text/template">标签,怎么办?
5星 · 资源好评率100%
好的,可以尝试以下代码来实现您的需求:
```
<template>
<div class="tree">
<div class="node" :class="{ 'node--has-children': hasChildren }" @click="toggle">
<div class="node__content">
<div class="node__circle"></div>
<div class="node__label" v-html="label"></div>
</div>
<div class="node__children" v-show="isOpen">
<tree-node v-for="(child, key) in children" :key="key" :tree-data="child" />
</div>
</div>
</div>
</template>
<script>
export default {
name: 'TreeNode',
props: {
treeData: {
type: Object | Array,
required: true
}
},
data() {
return {
isOpen: false
}
},
computed: {
label() {
return `
<div class="node__id">${this.treeData.id}</div>
<div class="node__extends">
<div class="node__extend">extendOne: ${this.treeData.extendOne}</div>
<div class="node__extend">extendTwo: ${this.treeData.extendTwo}</div>
<div class="node__extend">extendThree: ${this.treeData.extendThree}</div>
</div>
`
},
children() {
const userExtendThree = this.treeData.userExtendThree
if (userExtendThree) {
return [userExtendThree]
} else {
return []
}
},
hasChildren() {
return this.children.length > 0
}
},
methods: {
toggle() {
if (this.hasChildren) {
this.isOpen = !this.isOpen
}
}
}
}
</script>
<style lang="scss" scoped>
.tree {
font-size: 16px;
.node {
position: relative;
display: inline-block;
margin-right: 10px;
padding: 5px;
cursor: pointer;
&__content {
display: flex;
align-items: center;
}
&__circle {
width: 20px;
height: 20px;
border-radius: 50%;
background-color: orange;
border: 1px solid red;
margin-right: 10px;
}
&__label {
display: flex;
flex-direction: column;
justify-content: center;
color: red;
font-weight: bold;
}
&__id {
margin-bottom: 5px;
}
&__extends {
display: flex;
flex-direction: column;
}
&__extend {
margin: 5px 0;
}
&--has-children {
&::before {
content: '';
position: absolute;
left: -15px;
top: 50%;
transform: translateY(-50%);
width: 15px;
height: 1px;
background-color: red;
}
}
&__children {
margin-left: 20px;
}
}
}
</style>
```
你可以将这些代码复制到你的组件中,然后在需要使用的地方引入即可。
阅读全文