-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlinq.html
258 lines (230 loc) · 7.84 KB
/
linq.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
<!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>
<title>Populating dropdowns with Local JavaScript and LINQ</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="../src/jquery.loadJSON.js" type="text/javascript"></script>
<script src="scripts/jquery.linq.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/shCore.js"></script>
<script type="text/javascript" src="scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="scripts/shBrushXml.js"></script>
<link type="text/css" rel="stylesheet" href="scripts/shCoreDefault.css"/>
<link type="text/css" rel="stylesheet" href="scripts/shThemeDefault.css"/>
<script type="text/javascript">SyntaxHighlighter.all();</script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
var aoRegions, aoTowns;
/* //Uncomment this line if you want to load JS array via Ajax call
$('#Region').loadJSON('regions.js');
$.get("towns.js", function(jsonResponse){
aoTowns = jsonResponse.towns;
var id = $('#Region').val();
var queryResult = $.Enumerable.From(aoTowns)
.Where(function (town) { return town.regionid == id })
.OrderByDescending(function (town) { return town.text })
.ToArray();
$('#Town').loadJSON({ towns: queryResult} );
},
"json");
*/
aoRegions = [
{
"value": 1,
"text": "East Europe"
},
{
"value": 2,
"text": "West Europe"
},
{
"value": 3,
"text": "Middle Europe"
}
];
aoTowns = [
{
"value": 17,
"text": "Belgrade",
"regionid": 1
},
{
"value": 19,
"text": "Moscov",
"regionid": 1
},
{
"value": 20,
"text": "Kiev",
"regionid": 1
},
{
"value": 21,
"text": "London",
"regionid": 2
},
{
"value": 22,
"text": "Paris",
"regionid": 2
},
{
"value": 23,
"text": "Madrid",
"regionid": 2
},
{
"value": 24,
"text": "Lisabon",
"regionid": 2
},
{
"value": 31,
"text": "Berlin",
"regionid": 3
},
{
"value": 32,
"text": "Viena",
"regionid": 3
},
{
"value": 33,
"text": "Budapest",
"regionid": 3
},
{
"value": 33,
"text": "Prague",
"regionid": 3
}
,
{
"value": 33,
"text": "Warsaw",
"regionid": 3
}
];
$('#Region').loadJSON({ "regions":aoRegions });
$('#Region').change(function() {
var id = $(this).val();
var queryResult = $.Enumerable.From(aoTowns)
.Where(function (town) { return town.regionid == id })
.OrderByDescending(function (town) { return town.text })
.ToArray();
$('#Town').loadJSON({ "towns": queryResult} );
});
});
</script>
<style>
#Town{ width: 100px }
</style>
</head>
<body>
<div id="page-wrap">
<a href="http://code.google.com/p/jquery-load-json/" alt="Home">Home</a>
<div id="contact-area">
<a href="categories-ajax.html">Go To the Ajax example</a>
<h1>Filling the subcategory dropdown using the local JavaScript</h1>
<p>JQuery loadJSON plugin can be used to dinamically fill dropdown based on the selected item in primary dropdown. This is common usage in the applications
where you have primary list (e.g.categories or countries) and you need to dinamically populate secondary dropdown.</p>
<h2>Live example</h2>
<p>JQuery loadJSON plugin enables you to bind a list based on the JSON array of objects. In this example local variables containing the arrays of regions
and towns will be used to dinamicaly populate region and town dropdown lists. Each time user changes region, the town array will be filtered by the
regionid and loaded into the secondary dropdown. For filtering is used <a href="http://linqjs.codeplex.com/">JQuery LINQ</a> library but this is just
an option.
</p>
<form name="form_simple" id="form-simple" action="details.html" method="get">
<label for="Region">Region</label>
<select name="Region" id="Region">
<option value="-2" class="regions">Please select</option>
</select>
<br/>
<br/>
<label for="Town">Town</label>
<select name="Town" id="Town">
<option class="towns"/>
</select>
<br style="clear:both"/>
<br/>
</form>
<br /><br /><br /><br />
<h2>Implementation</h2>
<p>HTML code is shown in the following listing:</p>
<pre class="brush: xml;"><label for="Region">Region</label>
<select name="Region" >
<option value="-1" class="regions">Pleaase select</option>
</select>
<label for="Town">Town</label>
<select name="Town" id="Town">
<option class="towns" >-</option>
</select></pre>
<p>Classes "regions" and "towns in the option tags are used to map option elements that wil be populated with the JSON object that will be loaded into the HTML form shown above should have properties that matches name attributes
of the elements above. Example of JSON object that can be used to fill region list is shown below:</p>
<pre class="brush: js;">
aoRegions = [
{
"value": 1,
"text": "East Europe"
},
{
"value": 2,
"text": "West Europe"
},
{
"value": 3,
"text": "Middle Europe"
}
];
</pre>
<p>Example of JSON object that can be used to fill town list is shown below:</p>
<pre class="brush: js;">
aoRegions = [
{
"value": 17,
"text": "Belgrade"
"regionid": "1"
},
{
"value": 18,
"text": "Berlin"
"regionid": "2"
},
{
"value": 19,
"text": "London"
"regionid": "3"
},
{
"value": 20,
"text": "Paris"
"regionid": "3"
}
]
}
</pre>
<p>Each town has regionid property that will be used for filtering.</p>
<p>The following line of code will load the dropdown list:</p>
<pre class="brush: js;">
$('#Region').loadJSON({ "regions":aoRegions });
</pre>
<p>Note that original array is wraped into the object with "regions" property in order to match this property with the "regions" class
in the option template.</p>
<p>The following code will load the towns dropdown list when region is changed:</p>
<pre class="brush: js;">$('#Region').change(function() {
var id = $(this).val();
var queryResult = $.Enumerable.From(aoTowns)
.Where(function (town) { return town.regionid == id })
.OrderByDescending(function (town) { return town.text })
.ToArray();
$('#Town').loadJSON({ "towns": queryResult} );
});
</pre>
<p>When region is changed, id of the region is taken from the region select list, towns with that region id are filtered from the original array,
and they are ordered by text property.</p>
<a href="categories-ajax.html">Back to the list</a>
</div>
</div>
</body>
</html>