|
3 | 3 | class="vjs__tree"
|
4 | 4 | :style="{
|
5 | 5 | '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' : '' |
8 | 8 | }"
|
9 | 9 | @click.stop="handleClick($event)"
|
10 | 10 | @mouseover.stop="handleMouseover"
|
11 | 11 | @mouseout.stop="handleMouseout">
|
12 |
| - |
13 | 12 | <template v-if="selectable && existCheckbox" class="vjs-checkbox">
|
14 | 13 | <checkbox v-model="checkboxVal" @change="handleClick($event, true)"></checkbox>
|
15 | 14 | </template>
|
|
21 | 20 | :data="data"
|
22 | 21 | :index="index"
|
23 | 22 | :last-index="lastIndex">
|
24 |
| - <span v-if="child && !Array.isArray(parentData)">{{ index }}:</span> |
| 23 | + <span v-if="currentDeep > 1 && !Array.isArray(parentData)">{{ index }}:</span> |
25 | 24 | </brackets-left>
|
26 | 25 |
|
27 | 26 | <!-- 数据内容, data 为对象时, index 表示 key, 为数组才表示索引 -->
|
|
33 | 32 | <vue-json-pretty
|
34 | 33 | :parent-data="data"
|
35 | 34 | :data="item"
|
| 35 | + :deep="deep" |
36 | 36 | :path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)"
|
37 | 37 | :path-checked="pathChecked"
|
38 | 38 | :path-selectable="pathSelectable"
|
39 | 39 | :selectable-type="selectableType"
|
40 | 40 | :index="index"
|
41 |
| - :child="true" |
| 41 | + :current-deep="currentDeep + 1" |
42 | 42 | @click="handleItemClick">
|
43 | 43 | </vue-json-pretty>
|
44 | 44 | </div>
|
|
76 | 76 | },
|
77 | 77 | props: {
|
78 | 78 | /* 外部可用 START */
|
79 |
| - data: {}, // 当前树的数据 |
| 79 | + // 当前树的数据 |
| 80 | + data: {}, |
| 81 | + // 定义树的深度, 大于该深度的子树将不被展开 |
| 82 | + deep: { |
| 83 | + type: Number, |
| 84 | + default: Infinity |
| 85 | + }, |
80 | 86 | // 数据层级顶级路径
|
81 | 87 | path: {
|
82 | 88 | type: String,
|
|
98 | 104 | default: () => true
|
99 | 105 | },
|
100 | 106 | /* 外部可用 END */
|
101 |
| - parentData: {}, // 当前树的父级数据 |
102 |
| - child: Boolean, // 是否子树(优化: 通过 $parent?) |
| 107 | +
|
| 108 | + // 当前树的父级数据 |
| 109 | + parentData: {}, |
| 110 | + // 当前树的深度, 以根节点作为0开始, 所以第一层树的深度为1, 递归逐次递增 |
| 111 | + currentDeep: { |
| 112 | + type: Number, |
| 113 | + default: 1 |
| 114 | + }, |
103 | 115 | index: {}
|
104 | 116 | },
|
105 | 117 | data () {
|
106 | 118 | return {
|
107 |
| - visiable: true, |
| 119 | + visiable: this.currentDeep <= this.deep, |
108 | 120 | treeContentBackground: 'transparent',
|
109 | 121 | checkboxVal: this.pathChecked.includes(this.path) // 复选框的值
|
110 | 122 | }
|
|
0 commit comments