Skip to content

Commit 911370e

Browse files
author
leezng
authored
Merge pull request #70 from leezng/dev
release 1.6.4
2 parents e5ecc03 + 23faf93 commit 911370e

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default {
6868
| showSelectController | higher | whether to show the select controller at left | boolean | false |
6969
| selectOnClickNode | higher | whether to change selected value when click node | boolean | true |
7070
| highlightSelectedNode | higher | highlight current node when selected | boolean | true |
71+
| customValueFormatter | higher | a function that can return different html or strings to display for values in the data. If it returns null or empty, the default formatter is used | Function | false |
7172

7273
## Events
7374

example/App.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<div>
2828
<label>collapsedOnClickBrackets</label>
2929
<input type="checkbox" v-model="collapsedOnClickBrackets">
30+
</div>
31+
<div>
32+
<label>use custom link formatter</label>
33+
<input type="checkbox" v-model="useCustomLinkFormatter">
3034
</div>
3135
<div>
3236
<label>deep</label>
@@ -48,6 +52,7 @@
4852
:show-line="showLine"
4953
:highlight-mouseover-node="highlightMouseoverNode"
5054
:collapsed-on-click-brackets="collapsedOnClickBrackets"
55+
:custom-value-formatter="useCustomLinkFormatter ? customLinkFormatter : null"
5156
@click="handleClick">
5257
</vue-json-pretty>
5358
</div>
@@ -169,7 +174,8 @@ export default {
169174
}, {
170175
news_id: 51183,
171176
title: 'Traffic paradise: How to design streets for people and unmanned vehicles in the future?',
172-
source: 'Netease smart'
177+
source: 'Netease smart',
178+
link: 'http://netease.smart/traffic-paradise/1235'
173179
}, {
174180
news_id: 51182,
175181
title: 'Teslamask\'s American Business Relations: The government does not pay billions to build factories',
@@ -187,6 +193,7 @@ export default {
187193
highlightSelectedNode: true,
188194
selectOnClickNode: true,
189195
collapsedOnClickBrackets: true,
196+
useCustomLinkFormatter: false,
190197
path: 'res',
191198
deep: 3,
192199
itemData: {},
@@ -228,6 +235,13 @@ export default {
228235
},
229236
handleChange (newVal, oldVal) {
230237
console.log('newVal: ', newVal, ' oldVal: ', oldVal)
238+
},
239+
customLinkFormatter(data) {
240+
if (data.startsWith('http://')) {
241+
return `<a style="color:red;" href="${data}">"${data}"</a>`;
242+
} else {
243+
return null;
244+
}
231245
}
232246
}
233247
}

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.6.3",
3+
"version": "1.6.4",
44
"description": "A JSON tree view component that is easy to use and also supports data selection.",
55
"author": "leezng <im.leezng@gmail.com>",
66
"main": "vue-json-pretty.js",

src/components/app.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
:collapsed-on-click-brackets="collapsedOnClickBrackets"
5656
:current-key="key"
5757
:current-deep="currentDeep + 1"
58+
:custom-value-formatter="customValueFormatter"
5859
@click="handleItemClick"
5960
@change="handleItemChange">
6061
</vue-json-pretty>
@@ -71,6 +72,7 @@
7172

7273
<simple-text
7374
v-else
75+
:custom-value-formatter="customValueFormatter"
7476
:show-double-quotes="showDoubleQuotes"
7577
:show-comma="notLastKey"
7678
:parent-data="parentData"
@@ -172,6 +174,11 @@
172174
type: Boolean,
173175
default: true
174176
},
177+
// custom formatter for values
178+
customValueFormatter: {
179+
type: Function,
180+
default: null
181+
},
175182
/* outer props */
176183
177184
/* inner props */

src/components/simple-text.vue

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<template>
22
<div>
33
<slot></slot>
4-
<span :class="`vjs-value vjs-value__${dataType}`">
5-
{{ textFormatter(data) }}
6-
</span>
4+
<span :class="`vjs-value vjs-value__${dataType}`" v-html="textFormatter(data)"/>
75
</div>
86
</template>
97

@@ -16,7 +14,8 @@
1614
parentData: {},
1715
data: {},
1816
showComma: Boolean,
19-
currentKey: [Number, String]
17+
currentKey: [Number, String],
18+
customValueFormatter: Function
2019
},
2120
computed: {
2221
// 当前数据类型
@@ -30,11 +29,21 @@
3029
}
3130
},
3231
methods: {
33-
textFormatter (data) {
32+
defaultFormatter (data) {
3433
let text = data + ''
3534
if (this.dataType === 'string') text = `"${text}"`
3635
if (this.showComma) text += ','
3736
return text
37+
},
38+
39+
textFormatter (data) {
40+
if (this.customValueFormatter) {
41+
return this.customValueFormatter(
42+
data, this.currentKey, this.parentData
43+
) || this.defaultFormatter(data)
44+
} else {
45+
return this.defaultFormatter(data)
46+
}
3847
}
3948
}
4049
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import App from './components/app.vue'
22
import './assets/less/index.less'
33

44
export default Object.assign({}, App, {
5-
version: '1.6.2'
5+
version: '1.6.4'
66
})

0 commit comments

Comments
 (0)