Code Monkey home page Code Monkey logo

vue-json-pretty's Introduction

A Vue component for rendering JSON data as a tree structure.

Now it supports Vue3 at least. If you still use Vue2, see 1.x.

Build Status npm package GitHub license Sizes NPM downloads Issues

English | 简体中文

Features

  • As a JSON Formatter.
  • Written in TypeScript, support d.ts.
  • Support get item data from JSON.
  • Support big data.
  • Support editable.

Environment Support

  • Modern browsers, Electron and Internet Explorer 11 (with polyfills)
  • Server-side Rendering
IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
Electron
Electron
IE11, Edge last 10 versions last 10 versions last 10 versions last 2 versions

Using NPM or Yarn

$ npm install vue-json-pretty --save
$ yarn add vue-json-pretty

Use Vue2

$ npm install vue-json-pretty@v1-latest --save

Usage

The CSS file is included separately and needs to be imported manually. You can either import CSS globally in your app (if supported by your framework) or directly from the component.

<template>
  <div>
    <vue-json-pretty :data="{ key: 'value' }" />
  </div>
</template>

<script>
import VueJsonPretty from 'vue-json-pretty';
import 'vue-json-pretty/lib/styles.css';

export default {
  components: {
    VueJsonPretty,
  },
};
</script>

Use Nuxt.js

  1. In plugins/vue-json-pretty.js
import Vue from 'vue'
import VueJsonPretty from 'vue-json-pretty'

Vue.component("vue-json-pretty", VueJsonPretty)
  1. In nuxt.config.js
css: [
  'vue-json-pretty/lib/styles.css'
],
plugins: [
  '@/plugins/vue-json-pretty'
],

Props

Property Description Type Default
data(v-model) JSON data, support v-model when use editable JSON object -
collapsedNodeLength Objects or arrays which length is greater than this threshold will be collapsed number -
deep Paths greater than this depth will be collapsed number -
showLength Show the length when collapsed boolean false
showLine Show the line boolean true
showLineNumber Show the line number boolean false
showIcon Show the icon boolean false
showDoubleQuotes Show doublequotes on key boolean true
virtual Use virtual scroll boolean false
height The height of list when using virtual number 400
itemHeight The height of node when using virtual number 20
selectedValue(v-model) Selected data path string, array -
rootPath Root data path string root
nodeSelectable Defines whether a node supports selection (node) => boolean -
selectableType Support path select, default none multiple | single -
showSelectController Show the select controller boolean false
selectOnClickNode Trigger select when click node boolean true
highlightSelectedNode Support highlighting selected nodes boolean true
collapsedOnClickBrackets Support click brackets to collapse boolean true
renderNodeKey render node key, or use slot #renderNodeKey ({ node, defaultKey }) => vNode -
renderNodeValue render node value, or use slot #renderNodeValue ({ node, defaultValue }) => vNode -
editable Support editable boolean false
editableTrigger Trigger click | dblclick click
theme Sets the theme of the component. Options are 'light' or 'dark', with dark mode enhancing visibility on dark backgrounds 'light' | 'dark' light

Events

Event Name Description Parameters
nodeClick triggers when click node (node: NodeData)
bracketsClick triggers when click brackets (collapsed: boolean, node: NodeData)
iconClick triggers when click icon (collapsed: boolean, node: NodeData)
selectedChange triggers when the selected value changed (newVal, oldVal)

Slots

Slot Name Description Parameters
renderNodeKey render node key { node, defaultKey }
renderNodeValue render node value { node, defaultValue }

Contributors

vue-json-pretty's People

Contributors

blackmad avatar boukadam avatar cstuncsik avatar dan0xe avatar dependabot[bot] avatar fredrivett avatar henrikekblad avatar hsiaosiyuan0 avatar leezng avatar luhis avatar lukefinch avatar mawi1512 avatar mutdmour avatar nkreiger avatar rchl avatar souljorje avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-json-pretty's Issues

数字不能选中?

为啥数字不能选中啊?设计如此,还是? 或者是有配置项可以配置吗?
image

Not Working on Nuxt + Vuejs App

I installed this script in my website that is made using nuxt and vuejs but when i install your package my page shows a 404 error.

Can anyone know why?

data的排序问题

在使用中发现会对data中的下级对象数据根据key来排序,是否可以增加参数来控制是否需要排序?现在哪里的代码是对data进行排序的,我没有发现,能否告知?谢谢

Feature: Highlight selected item in tree mode

Hi @leezng

I've seen this pull request #14 is closed since "selectedPath" is conflicting with "selectedType".

However, I would like to see this feature with a configurable option.

You can let the user choose whether they want to highlight the selected item or not in both cases (tree or checkbox view).

What's your opinion on this idea? If you think this makes sense, let me know I will make a new pull request with this feature.

Cheers,
Ruby.

建议加个一键展开功能

设置层级以后,json数据量很大的话,一个个点击展开非常麻烦,建议增加一键展开和一键叠起功能。

Text Formatter

Guys how i can maked values with types string wrapped by ' single quotes.

How to increase the duplicate content of the double click event

I want add a event: v-on:dblclick.native="handleCopy" copy the column json data
if json like this:

{
  "a": "123",
  "b": "234",
  "c": "345"
}

when i double click "c": "345" column, i will get the data of 345

i was do like this:

<vue-json-pretty
          :data="message"
          selectableType="tree"
          v-on:dblclick.native="handleCopy"
          ></vue-json-pretty>
handleCopy(path, data) {
      console.info(data)
    },

but the console log is undefined.

是不是可以把这个click和doubleclick的 event也给用户暴露出来,定制自己的双击复制功能。

Separate CSS from JS?

Just for convenience, instead of overriding the default CSS when necessary, it'd be nice if it can be imported separately. (SCSS or CSS, w/e)

