diff --git a/README.md b/README.md index ac61fd8..dee5b8e 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ export default { | showSelectController | higher | whether to show the select controller at left | boolean | false | | selectOnClickNode | higher | whether to change selected value when click node | boolean | true | | highlightSelectedNode | higher | highlight current node when selected | boolean | true | +| 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 | ## Events diff --git a/example/App.vue b/example/App.vue index a69d41e..eb63e89 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,13 @@ export default { }, handleChange (newVal, oldVal) { console.log('newVal: ', newVal, ' oldVal: ', oldVal) + }, + customLinkFormatter(data) { + if (data.startsWith('http://')) { + return `"${data}"`; + } else { + return null; + } } } } diff --git a/package.json b/package.json index e9b04f0..c141761 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-json-pretty", - "version": "1.6.3", + "version": "1.6.4", "description": "A JSON tree view component that is easy to use and also supports data selection.", "author": "leezng ", "main": "vue-json-pretty.js", 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,21 @@ } }, 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) { + return this.customValueFormatter( + data, this.currentKey, this.parentData + ) || this.defaultFormatter(data) + } else { + return this.defaultFormatter(data) + } } } } diff --git a/src/index.js b/src/index.js index c508e4a..eaa0854 100644 --- a/src/index.js +++ b/src/index.js @@ -2,5 +2,5 @@ import App from './components/app.vue' import './assets/less/index.less' export default Object.assign({}, App, { - version: '1.6.2' + version: '1.6.4' })