Vue之使用JsonView来展示Json树

前两天干活儿有个需求,在前端需要展示可折叠的Json树,供开发人员查看,这里采用JsonView组件来实现,它是一款用于展示Json的Vue组件,支持大体积的Json文件快速解析渲染,下面记录一下实现过程。

1.首先先下载好JsonView的组件:JsonView.vue,组件代码如下:

  1 <template>
  2   <div class="bgView">
  3     <div :class="[‘json-view‘, length ? ‘closeable‘ : ‘‘]" :style="‘font-size:‘ + fontSize+‘px‘">
  4     <span @click="toggleClose" :class="[‘angle‘, innerclosed ? ‘closed‘ : ‘‘]" v-if="length">
  5     </span>
  6       <div class="content-wrap">
  7         <p class="first-line">
  8           <span v-if="jsonKey" class="json-key">"{{jsonKey}}": </span>
  9           <span v-if="length">
 10           {{prefix}}
 11           {{innerclosed ? (‘...‘ + subfix) : ‘‘}}
 12           <span class="json-note">
 13            {{innerclosed ? (‘ // count: ‘ + length) : ‘‘}}
 14           </span>
 15         </span>
 16           <span v-if="!length">{{isArray ? ‘[]‘ : ‘{}‘}}</span>
 17         </p>
 18         <div v-if="!innerclosed && length" class="json-body">
 19           <template v-for="(item, index) in items">
 20             <json-view :closed="closed" v-if="item.isJSON" :key="index" :json="item.value"
 21                        :jsonKey="item.key" :isLast="index === items.length - 1"></json-view>
 22             <p class="json-item" v-else :key="index">
 23             <span class="json-key">
 24                 {{(isArray ? ‘‘ : ‘"‘ + item.key + ‘"‘)}}
 25             </span>
 26               :
 27               <span class="json-value">
 28                 {{item.value + (index === items.length - 1 ? ‘‘ : ‘,‘)}}
 29             </span>
 30             </p>
 31           </template>
 32           <span v-show="!innerclosed" class="body-line"></span>
 33         </div>
 34         <p v-if="!innerclosed && length" class="last-line">
 35           <span>{{subfix}}</span>
 36         </p>
 37       </div>
 38     </div>
 39   </div>
 40 </template>
 41
 42 <script>
 43   export default {
 44     name: ‘jsonView‘,
 45     props: {
 46       json: [Object, Array],
 47       jsonKey: {
 48         type: String,
 49         default: ‘‘
 50       },
 51       closed: {
 52         type: Boolean,
 53         default: false
 54       },
 55       isLast: {
 56         type: Boolean,
 57         default: true
 58       },
 59       fontSize: {
 60         type: Number,
 61         default: 13
 62       }
 63     },
 64     created() {
 65       this.innerclosed = this.closed
 66       this.$watch(‘closed‘, () => {
 67         this.innerclosed = this.closed
 68       })
 69     },
 70     data() {
 71       return {
 72         innerclosed: true
 73       }
 74     },
 75     methods: {
 76       isObjectOrArray(source) {
 77         const type = Object.prototype.toString.call(source)
 78         const res = type === ‘[object Array]‘ || type === ‘[object Object]‘
 79         return res
 80       },
 81       toggleClose() {
 82         if (this.innerclosed) {
 83           this.innerclosed = false
 84         } else {
 85           this.innerclosed = true
 86         }
 87       }
 88     },
 89     computed: {
 90       isArray() {
 91         return Object.prototype.toString.call(this.json) === ‘[object Array]‘
 92       },
 93       length() {
 94         return this.isArray ? this.json.length : Object.keys(this.json).length
 95       },
 96       subfix() {
 97         return (this.isArray ? ‘]‘ : ‘}‘) + (this.isLast ? ‘‘ : ‘,‘)
 98       },
 99       prefix() {
100         return this.isArray ? ‘[‘ : ‘{‘
101       },
102       items() {
103         if (this.isArray) {
104           return this.json.map(item => {
105             const isJSON = this.isObjectOrArray(item)
106             return {
107               value: isJSON ? item : JSON.stringify(item),
108               isJSON,
109               key: ‘‘
110             }
111           })
112         }
113         const json = this.json
114         return Object.keys(json).map(key => {
115           const item = json[key]
116           const isJSON = this.isObjectOrArray(item)
117           return {
118             value: isJSON ? item : JSON.stringify(item),
119             isJSON,
120             key
121           }
122         })
123       }
124     }
125   }
126 </script>
127
128 <style>
129   .bgView {
130     background-color: #fafafa;
131   }
132
133   .json-view {
134     position: relative;
135     display: block;
136     width: 100%;
137     height: 100%;
138     white-space: nowrap;
139     padding-left: 20px;
140     box-sizing: border-box;
141   }
142
143   .json-note {
144     color: #909399;
145   }
146
147   .json-key {
148     color: rgb(147, 98, 15);
149   }
150
151   .json-value {
152     color: rgb(24, 186, 24);
153   }
154
155   .json-item {
156     margin: 0;
157     padding-left: 20px;
158   }
159
160   .first-line {
161     padding: 0;
162     margin: 0;
163   }
164
165   .json-body {
166     position: relative;
167     padding: 0;
168     margin: 0;
169   }
170
171   .json-body .body-line {
172     position: absolute;
173     height: 100%;
174     width: 0;
175     border-left: dashed 1px #bbb;
176     top: 0;
177     left: 2px;
178   }
179
180   .last-line {
181     padding: 0;
182     margin: 0;
183   }
184
185   .angle {
186     position: absolute;
187     display: block;
188     cursor: pointer;
189     float: left;
190     width: 20px;
191     text-align: center;
192     left: 0;
193   }
194
195   .angle::after {
196     content: "";
197     display: inline-block;
198     width: 0;
199     height: 0;
200     vertical-align: middle;
201     border-top: solid 4px #333;
202     border-left: solid 6px transparent;
203     border-right: solid 6px transparent;
204   }
205
206   .angle.closed::after {
207     border-left: solid 4px #333;
208     border-top: solid 6px transparent;
209     border-bottom: solid 6px transparent;
210   }
211 </style>

2.在需要使用的vue页面中引用JsonView组件

import JsonView from ‘@/components/JsonView‘

3.定义Json数据变量

jsonData:{},

4.页面展示代码

<JsonView :json="jsonData"></JsonView>

5.实现效果如下:

JsonView Attributes

打完收工!

原文地址:https://www.cnblogs.com/ailanlan/p/12069659.html

时间: 2024-08-30 03:48:04

Vue之使用JsonView来展示Json树的相关文章

Vue ----------- 了解, 展示json 数据

Vue.js  是一套构建用户界面的渐进式框架. 优点: 与大型框架不同的是采用自底向上的增量开发的设计, 只聚焦于视图层,不仅易于上手,还便于与第三方库或既有项目整合 当与现代化工具链以及各种类库结合使用时,也完全能为复杂的单页应用提供驱动 Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统 使用vue展示json对象中数据 特点:采用声明式渲染  .使用需要实例化 看起来这跟渲染一个字符串模板非常类似,但是 Vue 在背后做了大量工作. 现在数据和 DOM

jquery: json树组数据输出到表格Dom树的处理方法

项目背景 项目中需要把表格重排显示 处理方法 思路主要是用历遍Json数组把json数据一个个append到4个表格里,还要给每个单元格绑定个单击弹出自定义对话框,表格分了单双行,第一行最后还要改rowspan呵呵,程序还没优化运行正常先给客户展示先:) 1,表格数据->json数组 var keyArr = new Array(); var jsonArr = new Array(); $list.find("thead th").each(function () { keyA

html页面展示Json样式

一般有些做后台数据查询,要把后台返回json数据展示到页面上,如果需要展示样式更清晰.直观.一目了然,就要用到html+css+js实现这个小功能 一.css代码 pre {outline: 1px solid #ccc; } .string { color: green; } .number { color: darkorange; } .boolean { color: blue; } .null { color: magenta; } .key { color: red; } 二.html

JSON树节点的增删查改

最近了解到使用json字符串存到数据库的一种存储方式,取出来的json字符串可以进行相应的节点操作 故借此机会练习下递归,完成对json节点操作对应的工具类. 介绍一下我使用的依赖 <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</versio

怎样通过已经使用过webpack+vue+vueRouter+vuecli的配置文件package.json创建一个项目

首先,我们自己得手动创建一个webpack+vue+vueRouter+vuecli工程,执行下面:如:新建一个vue项目,创建一个基于"webpack"的项目,项目名为vuedemo: $ vue init webpack vuedemo 安装完成后进入工程名称再根据原来项目的配置文件初始化 $ cd vuedemo $ npm install 但是由于在新建的时候对eslint的选择中选择了Yes,所以后面根据配置package.json的时候,发现没有eslint-friendl

json转json树状结构

在写Restful服务的时候,因为都是返回去的都是一条条的json对象,这些都对象之间又是有关系的,但是却没有很好的体现出来,返回到页面的时候,不能成为一个树.返回去的json串数据如下: [{"belongsname":"","id":901,"isleaf":0,"name":"XJBHX-2标项目部","pid":"","type&

在html页面中展示JSON

背景:有时候我们需要将json数据直接显示在页面上(比如在做一个接口测试的项目,需要将接口返回的结果直接展示),但是如果直接显示字符串,不方便查看.需要格式化一下. 解决方案:其实JSON.stringify本身就可以将JSON格式化,具体的用法是: JSON.stringify(res, null, 2); //res是要JSON化的对象,2是spacing 如果想要效果更好看,还要加上格式化的代码和样式: js代码: function syntaxHighlight(json) { if (

vue中的axios.post使用json数据传输,出现请求头字段内容类型是不被允许的情况的解决方案

问题描述: 由于restful接口需要在头部header传递两个字段: Content-Type: application/jsonAccess-Token: 84c6635800b14e0eba4f7ece65e095a1 但是,在vue.js里面配置: 执行发送的时候出现: 意思是预发请求的时候不通过,不再正式发请求 经过反复的测试,发现,header里面包含自定义字段,浏览器是会先发一次options请求,如果请求通过,则继续发送正式的post请求,而如果不通过则返回以上错误 那这样就只能

如何递归将Json树, 显示在HTML上

{ id: "1", ProjectId: "4069", TaskId: "8", TaskName: "你好", T_BeginDate: "", T_EndDate: "", T_Duration: "0.00", T_Percent: "0", T_ParentId: "7", LinkProjectId: "