|
2 | 2 | <div
|
3 | 3 | :class="{
|
4 | 4 | 'vjs-tree': true,
|
5 |
| - 'has-selectable-control': isMultiple || showRadio, |
| 5 | + 'has-selectable-control': isMultiple || showSelectController, |
6 | 6 | 'is-root': currentDeep === 1,
|
7 | 7 | 'is-selectable': selectable,
|
8 | 8 | 'is-mouseover': isMouseover
|
9 | 9 | }"
|
10 | 10 | @click.stop="handleClick($event, 'tree')"
|
11 | 11 | @mouseover.stop="handleMouseover"
|
12 | 12 | @mouseout.stop="handleMouseout">
|
13 |
| - <template v-if="selectable"> |
| 13 | + <template v-if="showSelectController && selectable"> |
14 | 14 | <vue-checkbox v-if="isMultiple" v-model="currentCheckboxVal" @change="handleClick($event, 'checkbox')"></vue-checkbox>
|
15 |
| - <vue-radio v-else-if="isSingle && showRadio" v-model="model" @change="handleClick($event, 'radio')" :path="path"></vue-radio> |
| 15 | + <vue-radio v-else-if="isSingle" v-model="model" @change="handleClick($event, 'radio')" :path="path"></vue-radio> |
16 | 16 | </template>
|
17 | 17 |
|
18 | 18 | <template v-if="Array.isArray(data) || isObject(data)">
|
|
42 | 42 | :path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)"
|
43 | 43 | :path-selectable="pathSelectable"
|
44 | 44 | :selectable-type="selectableType"
|
45 |
| - :show-radio="showRadio" |
| 45 | + :show-select-controller="showSelectController" |
| 46 | + :select-on-click-node="selectOnClickNode" |
46 | 47 | :current-key="key"
|
47 | 48 | :current-deep="currentDeep + 1"
|
48 | 49 | @click="handleItemClick"
|
|
121 | 122 | type: String,
|
122 | 123 | default: '' // ''|multiple|single radio, checkbox
|
123 | 124 | },
|
124 |
| - // defined radio's view when selectableType=single |
125 |
| - showRadio: { |
| 125 | + // 是否展示左侧选择控件 |
| 126 | + showSelectController: { |
126 | 127 | type: Boolean,
|
127 | 128 | default: false
|
128 | 129 | },
|
| 130 | + // 是否在点击树的时候选中节点 |
| 131 | + selectOnClickNode: { |
| 132 | + type: Boolean, |
| 133 | + default: true |
| 134 | + }, |
129 | 135 | // 存在选择功能时, 定义已选中的数据层级
|
130 | 136 | // 多选时为数组['root.a', 'root.b'], 单选时为字符串'root.a'
|
131 | 137 | value: {
|
|
206 | 212 | */
|
207 | 213 | handleClick (e, emitType = '') {
|
208 | 214 | this.$emit('click', this.path, this.data)
|
209 |
| - if (this.isMultiple && emitType === 'checkbox') { |
| 215 | + if (this.isMultiple && (emitType === 'checkbox' || (this.selectOnClickNode && emitType === 'tree'))) { |
210 | 216 | // handle multiple
|
211 | 217 | const index = this.model.findIndex(item => item === this.path)
|
212 | 218 | if (index !== -1) {
|
213 | 219 | this.model.splice(index, 1)
|
214 | 220 | } else {
|
215 | 221 | this.model.push(this.path)
|
216 | 222 | }
|
| 223 | +
|
| 224 | + if (emitType !== 'checkbox') { |
| 225 | + this.currentCheckboxVal = !this.currentCheckboxVal |
| 226 | + } |
217 | 227 | this.$emit('change', this.currentCheckboxVal)
|
218 |
| - } else if (this.isSingle && (emitType === 'radio' || emitType === 'tree')) { |
| 228 | + } else if (this.isSingle && (emitType === 'radio' || (this.selectOnClickNode && emitType === 'tree'))) { |
219 | 229 | // handle single
|
220 | 230 | if (this.model !== this.path) {
|
221 | 231 | this.model = this.path
|
|
254 | 264 | return this.showDoubleQuotes ? `"${key}"` : key
|
255 | 265 | }
|
256 | 266 | },
|
| 267 | + created () { |
| 268 | + if (this.selectableType && !this.selectOnClickNode && !this.showSelectController) { |
| 269 | + throw new Error('[vue-json-pretty] error') |
| 270 | + } |
| 271 | + }, |
257 | 272 | watch: {
|
258 | 273 | deep (newVal) {
|
259 | 274 | this.visible = this.currentDeep <= newVal
|
|
0 commit comments