-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsample-dataHandler.html
67 lines (64 loc) · 1.7 KB
/
sample-dataHandler.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
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="src/jquery.jsForm.js"></script>
<script>
$(function(){
// some json data
var jsonData = {
name: "emaNtsriF", // standard inputs
links: [{href:'XXhttp://www.gargan.org',description:'Gargan.org'},{href:'XXhttp://www.github.com',description:'GitHub'}], // lists
};
// initialize the form, prefix is optional and defaults to data
$("#details").jsForm({
data:jsonData,
dataHandler: {
serialize: function(val, field, obj) {
if(!val)
return null;
if(field.hasClass("prex")) {
return "XX" + val;
}
if(field.hasClass("rev")) {
return val.split("").reverse().join("");;
}
return val;
},
deserialize: function(val, field, obj) {
if(!val)
return null;
if(field.hasClass("prex")) {
if(val.substring(0,2) === "XX")
return val.substring(2);
}
if(field.hasClass("rev")) {
return val.split("").reverse().join("");;
}
return val;
}
}
});
$("#show").click(function() {
// show the json data
alert(JSON.stringify($("#details").jsForm("get"), null, " "));
});
});
</script>
</head>
<body>
<h1>Simple Form Test</h1>
<div id="details">
Name: <input class="rev" name="data.name"/><br/>
<br/>
<fieldset>
<legend>Links</legend>
<ul class="collection" data-field="data.links">
<li><span class="field">links.description</span> Link: <input class="prex" name="links.href"/> <button class="delete">x</button></li>
</ul>
</fieldset>
<button class="add" data-field="data.links">add a link</button><br/>
Additional field: <input name="data.addedField" class="rev"/>
</div>
<button id="show">Show Object</button>
</body>
</html>