forked from BorisMoore/jquery-datalink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3collectionChange.html
More file actions
144 lines (127 loc) · 3.71 KB
/
3collectionChange.html
File metadata and controls
144 lines (127 loc) · 3.71 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
<script src="jquery.datalink2.js" type="text/javascript"></script>
<script src="jquery.tmpl2.js" type="text/javascript"></script>
<style type="text/css">
table { border-collapse: collapse; }
td { padding: 6px; }
.r-o { border: 2px darkblue solid; }
.row1 { border: 2px darkblue solid; border-bottom: 0px; }
.row2 { border: 2px darkblue solid; border-top: 0px; }
</style>
</head>
<body>
<button onclick="showData()">show data</button>
<button onclick="setLastName()">set last name</button>
<button onclick="append()">append</button>
<button onclick="deleteLast()">delete last row</button>
<hr /><br />
<table id="peopleGrid">
<tbody>
<tr class="row1" data-jq-path="">
<td>
<button class="btnDelete">X</button>
Name: <span data-jq-linkfrom="firstName"></span> <span data-jq-linkfrom="lastName"></span>
</td>
</tr>
<tr class="row2">
<td>
<input data-jq-linkto="firstName" data-jq-linkfrom="firstName" />
<input data-jq-linkto="lastName" data-jq-linkfrom="lastName" />
</td>
</tr>
<tr class="row1" data-jq-path="">
<td>
<button class="btnDelete">X</button>
Name: <span data-jq-linkfrom="firstName"></span> <span data-jq-linkfrom="lastName"></span>
</td>
</tr>
<tr class="row2">
<td>
<input data-jq-linkto="firstName" data-jq-linkfrom="firstName" />
<input data-jq-linkto="lastName" data-jq-linkfrom="lastName" />
</td>
</tr>
<tr class="row1" data-jq-path="">
<td>
<button class="btnDelete">X</button>
Name: <span data-jq-linkfrom="firstName"></span> <span data-jq-linkfrom="lastName"></span>
</td>
</tr>
<tr class="row2">
<td>
<input data-jq-linkto="firstName" data-jq-linkfrom="firstName" />
<input data-jq-linkto="lastName" data-jq-linkfrom="lastName" />
</td>
</tr>
</tbody>
</table>
<script id="personTmpl" type="text/x-jquery-tmpl">
<tr class="row1">
<td>
<button class="btnDelete">X</button>
Name: <span data-jq-linkfrom="firstName"></span> <span data-jq-linkfrom="lastName"></span>
</td>
</tr>
<tr class="row2">
<td>
<input data-jq-linkto="firstName" data-jq-linkfrom="firstName" />
<input data-jq-linkto="lastName" data-jq-linkfrom="lastName" />
</td>
</tr>
</script>
<script type="text/javascript">
var people = [
{
firstName: "Jo",
lastName: "Proctor"
},
person2 = {
firstName: "Maria",
lastName: "Garcia"
},
person3 = {
firstName: "Josh",
lastName: "Evetsky"
}
];
var savedlinks = $.dataLink( people, "#peopleGrid", "#personTmpl" ).pushValues();
$.dataLink( "#peopleGrid", people );
$( "#peopleGrid" )
.delegate( ".btnDelete", "click", function() {
var item = $.viewItem( this ),
index = $.inArray( item.data, item.parent.data );
$.changeArray( people, "splice", index, 1 );
});
function setLastName() {
if ( people.length ) {
$.setField( people[ people.length - 1 ], "lastName", "NewLastName" );
}
}
function append() {
$.changeArray( people, "push", {
firstName: "NewFirst",
lastName: "NewLast"
});
}
function deleteLast() {
$.changeArray( people, "pop" );
}
</script>
<!--Console-->
<script id="showData" type="text/x-jquery-tmpl">
<div>${firstName} ${lastName}</div>
</script>
<br /><hr />
<b>Console</b>
<div id="console"></div>
<script type="text/javascript">
function showData() {
$( "#console" ).append("-----------------");
$( "#showData" ).tmpl( people ).appendTo( "#console" );
}
</script>
</body>
</html>