Skip to content

Commit d3bbeed

Browse files
committed
Version updated to 1.2.0
data 增加对简单类型的支持(数字, 布尔, 字符串) click 事件支持选中对象,也支持选中简单数据,同时悬浮优化,将括号标签并入高亮范围内 内部 props: json 更名为 parentData
1 parent dc955c5 commit d3bbeed

File tree

5 files changed

+80
-67
lines changed

5 files changed

+80
-67
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737

3838
| Attribute | Description | Type | Default |
3939
|-------- |-------- |-------- | -------- |
40-
| data | json data | string, array, object | - |
40+
| data | json data | JSON object | - |
4141
| path | root data path | string | root |
4242

4343
## Events

example/App.vue

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<template>
22
<div id="app" class="example-app">
3-
<vue-json-pretty :data="data" :path="'res'" @click="handleClick"></vue-json-pretty>
3+
<div>
4+
<h2>JSON Tree:</h2>
5+
<vue-json-pretty :data="data" :path="'res'" @click="handleClick"></vue-json-pretty>
6+
</div>
47
<div class="result">
8+
<h2>Click Result:</h2>
59
<div>path: {{itemPath}}</div>
610
<div>data: <pre>{{itemData}}</pre></div>
711
</div>
@@ -30,7 +34,8 @@ export default {
3034
c: {
3135
a: null,
3236
b: 2
33-
}
37+
},
38+
d: 123456789
3439
},
3540
itemData: {},
3641
itemPath: ''
@@ -39,29 +44,30 @@ export default {
3944
methods: {
4045
handleClick (path, data) {
4146
this.itemPath = path
42-
this.itemData = data
47+
this.itemData = !data ? data + '' : data // 处理 data = null 的情况
4348
}
4449
}
4550
}
4651
</script>
4752

4853
<style lang="less">
54+
html, body {
55+
margin: 0;
56+
}
4957
.example-app {
50-
padding: 30px;
5158
> div {
5259
float: left;
53-
padding: 15px;
60+
padding: 0 15px;
5461
width: 50%;
5562
min-height: 300px;
56-
border: 1px solid #ccc;
5763
box-sizing: border-box;
64+
&:first-child {
65+
border-right: 5px double #ccc;
66+
}
5867
}
59-
.result {
68+
.result pre{
69+
margin: 0;
6070
font-family: Consolas;
61-
pre {
62-
margin: 0;
63-
font-family: inherit;
64-
}
6571
}
6672
}
6773
</style>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-json-pretty",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "A Vue.js project",
55
"author": "leezng <[email protected]>",
66
"main": "vue-json-pretty.js",

src/components/tree.vue

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
<template>
2-
<div class="vjs__tree">
3-
<brackets-left
4-
:visiable.sync="visiable"
5-
:data="data"
6-
:index="index"
7-
:last-index="lastIndex">
8-
<span v-if="child && !Array.isArray(json)">{{ index }}:</span>
9-
</brackets-left>
10-
11-
<!-- data 为对象时, index 表示 key, 为数组才表示索引 -->
12-
<div
13-
v-for="(item, index) in data"
14-
v-show="visiable"
15-
class="vjs__tree__content"
16-
:style="{ 'background-color': treeContentBackground }"
17-
:key="index"
18-
@mouseover.stop="treeContentBackground = '#eee'"
19-
@mouseout.stop="treeContentBackground = 'transparent'"
20-
@click.stop="handleClick">
21-
<tree
22-
v-if="Array.isArray(item) || isObject(item)"
23-
:json="data"
24-
:data="item"
25-
:path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)"
2+
<div
3+
class="vjs__tree"
4+
:style="{ 'background-color': treeContentBackground }"
5+
@click.stop="handleClick"
6+
@mouseover.stop="treeContentBackground = '#eee'"
7+
@mouseout.stop="treeContentBackground = 'transparent'">
8+
<template v-if="Array.isArray(data) || isObject(data)">
9+
<!-- 左闭合 -->
10+
<brackets-left
11+
:visiable.sync="visiable"
12+
:data="data"
2613
:index="index"
27-
:last-index="getLastIndex()"
28-
:child="true"
29-
@click="handleItemClick">
30-
</tree>
14+
:last-index="lastIndex">
15+
<span v-if="child && !Array.isArray(parentData)">{{ index }}:</span>
16+
</brackets-left>
3117

