/
examples.html
471 lines (406 loc) · 18.2 KB
/
examples.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>dirtyFields Examples</title>
<!--Pulls jquery 1.7 from Google-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery.dirtyFields.packed.js"></script>
<style>
a {color:blue;text-decoration:none;}
a:visited {color:blue;}
a:hover {text-decoration:underline;}
body {font: 85%/160% "Trebuchet MS", Tahoma, Arial;}
form {border:1px solid #ccc;}
form label {
width:10em;
float: left;
text-align: right;
margin-right: 0.5em;
display: block;
font-weight:bold;
}
hr {height:15px;background-color:#ccc;margin-top:30px;}
input, select, textarea {font: 85%/160% "Trebuchet MS", Tahoma, Arial;}
pre {
background-color: #ffffcc;
font-family:Arial, sans-serif;
font-size:95%;
overflow:auto;
padding:3px;
width:98%;
}
.checkboxP {margin-top:0ex;}
.checkboxDiv {margin-top:-1.8ex;}
.dirtyField {color:red;}
.dirtyForm {background-color:#ffffcc;}
.dirtyChoice {font-style:italic;font-weight:bold;}
.floatBreak {
position:relative;
clear:both;
width:100%;
margin-top:25px;
}
.formLeft {width:320px;float:left;}
.hideElement {display:none;}
.inputGroup {float:left;margin-bottom:10px;}
.memberBlock {border-bottom:1px solid #ccc;}
.templateLeft {margin-top:0ex;width:320px;float:left;}
#callbackBox {
overflow:scroll;
border:2px solid #ccc;
margin:5px;
padding:5px;
height:16ex;
}
#resetButton {margin-left:35em;margin-top:15px;width:100px;}
</style>
<!--[if IE]>
<style>
.formLeft {
margin-top:20px;
width:320px;
float:left;
}
.IEcheckbox {
margin-top:0px;
margin-bottom:-50px;
}
.IEselectBreak {
margin-top:-70px;
}
}</style>
<![endif]-->
<script type="text/javascript">
var memberCount= 0;
$(document).ready(function() {
$("#demo1").dirtyFields();
var demo2Settings= {
denoteDirtyForm: true,
denoteDirtyOptions:true,
dirtyOptionClass:"dirtyChoice",
trimText:true,
preFieldChangeCallback: function(originalValue) {
$("#callbackBox").append("[preFieldChangeCallback] Original value of field '" + $(this).attr("name") + "' is: " + originalValue + "<br />");
},
fieldChangeCallback: function(originalValue,result) {
$("#callbackBox").append("[fieldChange callback] Original value of field '" + $(this).attr("name") + "' is: " + originalValue + ". Dirty status is " + result + "<br />");
},
formChangeCallback: function(result,dirtyFieldsArray) {
if(result)
{
$(this).find("input[type='button']").attr("disabled",false);
$("#callbackBox").append("[formChangeCallback] Form is dirty. Dirty fields are: " + dirtyFieldsArray + "<br />");
}
else
{
$(this).find("input[type='button']").attr("disabled",true);
$("#callbackBox").append("[formChangeCallback] Form is clean.<br />");
}
//Scrolls the logging div to the bottom
var objDiv = document.getElementById("callbackBox");
objDiv.scrollTop = objDiv.scrollHeight;
}
};
$("#demo2").dirtyFields(demo2Settings);
$("#fakeSubmitButton").click(function() {
$.fn.dirtyFields.formSaved($("#demo2"));
});
$("#resetButton").click(function() {
$.fn.dirtyFields.rollbackForm($("#demo2"));
$("input[type='button']").attr("disabled",true);
});
$("#demo3").dirtyFields();
$("#addMember").click(function() {
memberCount= memberCount+1;
var newBlockId= "memberBlock" + memberCount;
var newTeamMemberId= "memberName" + memberCount;
var newSkillLevelId= "skillLevel" + memberCount;
var $memberBlock= $("#template",$("#demo3")).clone();
$memberBlock.attr("id",newBlockId);
$memberBlock.find("label:eq(0)").attr("for",newTeamMemberId);
$memberBlock.find("input").attr("id",newTeamMemberId).attr("name","memberName");
$memberBlock.find("label:eq(1)").attr("for",newSkillLevelId);
$memberBlock.find("select").attr("id",newSkillLevelId).attr("name","skillLevel");
$("#memberFieldset").append($memberBlock);
$.fn.dirtyFields.setStartingTextValue($("#"+newTeamMemberId),$("#demo3"));
$.fn.dirtyFields.setStartingSelectValue($("#"+newSkillLevelId),$("#demo3"));
$.fn.dirtyFields.configureField($("#"+newTeamMemberId),$("#demo3"),"text");
$.fn.dirtyFields.configureField($("#"+newSkillLevelId),$("#demo3"),"select");
$memberBlock.removeClass("hideElement");
return false;
});
$("#demo1Code").hide();
$("#demo2Code").hide();
$("#demo3Code").hide();
$("#demo1CodeLink").toggle(
function() {
$("#demo1Code").show("fast");
$(this).text("Hide code");
},
function() {
$("#demo1Code").hide("fast");
$(this).text("View code");
}
);
$("#demo2CodeLink").toggle(
function() {
$("#demo2Code").show("fast");
$(this).text("Hide code");
},
function() {
$("#demo2Code").hide("fast");
$(this).text("View code");
}
);
$("#demo3CodeLink").toggle(
function() {
$("#demo3Code").show("fast");
$(this).text("Hide code");
},
function() {
$("#demo3Code").hide("fast");
$(this).text("View code");
}
);
});
</script>
</head>
<body>
<h2>dirtyFields Examples</h2>
<ul class="pageNavList">
<li>
<a class="internalLink" href="#defaultSettings">Default Settings</a>
</li>
<li>
<a class="internalLink" href="#customSettingsCallbacks">Custom Settings, Callbacks, and Some Public Functions</a>
</li>
<li>
<a class="internalLink" href="#addingFields">Adding Fields Dynamically</a>
</li>
</ul>
<p><strong>NOTE:</strong> This page pulls down a copy of the jQuery library from Google, so the examples may not work without an Internet connection.</p>
<hr />
<a name="defaultSettings"></a><h3>Default Settings</h3>
<p>This example demonstrates what the plugin does with form fields on its default settings. Change the values of the text input, textarea, and select box, and as they lose the focus you'll see their labels change. Click on the checkboxes and radio buttons to see a similar thing happen with the associated text. Change things back to where they started to reverse the style change.</p>
<p><a id="demo1CodeLink" class="internalLink" href="">View code</a></p>
<pre id="demo1Code"><style>
.dirtyField {color:red;}
</style>
$(document).ready(function() {
$("#demo1").dirtyFields();
});
</pre>
<form name="demo1" id="demo1" method="post" action="none.html">
<p class="formLeft">
<label for="teamName1" id="lbl_teamName1">Team Name:</label>
<input type="text" id="teamName1" name="teamName" size="20" maxlength="200" value="Team Maverick" tabindex="1" />
</p>
<p>
<label for="teamHistory1" id="lbl_teamHistory1">Team History:</label>
<textarea name="teamHistory" id="teamHistory1" rows="4" cols="40" tabindex="2">Team Maverick was founded by John Laller and James Taft in 2005, and was one of the first teams in the Baltimore Tag-Team Pool League.</textarea>
</p>
<div class="floatBreak"></div>
<div class="formLeft">
<p class="checkboxP">
<label for="gameTypes" id="lbl_gameTypes1">Game Types Played:</label>
<div class="inputGroup checkboxDiv">
<input type="checkbox" name="gameTypes" id="gameTypes_1" value="8-Ball" checked="checked" tabindex="3"/> <span>8-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_2" value="9-Ball" checked="checked" tabindex="4"/> <span>9-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_3" value="3-Ball" tabindex="5"/> <span>3-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_4" value="1-Ball" tabindex="6"/> <span>1-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_5" value="Snooker" tabindex="7"/> <span>Snooker</span><br />
</div>
<div class="floatBreak"></div>
</p>
</div>
<p>
<label for="status" id="lbl_status1">Team Status:</label>
<div class="inputGroup">
<input type="radio" name="status" id="status_1" value="Active" checked="checked" tabindex="8" /> <span>Active</span> <br />
<input type="radio" name="status" id="status_2" value="Inactive" tabindex="9" /> <span>Inactive</span> <br />
<input type="radio" name="status" id="status_3" value="Ineligible" tabindex="10" /> <span>Ineligible</span> <br />
<input type="radio" name="status" id="status_4" value="Disbanded" tabindex="11" /> <span>Disbanded</span> <br />
</div>
<div class="floatBreak"></div>
</p>
<div class="floatBreak"></div>
<p>
<br />
<label for="activeMembers1" id="lbl_activeMembers1">Team Captain:</label>
<select name="activeMembers" id="activeMembers1" tabindex="12">
<option value="Saul">Carter, Saul</option>
<option value="Marty">Dorn, Marty</option>
<option value="Neal">Drew, Neal</option>
<option value="Carlos" selected="selected">Estaban, Carlos</option>
<option value="Steve">Franklin, Steve</option>
<option value="Tre">Garner, Tre</option>
</select>
</p>
</form>
<hr />
<a name="customSettingsCallbacks"></a><h3>Custom Settings, Callbacks, and Some Public Functions</h3>
<p>This example demonstrates the following:</p>
<ul>
<li>The use of the denoteDirtyForm option to track whether the form is clean or not.</li>
<li>The use of the denoteDirtyOptions option with a multi-select drop-down box.</li>
<li>The use of the dirtyOptionClass option to customize the name of the CSS class to use to style dirty <option> elements (the default class name is "dirtyOption").</li>
<li>The use of the trimText option to ignore leading or trailing spaces when evaluating if a text field has changed or not.</li>
<li>The execution of all three plugin callbacks.</li>
<li>The use of the formSaved and rollbackForm public functions.</li>
</ul>
<p>As you make changes to the form, the various callbacks will add logging statements to the <div> block below the form, and the formChangeCallback will change the background color of the form and disable or enable the form buttons depending on whether or not the form is dirty. After you make a few changes, click the "Reset" button to undo all of your changes. Make a few additional changes, the click the "Fake Submit" button to simulate an AJAX form submission and update the saved values/states of each form field with the current field values/states. Make another change or two, then click the "Reset" button again, and note how the values were reset to what they were just after you clicked the "Fake Submit" button, and not to the values that existed when the page was first loaded.</p>
<p><a id="demo2CodeLink" class="internalLink" href="">View code</a></p>
<pre id="demo2Code"><style>
.dirtyField {color:red;}
.dirtyForm {background-color:#ffffcc;}
.dirtyChoice {font-style:italic;font-weight:bold;}
</style>
$(document).ready(function() {
var demo2Settings= {
denoteDirtyForm: true,
denoteDirtyOptions:true,
dirtyOptionClass:"dirtyChoice",
trimText:true,
preFieldChangeCallback: function(originalValue) {
$("#callbackBox").append("[preFieldChangeCallback] Original value of field '" + $(this).attr("name") + "' is: " + originalValue + "<br />");
},
fieldChangeCallback: function(originalValue,result) {
$("#callbackBox").append("[fieldChange callback] Original value of field '" + $(this).attr("name") + "' is: " + originalValue + ". Dirty status is " + result + "<br />");
},
formChangeCallback: function(result,dirtyFieldsArray) {
if(result)
{
$(this).find("input[type='button']").attr("disabled",false);
$("#callbackBox").append("[formChangeCallback] Form is dirty. Dirty fields are: " + dirtyFieldsArray + "<br />");
}
else
{
$(this).find("input[type='button']").attr("disabled",true);
$("#callbackBox").append("[formChangeCallback] Form is clean.<br />");
}
//Scrolls the logging div to the bottom
var objDiv = document.getElementById("callbackBox");
objDiv.scrollTop = objDiv.scrollHeight;
}
};
$("#demo2").dirtyFields(demo2Settings);
});
</pre>
<form name="demo2" id="demo2" method="post" action="none.html">
<p class="formLeft">
<label for="teamName2" id="lbl_teamName2">Team Name:</label>
<input type="text" id="teamName2" name="teamName" size="20" maxlength="200" value="Team Maverick" tabindex="13" />
</p>
<p>
<label for="teamHistory2" id="lbl_teamHistory2">Team History:</label>
<textarea name="teamHistory" id="teamHistory2" rows="4" cols="40" tabindex="14">Team Maverick was founded by John Laller and James Taft in 2005, and was one of the first teams in the Baltimore Tag-Team Pool League.</textarea>
</p>
<div class="floatBreak"></div>
<div class="formLeft"
<p class="checkboxP">
<label for="gameTypes" id="lbl_gameTypes">Game Types Played:</label>
<div class="inputGroup checkboxDiv">
<input type="checkbox" name="gameTypes" id="gameTypes_1" value="1" checked="checked" tabindex="15" /> <span>8-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_2" value="2" checked="checked" tabindex="16" /> <span>9-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_3" value="3" tabindex="17" /> <span>3-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_4" value="4" tabindex="18" /> <span>1-Ball</span><br />
<input type="checkbox" name="gameTypes" id="gameTypes_5" value="5" tabindex="19" /> <span>Snooker</span><br />
</div>
<div class="floatBreak"></div>
</p>
</div>
<p>
<label for="status" id="lbl_status">Team Status:</label>
<div class="inputGroup">
<input type="radio" name="status" id="status_1" value="1" checked="checked" tabindex="20" /> <span>Active</span> <br />
<input type="radio" name="status" id="status_2" value="2" tabindex="21" /> <span>Inactive</span> <br />
<input type="radio" name="status" id="status_3" value="3" tabindex="22" /> <span>Ineligible</span> <br />
<input type="radio" name="status" id="status_4" value="4" tabindex="23" /> <span>Disbanded</span> <br />
</div>
<div class="floatBreak"></div>
</p>
<div class="floatBreak"></div>
<p>
<br />
<label for="activeMembers2" id="lbl_activeMembers2">Active Members:</label>
<select name="activeMembers" id="activeMembers2" multiple="multiple">
<option value="Abel" selected="selected" tabindex="24">Abel, Matthew</option>
<option value="Bailey" selected="selected" tabindex="25">Balley, Lon</option>
<option value="Cable" selected="selected" tabindex="26">Cable, Brad</option>
<option value="Carter" tabindex="27">Carter, Saul</option>
<option value="Dorn" tabindex="28">Dorn, Marty</option>
<option value="Drew" tabindex="29">Drew, Neal</option>
<option value="Estaban" tabindex="30">Estaban, Carlos</option>
<option value="Franklin" tabindex="31">Franklin, Steve</option>
<option value="Garner" tabindex="32">Garner, Tre</option>
</select>
</p>
<p>
<input type="button" id="resetButton" value="Reset" tabindex="33" disabled="true" /> <input type="button" id="fakeSubmitButton" name="fakeSubmitButton" value="Fake Submit" tabindex="34" disabled="true">
<br />
</p>
</form>
<div id="callbackBox">
</div>
<hr />
<a name="addingFields"></a><h3>Adding Fields Dynamically</h3>
<p>This example demonstrates the use of the configureFields public function to add the event handler to new form fields created after the page has been loaded. Click the "Add Member" link to add new team members, and make changes to those new form fields to see how the correponding labels are updated.</p>
<p><a id="demo3CodeLink" class="internalLink" href="">View code</a></p>
<pre id="demo3Code"><style>
.dirtyField {color:red;}
</style>
$(document).ready(function() {
$("#demo3").dirtyFields();
$("#addMember").click(function() {
//Code to create the new fields and set the names and ids
memberCount= memberCount+1;
var newBlockId= "memberBlock" + memberCount;
var newTeamMemberId= "memberName" + memberCount;
var newSkillLevelId= "skillLevel" + memberCount;
var $memberBlock= $("#template",$("#demo3")).clone();
$memberBlock.attr("id",newBlockId);
$memberBlock.find("label:eq(0)").attr("for",newTeamMemberId);
$memberBlock.find("input").attr("id",newTeamMemberId).attr("name","memberName");
$memberBlock.find("label:eq(1)").attr("for",newSkillLevelId);
$memberBlock.find("select").attr("id",newSkillLevelId).attr("name","skillLevel");
//Add the new set of fields
$("#memberFieldset").append($memberBlock);
//Use the plugin's setStartingTextValue and setStartingSelectValue functions to store the initial value of the fields
$.fn.dirtyFields.setStartingTextValue($("#"+newTeamMemberId),$("#demo3"));
$.fn.dirtyFields.setStartingSelectValue($("#"+newSkillLevelId),$("#demo3"));
//Use the plugin's configureField function to set the event handlers
$.fn.dirtyFields.configureField($("#"+newTeamMemberId),$("#demo3"),"text");
$.fn.dirtyFields.configureField($("#"+newSkillLevelId),$("#demo3"),"select");
//Show the new set of fields
$memberBlock.removeClass("hideElement");
return false;
});
});
</pre>
<form name="demo3" id="demo3" method="post" action="none.html">
<div id="template" class="hideElement memberBlock">
<p class="templateLeft">
<label id="lbl_memberName" for="memberName">Name:</label><input type="text" name="mName" id="memberName" value="" tabindex="35">
</p>
<p>
<label id="lbl_skillLevel" for="skillLevel">Skill Level:</label>
<select name="sLevel" id="skillLevel">
<option value="Novice">Novice</option>
<option value="Intermediate" selected="selected">Intermediate</option>
<option value="Expert">Expert</option>
</select>
</p>
<div class="floatBreak"></div>
</div>
<p>
<label for="teamName3" id="lbl_teamName3">Team Name:</label>
<input type="text" id="teamName3" name="teamName" size="20" maxlength="200" value="Team Maverick" tabindex="35" />
</p>
<fieldset id="memberFieldset">
<legend>Team Members</legend>
<p><a id="addMember" href="" class="internalLink" tabindex="36">Add Member</a></p>
</fieldset>
</form>
</body>
</html>