Skip to content

Commit 2a22f7a

Browse files
committed
移除child标识,改为currenDeep,并增加deep属性
1 parent 5b9fe1e commit 2a22f7a

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

example/App.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<h3>JSON Input:</h3>
2222
<textarea v-model="val"></textarea>
2323

24-
<h3>Click Result:</h3>
24+
<h3>Latest Click Result:</h3>
2525
<div>path: {{itemPath}}</div>
2626
<div>data: <pre>{{itemData}}</pre></div>
2727
</div>
@@ -103,6 +103,7 @@ export default {
103103
<style lang="less">
104104
html, body {
105105
margin: 0;
106+
background-color: #f9f9f9;
106107
}
107108
.example {
108109
position: relative;
@@ -130,6 +131,11 @@ export default {
130131
border-radius: 5px;
131132
resize: vertical;
132133
font-family: inherit;
134+
&:focus {
135+
outline: none;
136+
border-color: #1d8ce0;
137+
box-shadow: 0 0 3px #1d8ce0;
138+
}
133139
}
134140
pre{
135141
margin: 0;

src/components/app.vue

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
class="vjs__tree"
44
:style="{
55
'background-color': treeContentBackground,
6-
'position': child ? '' : 'relative',
7-
'margin-left': !child && existCheckbox ? '30px' : ''
6+
'position': currentDeep > 1 ? '' : 'relative',
7+
'margin-left': currentDeep === 1 && existCheckbox ? '30px' : ''
88
}"
99
@click.stop="handleClick($event)"
1010
@mouseover.stop="handleMouseover"
1111
@mouseout.stop="handleMouseout">
12-
1312
<template v-if="selectable && existCheckbox" class="vjs-checkbox">
1413
<checkbox v-model="checkboxVal" @change="handleClick($event, true)"></checkbox>
1514
</template>
@@ -21,7 +20,7 @@
2120
:data="data"
2221
:index="index"
2322
:last-index="lastIndex">
24-
<span v-if="child && !Array.isArray(parentData)">{{ index }}:</span>
23+
<span v-if="currentDeep > 1 && !Array.isArray(parentData)">{{ index }}:</span>
2524
</brackets-left>
2625

2726
<!-- 数据内容, data 为对象时, index 表示 key, 为数组才表示索引 -->
@@ -33,12 +32,13 @@
3332
<vue-json-pretty
3433
:parent-data="data"
3534
:data="item"
35+
:deep="deep"
3636
:path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)"
3737
:path-checked="pathChecked"
3838
:path-selectable="pathSelectable"
3939
:selectable-type="selectableType"
4040
:index="index"
41-
:child="true"
41+
:current-deep="currentDeep + 1"
4242
@click="handleItemClick">
4343
</vue-json-pretty>
4444
</div>
@@ -76,7 +76,13 @@
7676
},
7777
props: {
7878
/* 外部可用 START */
79-
data: {}, // 当前树的数据
79+
// 当前树的数据
80+
data: {},
81+
// 定义树的深度, 大于该深度的子树将不被展开
82+
deep: {
83+
type: Number,
84+
default: Infinity
85+
},
8086
// 数据层级顶级路径
8187
path: {
8288
type: String,
@@ -98,13 +104,19 @@
98104
default: () => true
99105
},
100106
/* 外部可用 END */
101-
parentData: {}, // 当前树的父级数据
102-
child: Boolean, // 是否子树(优化: 通过 $parent?)
107+
108+
// 当前树的父级数据
109+
parentData: {},
110+
// 当前树的深度, 以根节点作为0开始, 所以第一层树的深度为1, 递归逐次递增
111+
currentDeep: {
112+
type: Number,
113+
default: 1
114+
},
103115
index: {}
104116
},
105117
data () {
106118
return {
107-
visiable: true,
119+
visiable: this.currentDeep <= this.deep,
108120
treeContentBackground: 'transparent',
109121
checkboxVal: this.pathChecked.includes(this.path) // 复选框的值
110122
}

0 commit comments

Comments
 (0)