32-
<div v-else :class="{ 'vjs__not__lastIndex': index !== getLastIndex() }" >
33-
<span v-if="isObject(data)">{{ index }}:</span>
34-
<span :class="getValueClass(item)">{{ `${item}` }}</span>
18+
<!-- 数据内容, data 为对象时, index 表示 key, 为数组才表示索引 -->
19+
<div
20+
v-for="(item, index) in data"
21+
v-show="visiable"
22+
class="vjs__tree__content"
23+
:key="index">
24+
<tree
25+
:parent-data="data"
26+
:data="item"
27+
:path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)"
28+
:index="index"
29+
:child="true"
30+
@click="handleItemClick">
31+
</tree>
3532
</div>
36-
</div>
3733

38-
<brackets-right
39-
:visiable.sync="visiable"
40-
:data="data"
41-
:index="index"
42-
:last-index="lastIndex">
43-
</brackets-right>
34+
<!-- 右闭合 -->
35+
<brackets-right
36+
:visiable.sync="visiable"
37+
:data="data"
38+
:index="index"
39+
:last-index="lastIndex">
40+
</brackets-right>
41+
</template>
42+
43+
<template v-else>
44+
<div :class="{ 'vjs__not__lastIndex': index !== lastIndex }">
45+
<span v-if="isObject(parentData)">{{ index }}:</span>
46+
<!-- data 可能为 null, 因此界面展示转为字符串 -->
47+
<span :class="getValueClass(data)">{{ data + '' }}</span>
48+
</div>
49+
</template>
4450
</div>
4551
</template>
4652

@@ -50,6 +56,10 @@
5056
5157
export default {
5258
name: 'tree',
59+
components: {
60+
BracketsLeft,
61+
BracketsRight
62+
},
5363
props: {
5464
/* 外部可用 START */
5565
data: {}, // 当前树的数据
@@ -59,20 +69,26 @@
5969
default: 'root'
6070
},
6171
/* 外部可用 END */
62-
json: {}, // 当前树的父级数据
72+
parentData: {}, // 当前树的父级数据
6373
child: Boolean, // 是否子树
64-
index: {},
65-
lastIndex: {}
74+
index: {}
6675
},
6776
data () {
6877
return {
6978
visiable: true,
7079
treeContentBackground: 'transparent'
7180
}
7281
},
73-
components: {
74-
BracketsLeft,
75-
BracketsRight
82+
computed: {
83+
// 获取当前 data 中最后一项的 key 或 索引, 便于界面判断是否添加 ","
84+
lastIndex () {
85+
if (Array.isArray(this.parentData)) {
86+
return this.parentData.length - 1
87+
} else if (this.isObject(this.parentData)) {
88+
let arr = Object.keys(this.parentData)
89+
return arr[arr.length - 1]
90+
}
91+
}
7692
},
7793
methods: {
7894
// 触发组件的 click 事件
@@ -87,15 +103,6 @@
87103
isObject (value) {
88104
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase() === 'object'
89105
},
90-
// 获取当前 data 中最后一项的 key 或 索引, 便于界面判断是否添加 ","
91-
getLastIndex () {
92-
if (Array.isArray(this.data)) {
93-
return this.data.length - 1
94-
} else if (this.isObject(this.data)) {
95-
let arr = Object.keys(this.data)
96-
return arr[arr.length - 1]
97-
}
98-
},
99106
getValueClass (value) {
100107
if (value === null) {
101108
return 'vjs__value__null'

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import VueJsonPretty from './components/tree.vue'
22

33
export default Object.assign({}, VueJsonPretty, {
4-
version: '1.1.1'
4+
version: '1.2.0'
55
})

0 commit comments

Comments
 (0)