|
1 | 1 | # json-editor
|
2 | 2 | This is a solution for create forms from JSON source automatically (with some minor constraints, like the enum values)
|
| 3 | + |
| 4 | +## Demo |
| 5 | +https://codepen.io/iroshan/pen/ReZxZy |
| 6 | + |
| 7 | +## Example |
| 8 | +HTML: |
| 9 | +```html |
| 10 | +<div id="json-container"></div> |
| 11 | +``` |
| 12 | + |
| 13 | +CSS: |
| 14 | +```css |
| 15 | +/* THEME */ |
| 16 | + |
| 17 | +.json-editor-container { |
| 18 | + transition: all 0.5s ease-in-out; |
| 19 | + border: none; |
| 20 | + border-left: 1px solid rgba(255, 255, 255, 0.2); |
| 21 | + background: #282c34; |
| 22 | + margin: 0; |
| 23 | + padding: 3px; |
| 24 | + padding-left: 15px; |
| 25 | + max-height: 10000px; |
| 26 | +} |
| 27 | + |
| 28 | +.json-editor-container:last-child { |
| 29 | + border-bottom: 1px solid rgba(255, 255, 255, 0.2); |
| 30 | + border-radius: 0 0 0 5px; |
| 31 | +} |
| 32 | + |
| 33 | +.json-editor-container.root { |
| 34 | + padding: 3px; |
| 35 | + margin: 0; |
| 36 | + border: none; |
| 37 | + border-radius: 5px; |
| 38 | +} |
| 39 | + |
| 40 | +.json-editor-container.root > .json-editor-container { |
| 41 | + border-left: none; |
| 42 | + padding-left: 3px; |
| 43 | +} |
| 44 | + |
| 45 | +.json-editor-container.root > .json-editor-container:last-child { |
| 46 | + border-bottom: none; |
| 47 | +} |
| 48 | + |
| 49 | +.json-editor-container.hidden-property { |
| 50 | + opacity: 0.3; |
| 51 | + max-height: 40px; |
| 52 | +} |
| 53 | + |
| 54 | +.json-editor-container input[type="checkbox"] { |
| 55 | + vertical-align: top; |
| 56 | + margin-top: 13px; |
| 57 | + margin-left: 5px; |
| 58 | +} |
| 59 | + |
| 60 | +.json-editor-container label { |
| 61 | + color: #d19a66; |
| 62 | + height: 22px; |
| 63 | + vertical-align: top; |
| 64 | + margin-top: 13px; |
| 65 | + white-space: nowrap; |
| 66 | + overflow: hidden; |
| 67 | + text-overflow: ellipsis; |
| 68 | + width: calc(30% - 25px); |
| 69 | +} |
| 70 | + |
| 71 | +.json-editor-container.root > label { |
| 72 | + width: calc(100% - 10px); |
| 73 | +} |
| 74 | + |
| 75 | +.json-editor-container input[type="text"], |
| 76 | +.json-editor-container select { |
| 77 | + vertical-align: top; |
| 78 | + width: calc(70% - 25px); |
| 79 | + border-radius: 5px; |
| 80 | + padding: 5px; |
| 81 | + height: 20px; |
| 82 | + background: rgba(255, 255, 255, 0.8); |
| 83 | +} |
| 84 | + |
| 85 | +.json-editor-container select { |
| 86 | + height: 30px; |
| 87 | + width: calc(70% - 15px); |
| 88 | +} |
| 89 | + |
| 90 | +.json-editor-container input[type="checkbox"]:focus, |
| 91 | +.json-editor-container input[type="text"]:focus, |
| 92 | +.json-editor-container select:focus { |
| 93 | + background: rgba(255, 255, 255, 0.9); |
| 94 | + box-shadow: 0 0 4px 4px rgba(255, 255, 255, 0.2); |
| 95 | +} |
| 96 | + |
| 97 | +@media screen and (max-width: 600px) { |
| 98 | + .json-editor-container { |
| 99 | + margin: 1px; |
| 100 | + padding: 1px; |
| 101 | + padding-left: 5px; |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Javascript: |
| 107 | +```javascript |
| 108 | +var json = { |
| 109 | + object: { |
| 110 | + first: 0, |
| 111 | + second: 1 |
| 112 | + }, |
| 113 | + int: 0, |
| 114 | + float: 0.1, |
| 115 | + string: "hello", |
| 116 | + boolean: { |
| 117 | + type: "enum", |
| 118 | + values: ["True", "False"] |
| 119 | + }, |
| 120 | + enum: { // special type |
| 121 | + type: "enum", |
| 122 | + values: [ |
| 123 | + "A", |
| 124 | + "B", |
| 125 | + "C" |
| 126 | + ] |
| 127 | + }, |
| 128 | + person: { |
| 129 | + name: "Adam", |
| 130 | + age: 35, |
| 131 | + children: [ |
| 132 | + { |
| 133 | + name: "Adam", |
| 134 | + age: 4 |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "Eve", |
| 138 | + age: 3 |
| 139 | + } |
| 140 | + ] |
| 141 | + }, |
| 142 | + array: [ |
| 143 | + 0, |
| 144 | + 1, |
| 145 | + 2 |
| 146 | + ] |
| 147 | +}; |
| 148 | + |
| 149 | +var container = document.querySelector("#json-container"); |
| 150 | +var editor = new JSONEditor(); |
| 151 | +editor.setTitle("Edit JSON"); |
| 152 | +editor.setJSON(container, json); |
| 153 | +container.addEventListener("change", function() { |
| 154 | + console.clear(); |
| 155 | + console.log(editor.getJSON()); |
| 156 | +}); |
| 157 | +``` |
0 commit comments