|
18 | 18 | <brackets-left
|
19 | 19 | :visiable.sync="visiable"
|
20 | 20 | :data="data"
|
21 |
| - :index="index" |
22 |
| - :last-index="lastIndex"> |
23 |
| - <span v-if="currentDeep > 1 && !Array.isArray(parentData)">{{ index }}:</span> |
| 21 | + :not-last-key="notLastKey"> |
| 22 | + <span v-if="currentDeep > 1 && !Array.isArray(parentData)">{{ currentKey }}:</span> |
24 | 23 | </brackets-left>
|
25 | 24 |
|
26 |
| - <!-- 数据内容, data 为对象时, index 表示 key, 为数组才表示索引 --> |
| 25 | + <!-- 数据内容, data 为对象时, key 表示键名, 为数组时表示索引 --> |
27 | 26 | <div
|
28 |
| - v-for="(item, index) in data" |
| 27 | + v-for="(item, key) in data" |
29 | 28 | v-show="visiable"
|
30 | 29 | class="vjs__tree__content"
|
31 |
| - :key="index"> |
| 30 | + :key="key"> |
32 | 31 | <vue-json-pretty
|
33 | 32 | :parent-data="data"
|
34 | 33 | :data="item"
|
35 | 34 | :deep="deep"
|
36 |
| - :path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)" |
| 35 | + :path="path + (Array.isArray(data) ? `[${key}]` : `.${key}`)" |
37 | 36 | :path-checked="pathChecked"
|
38 | 37 | :path-selectable="pathSelectable"
|
39 | 38 | :selectable-type="selectableType"
|
40 |
| - :index="index" |
| 39 | + :current-key="key" |
41 | 40 | :current-deep="currentDeep + 1"
|
42 | 41 | @click="handleItemClick">
|
43 | 42 | </vue-json-pretty>
|
|
47 | 46 | <brackets-right
|
48 | 47 | :visiable.sync="visiable"
|
49 | 48 | :data="data"
|
50 |
| - :index="index" |
51 |
| - :last-index="lastIndex"> |
| 49 | + :not-last-key="notLastKey"> |
52 | 50 | </brackets-right>
|
53 | 51 | </template>
|
54 | 52 |
|
55 |
| - <template v-else> |
56 |
| - <div :class="{ 'vjs__not__lastIndex': index !== lastIndex }"> |
57 |
| - <span v-if="isObject(parentData)">{{ index }}:</span> |
58 |
| - <!-- data 可能为 null, 因此界面展示转为字符串 --> |
59 |
| - <span :class="getValueClass(data)">{{ data + '' }}</span> |
60 |
| - </div> |
61 |
| - </template> |
| 53 | + <simple-text |
| 54 | + v-else |
| 55 | + :parentDataType="getDataType(parentData)" |
| 56 | + :dataType="getDataType(data)" |
| 57 | + :text="data + ''" |
| 58 | + :notLastKey="notLastKey" |
| 59 | + :currentKey="currentKey"> |
| 60 | + </simple-text> |
62 | 61 | </div>
|
63 | 62 | </template>
|
64 | 63 |
|
65 | 64 | <script>
|
| 65 | + import SimpleText from './simple-text' |
66 | 66 | import Checkbox from './checkbox'
|
67 | 67 | import BracketsLeft from './brackets-left'
|
68 | 68 | import BracketsRight from './brackets-right'
|
69 | 69 |
|
70 | 70 | export default {
|
71 | 71 | name: 'vue-json-pretty',
|
72 | 72 | components: {
|
| 73 | + SimpleText, |
73 | 74 | Checkbox,
|
74 | 75 | BracketsLeft,
|
75 | 76 | BracketsRight
|
|
112 | 113 | type: Number,
|
113 | 114 | default: 1
|
114 | 115 | },
|
115 |
| - index: {} |
| 116 | + // 当前树的数据 data 为数组时 currentKey 表示索引, 为对象时表示键名 |
| 117 | + currentKey: [Number, String] |
116 | 118 | },
|
117 | 119 | data () {
|
118 | 120 | return {
|
|
123 | 125 | },
|
124 | 126 | computed: {
|
125 | 127 | // 获取当前 data 中最后一项的 key 或 索引, 便于界面判断是否添加 ","
|
126 |
| - lastIndex () { |
| 128 | + lastKey () { |
127 | 129 | if (Array.isArray(this.parentData)) {
|
128 | 130 | return this.parentData.length - 1
|
129 | 131 | } else if (this.isObject(this.parentData)) {
|
130 | 132 | let arr = Object.keys(this.parentData)
|
131 | 133 | return arr[arr.length - 1]
|
132 | 134 | }
|
133 | 135 | },
|
| 136 | + // 是否不是最后一项 |
| 137 | + notLastKey () { |
| 138 | + return this.currentKey !== this.lastKey |
| 139 | + }, |
134 | 140 | // 当前的树是否支持选中功能
|
135 | 141 | selectable () {
|
136 | 142 | return this.pathSelectable(this.path, this.data)
|
|
167 | 173 | },
|
168 | 174 | // 工具函数: 判断是否对象
|
169 | 175 | isObject (value) {
|
170 |
| - return Object.prototype.toString.call(value).slice(8, -1).toLowerCase() === 'object' |
| 176 | + return this.getDataType(value) === 'object' |
171 | 177 | },
|
172 |
| - // 简单类型数据添加 class, 定义样式 |
173 |
| - getValueClass (value) { |
174 |
| - let dataType = Object.prototype.toString.call(value).slice(8, -1).toLowerCase() |
| 178 | + // 获取数据类型 |
| 179 | + getDataType (value) { |
175 | 180 | // 若使用 typeof 会影响 webpack 压缩后体积变大
|
176 |
| - switch (dataType) { |
177 |
| - case 'null': |
178 |
| - case 'string': |
179 |
| - case 'number': |
180 |
| - case 'boolean': |
181 |
| - return `vjs__value__${dataType}` |
182 |
| - default: |
183 |
| - return '' |
184 |
| - } |
| 181 | + return Object.prototype.toString.call(value).slice(8, -1).toLowerCase() |
185 | 182 | }
|
186 | 183 | }
|
187 | 184 | }
|
|
0 commit comments