mirror of
https://github.com/tenrok/vue-json-viewer.git
synced 2026-06-20 20:00:37 +03:00
add an option to enable key sorting before display
This commit is contained in:
@@ -4,20 +4,25 @@ Simple json display component, based on vue
|
|||||||
|
|
||||||
## Installing:
|
## Installing:
|
||||||
Using npm:
|
Using npm:
|
||||||
```
|
```
|
||||||
$ npm install vue-json-viewer
|
$ npm install vue-json-viewer
|
||||||
```
|
```
|
||||||
Using bower:
|
Using bower:
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
$ yarn add vue-json-viewer
|
$ yarn add vue-json-viewer
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
|
|
||||||
``` html
|
``` html
|
||||||
<json-viewer :value="jsonData" :show-copy="true" icon-prefix="ion" :show-bigger="true"></json-viewer>
|
<json-viewer
|
||||||
|
:value="jsonData"
|
||||||
|
:show-copy="true"
|
||||||
|
icon-prefix="ion"
|
||||||
|
:show-bigger="true"
|
||||||
|
:sort-keys="true" />
|
||||||
```
|
```
|
||||||
|
|
||||||
``` js
|
``` js
|
||||||
@@ -42,14 +47,15 @@ export default {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
## Result:
|
## Result:
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
## Options:
|
## Options:
|
||||||
|
|
||||||
| Property | Description |
|
| Property | Description |
|
||||||
| ----------- |:-------------|
|
| ----------- |:------------- |
|
||||||
| value | json data |
|
| value | json data |
|
||||||
| show-copy | display the copy button |
|
| show-copy | display the copy button |
|
||||||
| show-bigger | display the bigger button |
|
| show-bigger | display the bigger button |
|
||||||
| icon-prefix | Custom Font icon prefix |
|
| icon-prefix | Custom Font icon prefix |
|
||||||
|
| sort-keys | Sort items by key names |
|
||||||
|
|||||||
+3
-2
@@ -4,7 +4,7 @@
|
|||||||
<j-icon v-if="isObject" :type="value ? 'arrow-down-b' : 'arrow-up-b'" @click.stop="toggleNode"></j-icon>
|
<j-icon v-if="isObject" :type="value ? 'arrow-down-b' : 'arrow-up-b'" @click.stop="toggleNode"></j-icon>
|
||||||
{{keyName}}:
|
{{keyName}}:
|
||||||
</span>
|
</span>
|
||||||
<commponent :is="`Json${valueType}`" :json-value="value" v-model="toggle" :key-name="keyName"></commponent>
|
<commponent :is="`Json${valueType}`" :json-value="value" v-model="toggle" :key-name="keyName" :sort-keys="sortKeys"></commponent>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -20,7 +20,8 @@ export default {
|
|||||||
name: 'JsonBox',
|
name: 'JsonBox',
|
||||||
props: {
|
props: {
|
||||||
value: [Object, Array, String, Number, Boolean],
|
value: [Object, Array, String, Number, Boolean],
|
||||||
keyName: String
|
keyName: String,
|
||||||
|
sortKeys: Boolean
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
|||||||
+7
-3
@@ -6,7 +6,7 @@
|
|||||||
<j-icon v-if="showCopy && !copied" class="copy" type="clipboard" @click="clip"></j-icon>
|
<j-icon v-if="showCopy && !copied" class="copy" type="clipboard" @click="clip"></j-icon>
|
||||||
</div>
|
</div>
|
||||||
<div class="code-box" :class="{'more': moreCode}">
|
<div class="code-box" :class="{'more': moreCode}">
|
||||||
<json-box :value="value" :key-name="keyName"></json-box>
|
<json-box :value="value" :key-name="keyName" :sort-keys="sortKeys"></json-box>
|
||||||
</div>
|
</div>
|
||||||
<div class="more-code" @click="toggleMoreCode">
|
<div class="more-code" @click="toggleMoreCode">
|
||||||
<j-icon :type="moreCode ? 'ios-arrow-up' : 'ios-arrow-down'"></j-icon>
|
<j-icon :type="moreCode ? 'ios-arrow-up' : 'ios-arrow-down'"></j-icon>
|
||||||
@@ -38,6 +38,10 @@ export default {
|
|||||||
iconPrefix: {
|
iconPrefix: {
|
||||||
type: String,
|
type: String,
|
||||||
default: ''
|
default: ''
|
||||||
|
},
|
||||||
|
sortKeys: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
provide() {
|
provide() {
|
||||||
@@ -105,7 +109,7 @@ export default {
|
|||||||
margin-bottom: 15px;
|
margin-bottom: 15px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.more-code {
|
.more-code {
|
||||||
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
|
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -148,7 +152,7 @@ export default {
|
|||||||
font-size: 18px;
|
font-size: 18px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.j-icon {
|
.j-icon {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-3
@@ -2,8 +2,8 @@
|
|||||||
<span>
|
<span>
|
||||||
<j-icon v-if="!keyName" :type="value ? 'arrow-down-b' : 'arrow-up-b'" @click.stop="toggle"></j-icon>
|
<j-icon v-if="!keyName" :type="value ? 'arrow-down-b' : 'arrow-up-b'" @click.stop="toggle"></j-icon>
|
||||||
<span>{</span>
|
<span>{</span>
|
||||||
<template v-if="Object.keys(jsonValue).length">
|
<template v-if="Object.keys(ordered).length">
|
||||||
<json-box v-show="value" v-for="(v, k) in jsonValue" :key="k" :key-name="k" :value="v"></json-box>
|
<json-box v-show="value" v-for="(v, k) in ordered" :key="k" :key-name="k" :value="v"></json-box>
|
||||||
<span v-show="!value" class="node-ellipsis">...</span>
|
<span v-show="!value" class="node-ellipsis">...</span>
|
||||||
</template>
|
</template>
|
||||||
<span>}</span>
|
<span>}</span>
|
||||||
@@ -17,7 +17,19 @@ export default {
|
|||||||
props: {
|
props: {
|
||||||
jsonValue: Object,
|
jsonValue: Object,
|
||||||
keyName: String,
|
keyName: String,
|
||||||
value: Boolean
|
value: Boolean,
|
||||||
|
sortKeys: Boolean
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
ordered () {
|
||||||
|
const ordered = {};
|
||||||
|
if (this.sortKeys) {
|
||||||
|
Object.keys(this.jsonValue).sort().forEach(function(key) {
|
||||||
|
ordered[key] = this.jsonValue[key];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return ordered;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
toggle() {
|
toggle() {
|
||||||
|
|||||||
Reference in New Issue
Block a user