|
30 | 30 | class="vjs__tree__content" |
31 | 31 | :key="key"> |
32 | 32 | <vue-json-pretty |
| 33 | + v-model="value" |
33 | 34 | :parent-data="data" |
34 | 35 | :data="item" |
35 | 36 | :deep="deep" |
36 | 37 | :show-length="showLength" |
37 | 38 | :show-double-quotes="showDoubleQuotes" |
| 39 | + :show-mouse-over="showMouseOver" |
38 | 40 | :path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)" |
39 | | - :path-checked="pathChecked" |
40 | 41 | :path-selectable="pathSelectable" |
41 | 42 | :selectable-type="selectableType" |
42 | 43 | :current-key="key" |
43 | 44 | :current-deep="currentDeep + 1" |
44 | | - @click="handleItemClick"> |
| 45 | + @click="handleItemClick" |
| 46 | + @change="handleItemChange"> |
45 | 47 | </vue-json-pretty> |
46 | 48 | </div> |
47 | 49 |
|
|
98 | 100 | type: Boolean, |
99 | 101 | default: true |
100 | 102 | }, |
| 103 | + // 是否展示鼠标悬浮效果 |
| 104 | + showMouseOver: { |
| 105 | + type: Boolean, |
| 106 | + default: false |
| 107 | + }, |
101 | 108 | // 数据层级顶级路径 |
102 | 109 | path: { |
103 | 110 | type: String, |
|
106 | 113 | // 定义数据层级支持的选中方式, 默认无该功能 |
107 | 114 | selectableType: { |
108 | 115 | type: String, |
109 | | - default: '' // both, checkbox, tree |
| 116 | + default: '' // radio, checkbox |
110 | 117 | }, |
111 | | - // 定义已选中的数据层级 |
112 | | - pathChecked: { |
| 118 | + // 存在选择功能时,定义已选中的数据层级 |
| 119 | + value: { |
113 | 120 | type: Array, |
114 | 121 | default: () => [] |
115 | 122 | }, |
|
120 | 127 | }, |
121 | 128 | /* 外部可用 END */ |
122 | 129 |
|
123 | | - /* 内部递归使用 */ |
| 130 | + /* 内部使用 */ |
124 | 131 | // 当前树的父级数据 |
125 | 132 | parentData: {}, |
126 | 133 | // 当前树的深度, 以根节点作为0开始, 所以第一层树的深度为1, 递归逐次递增 |
|
135 | 142 | return { |
136 | 143 | visible: this.currentDeep <= this.deep, |
137 | 144 | treeContentBackground: 'transparent', |
138 | | - checkboxVal: this.pathChecked.includes(this.path) // 复选框的值 |
| 145 | + checkboxVal: this.value.includes(this.path) // 复选框的值 |
139 | 146 | } |
140 | 147 | }, |
141 | 148 | computed: { |
| 149 | + model () { |
| 150 | + return this.value || [] |
| 151 | + }, |
142 | 152 | // 获取当前 data 中最后一项的 key 或 索引, 便于界面判断是否添加 "," |
143 | 153 | lastKey () { |
144 | 154 | if (Array.isArray(this.parentData)) { |
|
158 | 168 | }, |
159 | 169 | // 存在复选框 |
160 | 170 | 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' |
166 | 172 | } |
167 | 173 | }, |
168 | 174 | methods: { |
169 | 175 | /** |
170 | 176 | * 触发组件的 click 事件 |
171 | | - * @param {Boolean} changed 复选框值是否已改变(如果来自复选框 change 事件则已改变) |
| 177 | + * @param {Boolean} fromSelect 是否来自复选框的事件 |
172 | 178 | */ |
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 | + } |
178 | 192 | }, |
179 | 193 | // 处理子树触发的 click 事件, 并传递到顶层 |
180 | 194 | handleItemClick (path, data, checked) { |
181 | 195 | this.$emit('click', path, data, checked) |
182 | 196 | }, |
| 197 | + handleItemChange (val) { |
| 198 | + // 不存在选择的时候change事件无意义 |
| 199 | + if (this.existCheckbox) { |
| 200 | + this.$emit('change', val) |
| 201 | + } |
| 202 | + }, |
183 | 203 | handleMouseover () { |
184 | | - this.existMouseover && this.selectable && (this.treeContentBackground = '#eee') |
| 204 | + this.showMouseOver && this.selectable && (this.treeContentBackground = '#eee') |
185 | 205 | }, |
186 | 206 | handleMouseout () { |
187 | | - this.existMouseover && this.selectable && (this.treeContentBackground = 'transparent') |
| 207 | + this.showMouseOver && this.selectable && (this.treeContentBackground = 'transparent') |
188 | 208 | }, |
189 | 209 | // 是否对象 |
190 | 210 | isObject (value) { |
|
0 commit comments