Vue JsonView 树形格式化代码插件
<template>
<div class="bgView">
<div :class="['json-view', length ? 'closeable' : '']" :style="'font-size:' + fontSize+'px'">
<span @click="toggleClose" :class="['angle', innerclosed ? 'closed' : '']" v-if="length"></span>
<div class="content-wrap">
<p class="first-line">
<span v-if="jsonKey" class="json-key">"{{jsonKey}}": </span>
<span v-if="length">
{{prefix}}
{{innerclosed ? ('...' + subfix) : ''}}
<span class="json-note">
{{innerclosed ? (' // count: ' + length) : ''}}
</span>
</span>
<span v-if="!length">{{isArray ? '[]' : '{}'}}</span>
</p>
<div v-if="!innerclosed && length" class="json-body">
<template v-for="(item, index) in items">
<json-view :closed="closed" v-if="item.isJSON" :key="index" :json="item.value" :jsonKey="item.key"
:isLast="index === items.length - 1"></json-view>
<p class="json-item" v-else :key="index">
<span class="json-key">
{{(isArray ? '' : '"' + item.key + '"')}}
</span>
:
<span class="json-value">
{{item.value + (index ===
items.length - 1 ? '' : ',')}}
</span> </p>
</template>
<span v-show="!innerclosed" class="body-line"></span>
</div>
<p v-if="!innerclosed && length" class="last-line">
<span>{{subfix}}</span>
</p>
</div>
</div>
</div>
</template> <script>
export default {
name: 'jsonView',
props: {
json: [Object, Array],
jsonKey: {
type: String,
default: ''
},
closed: {
type: Boolean,
default: false
},
isLast: {
type: Boolean,
default: true
},
fontSize: {
type: Number,
default: 18
}
},
created() {
this.innerclosed = this.closed
this.$watch('closed', () => {
this.innerclosed = this.closed
})
},
data() {
return {
innerclosed: true
}
},
methods: {
isObjectOrArray(source) {
const type = Object.prototype.toString.call(source)
const res = type === '[object Array]' || type === '[object Object]'
return res
},
toggleClose() {
if (this.innerclosed) {
this.innerclosed = false
} else {
this.innerclosed = true
}
}
},
computed: {
isArray() {
return Object.prototype.toString.call(this.json) === '[object Array]'
},
length() {
return this.isArray ? this.json.length : Object.keys(this.json).length
},
subfix() {
return (this.isArray ? ']' : '}') + (this.isLast ? '' : ',')
},
prefix() {
return this.isArray ? '[' : '{'
},
items() {
if (this.isArray) {
return this.json.map(item => {
const isJSON = this.isObjectOrArray(item)
return {
value: isJSON ? item : JSON.stringify(item),
isJSON,
key: ''
}
})
}
const json = this.json
return Object.keys(json).map(key => {
const item = json[key]
const isJSON = this.isObjectOrArray(item)
return {
value: isJSON ? item : JSON.stringify(item),
isJSON,
key
}
})
}
}
}
</script> <style lang="scss">
.bgView {
background-color: #fafafa;
}
.json-view {
position: relative;
display: block;
width: 100%;
height: 100%;
white-space: nowrap;
padding-left: 20px;
box-sizing: border-box;
.json-note {
color: #909399;
}
.json-key {
color: rgb(147, 98, 15);
}
.json-value {
color: rgb(24, 186, 24);
}
.json-item {
margin: 0;
padding-left: 20px; }
.first-line {
padding: 0;
margin: 0;
}
.json-body {
position: relative;
padding: 0;
margin: 0; .body-line {
position: absolute;
height: 100%;
width: 0;
border-left: dashed 1px #bbb;
top: 0;
left: 2px;
}
}
.last-line {
padding: 0;
margin: 0;
}
.angle {
position: absolute;
display: block;
cursor: pointer;
float: left;
width: 20px;
text-align: center;
left: 0; &::after {
content: "";
display: inline-block;
width: 0;
height: 0;
vertical-align: middle;
border-top: solid 4px #333;
border-left: solid 6px transparent;
border-right: solid 6px transparent;
}
&.closed::after {
border-left: solid 4px #333;
border-top: solid 6px transparent;
border-bottom: solid 6px transparent;
}
}
}
</style>
使用实例
<template>
<div>
<JsonView :json="JsonData"></JsonView>
</div>
</template> <script>
import JsonView from '@/components/JsonView' export default {
name: 'test',
data() {
return {
JsonData: {
'code': 200,
'message': 'succeed !',
'data': [
{
'uuid': '76254DDB-A8EA-46CB-B3D7-6EEBD13BB2E6',
'version': 1,
'code': '401',
'message': '请求无权限',
'createId': 'dev',
'createDate': '2018-12-03T00:00:00',
'modifyId': null,
'modifyDate': null
},
{
'uuid': 'B0415CC2-F0E0-4B0C-A3BA-50ABAEE98BB9',
'version': 1,
'code': '500',
'message': '服务器错误',
'createId': 'dev',
'createDate': '2018-12-03T00:00:00',
'modifyId': null,
'modifyDate': null
},
{
'uuid': 'B70692E0-CCB7-4C44-B59B-7B75B16FA9FE',
'version': 1,
'code': '200',
'message': '请求成功',
'createId': 'dev',
'createDate': '2018-12-03T15:06:54.717',
'modifyId': null,
'modifyDate': null
},
{
'uuid': 'C8A37C2D-0842-423B-AEBA-976C106A3E90',
'version': 1,
'code': '202',
'message': '请求失败',
'createId': 'dev',
'createDate': '2018-12-03T00:00:00',
'modifyId': null,
'modifyDate': null
}
]
}
}
},
components: { JsonView }
}
</script>
可传参数:
json: [Object, Array], // 必传 显示的数据
closed: { // 是否默认展开
type: Boolean,
default: false
},
fontSize: { // 文字大小
type: Number,
default: 18
}
Vue JsonView 树形格式化代码插件的更多相关文章
- vscode格式化代码插件Beautify
vscode格式化代码安装 VsCode 格式化代码插件搜索并安装 Beautify 格式化代码插件使用:打开要格式化的文件 —> F1 —> Beautify file —> 选择 ...
- sublime自动格式化代码插件HTML-CSS-JS Prettify安装
sublime自动格式化代码插件HTML-CSS-JS Prettify安装 问题: 用 Sublime Text 格式化代码(安装 HTML-CSS-JS Prettify 插件)时,格式化时却会提 ...
- sublime text3安装格式化代码插件
1.代码提示插件:sublimeCodeIntel a)在Sublime Text 3中,按下Ctrl+Shift+P调出命令面板;b)输入install 调出 Install Package 选项并 ...
- vsCode 设置vue 保存自动格式化代码
setting { // vscode默认启用了根据文件类型自动设置tabsize的选项 "editor.detectIndentation": false, // 重新设定tab ...
- Qt Creator配置clang-format格式化代码插件
clang-format是一种格式化代码的插件,可用于格式化C / C ++ / Java / JavaScript / Objective-C / Protobuf / C#代码.而Qt Creat ...
- UE编辑器加载格式化代码插件astyle
UE 的格式化功能不强,自带的astyle版本陈旧,一般采用开源工具astyle来实现代码格式化. 1. 首先下载最新的astyle,因为ue自带的astyle版本太老,不支持空格.中文名等. 2. ...
- vscode vue eslint 快捷键格式化代码
添加vetur , eslint插件 在工作区添加以下代码 "workbench.startupEditor": "welcomePage", &quo ...
- phpstorm通过FileWatchers配置自动格式化代码插件
在自动格式代码的插件中, prettier一直是挺不错的, 这个插件在不同的IDE里有不同的配置地方, 但是配置参数基本上是差不多的. 下面就说明下在phpstorm(版本2019.2)中如何配置的吧 ...
- Sublime text 3 格式化代码 插件
JsFormat: 重新打开sublime就能使用js格式化插件 使用方法: 1.快捷键:ctrl+alt+f 2.或者先用快捷键打开命令面板 “ctrl + shift + p”, 再输入 “For ...
随机推荐
- JS代码引用位置问题-转
看到很多JS代码全部放在head中的情况,其实这是个细节问题.转载一个知乎用户于江水的答案: 作者:于江水链接:https://www.zhihu.com/question/34147508/answ ...
- 单片机显示原理(LCD1602)
一.接口 LCD1602是很多单片机爱好者较早接触的字符型液晶显示器,它的主控芯片是HD44780或者其它兼容芯片.与此相仿的是LCD12864液晶显示器,它是一种图形点阵显示器,能显示的内容比LCD ...
- 使用enca进行字符集转码
在linux进行开发与运维的时候,我们常常遇到字符编码的问题,系统字符设置.vimrc fileencoding设置.终端设置往往搞的晕头转向,当一个文件出现乱码的时候,我们通常不能识别它是什么编码的 ...
- Github 协同开发
ithub开发流程 Github的流程.也就是: 开发者各自fork项目的repo到自己Github账户下 每次开发同步到项目的repo然后再进行开发 push自己的开发分支到自己Github账户下面 ...
- Android.mk宏定义demo【转】
本文转载自:http://blog.csdn.net/u010164190/article/details/72783963 1.Android.mk LOCAL_PATH := $(call my ...
- 配置win2008防火墙 允许被Ping的设置方法
出于安全因素考虑,在 Windows Server 2008 上是不允许从外部对其执行 Ping 指令的 如果要配置允许被 Ping 通过以往的设置步骤会发现并不能从 Windows firewall ...
- php mktime和strtotime
本文章来给各位同学介绍一下利用php用strtotime或mktime指定日期数据(本周,上周,本月,上月,本季度)实例,希望对各位同学会有所帮助呀. strtotime定义和用法 strtotime ...
- putty和xshell使用和免密登录
putty和xshell使用和免密登录 XSHELL的设置 事前:我们先去关闭防火墙和selinux 关闭防火墙: ufw disable 再去看看selinux 一.查看SELinux状态命令: ...
- [Luogu1273] 有线电视网
[Luogu1273] 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树 ...
- C++批量加载动态库函数方法
1.枚举定义enum { // 0 - GigE DLL (implicitly called) Func_isVersionCompliantDLL, Func_isDriver ...