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