From 15d8ff7e5229ede92f8f139347e4204784cba98b Mon Sep 17 00:00:00 2001 From: David Blackman Date: Fri, 13 Mar 2020 12:57:27 -0400 Subject: [PATCH] add ability to use a custom formatter for values --- example/App.vue | 19 ++++++++++++++++++- src/components/app.vue | 7 +++++++ src/components/simple-text.vue | 20 +++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/example/App.vue b/example/App.vue index a69d41e..eb61f20 100644 --- a/example/App.vue +++ b/example/App.vue @@ -27,6 +27,10 @@
+
+
+ +
@@ -48,6 +52,7 @@ :show-line="showLine" :highlight-mouseover-node="highlightMouseoverNode" :collapsed-on-click-brackets="collapsedOnClickBrackets" + :custom-value-formatter="useCustomLinkFormatter ? customLinkFormatter : null" @click="handleClick">
@@ -169,7 +174,8 @@ export default { }, { news_id: 51183, title: 'Traffic paradise: How to design streets for people and unmanned vehicles in the future?', - source: 'Netease smart' + source: 'Netease smart', + link: 'http://netease.smart/traffic-paradise/1235' }, { news_id: 51182, title: 'Teslamask\'s American Business Relations: The government does not pay billions to build factories', @@ -187,6 +193,7 @@ export default { highlightSelectedNode: true, selectOnClickNode: true, collapsedOnClickBrackets: true, + useCustomLinkFormatter: false, path: 'res', deep: 3, itemData: {}, @@ -228,6 +235,16 @@ export default { }, handleChange (newVal, oldVal) { console.log('newVal: ', newVal, ' oldVal: ', oldVal) + }, + customLinkFormatter(data) { + console.log(data) + console.log(data.startsWith('http://')) + console.log(data.startsWith('http')) + if (data.startsWith('http://')) { + return `"${data}"`; + } else { + return null; + } } } } diff --git a/src/components/app.vue b/src/components/app.vue index 4920cc6..7206230 100644 --- a/src/components/app.vue +++ b/src/components/app.vue @@ -55,6 +55,7 @@ :collapsed-on-click-brackets="collapsedOnClickBrackets" :current-key="key" :current-deep="currentDeep + 1" + :custom-value-formatter="customValueFormatter" @click="handleItemClick" @change="handleItemChange"> @@ -71,6 +72,7 @@
- - {{ textFormatter(data) }} - +
@@ -16,7 +14,8 @@ parentData: {}, data: {}, showComma: Boolean, - currentKey: [Number, String] + currentKey: [Number, String], + customValueFormatter: Function }, computed: { // 当前数据类型 @@ -30,11 +29,22 @@ } }, methods: { - textFormatter (data) { + defaultFormatter (data) { let text = data + '' if (this.dataType === 'string') text = `"${text}"` if (this.showComma) text += ',' return text + }, + + textFormatter (data) { + if (this.customValueFormatter) { + console.log(this.customValueFormatter) + return this.customValueFormatter( + data, this.currentKey, this.parentData + ) || this.defaultFormatter(data) + } else { + return this.defaultFormatter(data) + } } } }