Skip to content

Commit 15d8ff7

Browse files
committed
add ability to use a custom formatter for values
1 parent 41ffbe1 commit 15d8ff7

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

example/App.vue

Lines changed: 18 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,16 @@ export default {
228235
},
229236
handleChange (newVal, oldVal) {
230237
console.log('newVal: ', newVal, ' oldVal: ', oldVal)
238+
},
239+
customLinkFormatter(data) {
240+
console.log(data)
241+
console.log(data.startsWith('http://'))
242+
console.log(data.startsWith('http'))
243+
if (data.startsWith('http://')) {
244+
return `<a style="color:red;" href="${data}">"${data}"</a>`;
245+
} else {
246+
return null;
247+
}
231248
}
232249
}
233250
}

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: 15 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,22 @@
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+
console.log(this.customValueFormatter)
42+
return this.customValueFormatter(
43+
data, this.currentKey, this.parentData
44+
) || this.defaultFormatter(data)
45+
} else {
46+
return this.defaultFormatter(data)
47+
}
3848
}
3949
}
4050
}

0 commit comments

Comments
 (0)