Skip to content

Commit cc9ea77

Browse files
committed
Checkbox and tree should not exist at the same time
1 parent bb417bc commit cc9ea77

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

example/App.vue

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<vue-json-pretty
1212
:deep="deep"
1313
:data="json"
14-
:path="'res'">
14+
:path="'res'"
15+
@click="handleClick">
1516
</vue-json-pretty>
1617
</div>
1718
</div>
@@ -28,9 +29,7 @@
2829
<label>selectable-type</label>
2930
<select v-model="selectableType">
3031
<option>-</option>
31-
<option>both</option>
3232
<option>checkbox</option>
33-
<option>tree</option>
3433
</select>
3534
</div>
3635
<div>
@@ -45,6 +44,10 @@
4544
<label>showDoubleQuotes</label>
4645
<input type="checkbox" v-model="showDoubleQuotes">
4746
</div>
47+
<div>
48+
<label>showMouseOver</label>
49+
<input type="checkbox" v-model="showMouseOver">
50+
</div>
4851
<div>
4952
<label>deep</label>
5053
<select v-model="deep">
@@ -66,11 +69,13 @@
6669
:path="path"
6770
:deep="deep"
6871
:show-double-quotes="showDoubleQuotes"
72+
:show-mouse-over="showMouseOver"
6973
:show-length="showLength"
70-
:path-checked="['res', 'res.c']"
74+
v-model="pathSelected"
7175
:path-selectable="((path, data) => typeof data !== 'number')"
7276
:selectable-type="selectableType"
73-
@click="handleClick">
77+
@click="handleClick"
78+
@change="handleChange">
7479
</vue-json-pretty>
7580
</div>
7681
</div>
@@ -107,9 +112,11 @@ export default {
107112
members: ['Daniel, Mike, John']
108113
}]
109114
},
110-
selectableType: 'both',
115+
pathSelected: ['res', 'res.error'],
116+
selectableType: 'checkbox',
111117
showLength: true,
112118
showDoubleQuotes: true,
119+
showMouseOver: true,
113120
path: 'res',
114121
deep: 4,
115122
itemData: {},
@@ -130,10 +137,13 @@ export default {
130137
}
131138
},
132139
methods: {
133-
handleClick (path, data, checked) {
134-
console.log('click', path, data, checked)
140+
handleClick (path, data) {
141+
console.log('click: ', path, data)
135142
this.itemPath = path
136143
this.itemData = !data ? data + '' : data // 处理 data = null 的情况
144+
},
145+
handleChange (val) {
146+
console.log('change: ', val)
137147
}
138148
}
139149
}

src/components/app.vue

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@
3030
class="vjs__tree__content"
3131
:key="key">
3232
<vue-json-pretty
33+
v-model="value"
3334
:parent-data="data"
3435
:data="item"
3536
:deep="deep"
3637
:show-length="showLength"
3738
:show-double-quotes="showDoubleQuotes"
39+
:show-mouse-over="showMouseOver"
3840
:path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)"
39-
:path-checked="pathChecked"
4041
:path-selectable="pathSelectable"
4142
:selectable-type="selectableType"
4243
:current-key="key"
4344
:current-deep="currentDeep + 1"
44-
@click="handleItemClick">
45+
@click="handleItemClick"
46+
@change="handleItemChange">
4547
</vue-json-pretty>
4648
</div>
4749

@@ -98,6 +100,11 @@
98100
type: Boolean,
99101
default: true
100102
},
103+
// 是否展示鼠标悬浮效果
104+
showMouseOver: {
105+
type: Boolean,
106+
default: false
107+
},
101108
// 数据层级顶级路径
102109
path: {
103110
type: String,
@@ -106,10 +113,10 @@
106113
// 定义数据层级支持的选中方式, 默认无该功能
107114
selectableType: {
108115
type: String,
109-
default: '' // both, checkbox, tree
116+
default: '' // radio, checkbox
110117
},
111-
// 定义已选中的数据层级
112-
pathChecked: {
118+
// 存在选择功能时,定义已选中的数据层级
119+
value: {
113120
type: Array,
114121
default: () => []
115122
},
@@ -120,7 +127,7 @@
120127
},
121128
/* 外部可用 END */
122129
123-
/* 内部递归使用 */
130+
/* 内部使用 */
124131
// 当前树的父级数据
125132
parentData: {},
126133
// 当前树的深度, 以根节点作为0开始, 所以第一层树的深度为1, 递归逐次递增
@@ -135,10 +142,13 @@
135142
return {
136143
visible: this.currentDeep <= this.deep,
137144
treeContentBackground: 'transparent',
138-
checkboxVal: this.pathChecked.includes(this.path) // 复选框的值
145+
checkboxVal: this.value.includes(this.path) // 复选框的值
139146
}
140147
},
141148
computed: {
149+
model () {
150+
return this.value || []
151+
},
142152
// 获取当前 data 中最后一项的 key 或 索引, 便于界面判断是否添加 ","
143153
lastKey () {
144154
if (Array.isArray(this.parentData)) {
@@ -158,33 +168,43 @@
158168
},
159169
// 存在复选框
160170
existCheckbox () {
161-
return this.selectableType === 'both' || this.selectableType === 'checkbox'
162-
},
163-
// 存在mouseover
164-
existMouseover () {
165-
return this.selectableType === 'both' || this.selectableType === 'tree'
171+
return this.selectableType === 'checkbox'
166172
}
167173
},
168174
methods: {
169175
/**
170176
* 触发组件的 click 事件
171-
* @param {Boolean} changed 复选框值是否已改变(如果来自复选框 change 事件则已改变)
177+
* @param {Boolean} fromSelect 是否来自复选框的事件
172178
*/
173-
handleClick (e, changed = false) {
174-
// 由于 checkbox 也依赖该函数, 因此通过 changed 进行排除
175-
if (!changed && !this.existMouseover || !this.selectable) return
176-
changed || (this.checkboxVal = !this.checkboxVal)
177-
this.$emit('click', this.path, this.data, this.checkboxVal)
179+
handleClick (e, fromSelect = false) {
180+
// 由于 checkbox 也依赖该函数, 因此通过 fromSelect 进行排除
181+
this.$emit('click', this.path, this.data)
182+
if (fromSelect) {
183+
const index = this.model.findIndex(item => item === this.path)
184+
if (index !== -1) {
185+
this.model.splice(index, 1)
186+
} else {
187+
this.model.push(this.path)
188+
}
189+
this.$emit('input', this.model)
190+
this.$emit('change', this.checkboxVal)
191+
}
178192
},
179193
// 处理子树触发的 click 事件, 并传递到顶层
180194
handleItemClick (path, data, checked) {
181195
this.$emit('click', path, data, checked)
182196
},
197+
handleItemChange (val) {
198+
// 不存在选择的时候change事件无意义
199+
if (this.existCheckbox) {
200+
this.$emit('change', val)
201+
}
202+
},
183203
handleMouseover () {
184-
this.existMouseover && this.selectable && (this.treeContentBackground = '#eee')
204+
this.showMouseOver && this.selectable && (this.treeContentBackground = '#eee')
185205
},
186206
handleMouseout () {
187-
this.existMouseover && this.selectable && (this.treeContentBackground = 'transparent')
207+
this.showMouseOver && this.selectable && (this.treeContentBackground = 'transparent')
188208
},
189209
// 是否对象
190210
isObject (value) {

0 commit comments

Comments
 (0)