forked from corinis/jsForm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample-object.html
76 lines (69 loc) · 1.93 KB
/
sample-object.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="src/jquery.jsForm.js"></script>
<script>
$(function(){
// some json data
var jsonData = {
"description": "testing",
"objects": [
{
"id": 1,
"enable": true,
"find": {
"name": "asdf"
}
},
{
"id": 2,
"enable": false,
"find": {
"name": "asdf"
}
}
]
};
$("textarea.object[name='objects.']").on("fill", function(){
// fill will be triggered after the content has been inserted - this way we can override that
$(this).val(JSON.stringify($(this).data("pojo")));
});
// update the pojo with the "json" content
$("textarea.object[name='objects.']").keyup(function(){
try {
$(this).data().pojo = JSON.parse($(this).val());
// json is fine
$(this).css("border", "2px solid green");
$(this).attr("title", "JSON ok");
} catch (ex) {
// error in json (add a title field with the error)
$(this).css("border", "2px solid red");
$(this).attr("title", ex);
}
});
// initialize the form, prefix is optional and defaults to data
$("#details").jsForm({
data:jsonData
});
$("#show").click(function() {
// show the json data
alert(JSON.stringify($("#details").jsForm("get"), null, " "));
});
});
</script>
</head>
<body>
<h1>Simple Form Test - working with objects</h1>
<div id="details">
<input name="data.description"/>
<ul class="collection" data-field="data.objects">
<li>
<label for="objects.id">id:</label>
<input name="objects.id" title=''/> <br />
<textarea class="object" name="objects." ></textarea>
</li>
</ul>
</div>
<button id="show">Show Object</button>
</body>
</html>