I prefer control over styling, rather than just having it thrown into DOM as is.

Two options here... But perhaps the best would be:

  • Create a bundled output, that includes both JS and CSS and create another output where it has CSS and JS separately in build.

能否移除.stop修饰符?

我把vue-json-pretty放在了element的下拉选择的el-option组件中,结果点击vue-json-pretty无法冒泡让el-select选中相应的值,因为被vue-json-pretty阻止冒泡了。如果开发者需要阻止冒泡,他可以选择在click事件上加上.stop修饰符。

Click handler should have parameter to identify the source component

When you have multiple vue-json-pretty components on the page (say, created in a for loop), the click handler does not know which one is causing this event.

Consider this scenario: there are multiple json structures on the page, each one with same schema, but from different server. Whenever you click on a node in one of these, you should contact the respective server for more details on that node.

Currently it is not possible to do this, since the click-handler does not give back any information about the source component where the event is raised.

Also, is there anyway this vue-json-pretty could hold some user data attached with each of its instance (at the component level at the top) that it will supply back to the click handler? For example, would like to associate the server url with each vue-json-pretty instance, so that in the click handler we can:

<panel v-for="(server) in servers">
  <vue-json-pretty :data="svcDetails[server]" @click="onDetailsClick" :attachData="server"></vue-json-pretty>
</panel>

onDetailsClick: function(path, value, attachedData) { 
    const server = attachedData; // get the attached data
    axios.post(server, body: value);  // use the currently selected value with attached data somehow
}

Feature request: emit an event whenever visibility changes

I'm using this component inside a long list of items, so I'm using https://github.com/Akryum/vue-virtual-scroller to improve performance. I'm using its DynamicScroller which supports variable height entries, but which must be notified when an item's height changes. It would be useful for vue-json-pretty to emit an event when the user clicks on brackets to toggle visibility of nested data.

I took a look at the source; I see that the brackets mixin emits an 'update:visible' event to allow binding sync, but it looks like vue doesn't bubble that event up outside of vue-json-pretty, as I can't seem to bind them in my code.

FR: current key as className

I would like to be able to add specific css to certain nodes from a certain JSON key.

So for example, all text nodes are now vjs-value vjs-value__string but I'd like it when I use the following JSON:

stackTrace: 'trace here with lots of line endings and very unreadable if they are ignore'

That it would turn into vjs-value vjs-value__string stackTrace. Or maybe vjs-key-stackTrace to avoid existing CSS names.

In my case, I want to set white-space: pre-line to this specific strack trace string, otherwise it's quite unreadable.

Missing serial number

1.Missing serial number

image

2.Want to add, modify and delete data (preferably mobile)

3.Let's get the formatted string and paste it into the blog

💪React version is in progress

🍻Now I have created a new react branch for developing the react version. The first phase will directly rewrite the original vue code, and the second phase will optimize and transform the existing code. Finally, the npm package will be released independently, and react and vue will be released in parallel on github.

Features

  • Use latest React API.
  • Use Typescript.
  • Use Webpack 4.

Next

  • Reduce size.
  • Perform optimization under big data.

Question: can I collapse certain named field?

Thanks for this nice library. I am using on my report page to show various query results.
One of the result contains an array with lots of items and I wonder:
if I can make the field to be collapsed by default and just showing items count.

...
requests: [...] // 3 items

Additional Classes for selectable key and value

This project is a great work. It is helpful. Thank you.

One small drawback is: Currently all the keys and values are styled the same. I would like to showcase the selectable keys and values differently so that users can immediately see only few keys and values are clickable. (Background color on hover is good, but without hovering on the node, just by looking at all the nodes it is difficult to say which nodes are clickable and which are not).

When using the function pathSelectable to customize the selectable behavior of a node, could you please add the classes vjs__selectable__value , vjs__selectable_key to the value and key respectively so that:

  • we can customize the selectable key css, and selectable value css each separately
  • and customize the mouse appearance, e.g. change the mouse to, say, pointer or hand etc. only for the selectable keys and values etc.

For example, the below screenshot illustrates the css overrides, where the usual keys and values take grey colors, and only the selectable keys and selectable values highlighted with different color, and underline, so that it is visually easy for users to understand that these keys and values are different and special.

image

Feature Request: showLineNumber

Hi, I always use vue-json-pretty component to render beautiful JSON data.

In my opinion, it should show line number of JSON data as option.
Is it interesting or useful? Please, let me know your ideas and comments.

Thank you,
World

Is there a way to show big json ?

When dealing with a big json, the CPU/MEM usage of chrome process will grow fast and the browser page will load slowly or even not display.

There is a sample json(size 1MB) in the attachment, copy and paste in the demo page can reproduce this condition. MEM usage of process will rich to nearly 2GB.

sample_1M.txt

Integrate with nuxt

I use vue-json-pretty with nuxtjs. But it dont work because "document is undefined".
Can you make a version for this integration?

TYpescript support

Hi,

Is there a way to add typescript support ?
I've tried npm install @types/vue-json-pretty but package doesn't exist.

Thanks,

best regards,
Alban

Error 'Unknown custom element: <vue-json-pretty>'

JSBin

https://jsbin.com/fomudog/edit?html,console

Console

"[Vue warn]: Unknown custom element: <vue-json-pretty> - did you register the component correctly? For recursive components, make sure to provide the \"name\" option.

found in

---> <App>
       <Root>"

Code

// App
const App = Vue.component('app', {
  render(create) {
    return create('div', [
      create('vue-json-pretty', {
        props: {              
          data: "{ key: 'value' }"
        }
      })
    ])
  }      
})

// Use    
Vue.use(VueJsonPretty)
      
// Instance
new Vue({        
  render(create) {
    return create(App)
  }
}).$mount('#app')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.