Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit def2058

Browse files
committed
Re-added selected option property
Use this to programmatically set a dropdown item as selected on init. Value can be string that matches the value of the targeted dropdown item, or integer that matches the dropdown item index.
1 parent 3975dc7 commit def2058

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ If this value is set to false, this dropdown will be enabled if any one of the r
141141

142142
Required dropdown value parameter name used in Ajax requests. If this value is not set, the plugin will use the dropdown name attribute. If neither this parameter nor the name attribute is set, this dropdown will not be taken into account in any Ajax request.
143143

144+
##### selected (string|integer)
145+
146+
selected: 'red'
147+
148+
selected: 2
149+
150+
<sub>Added: 1.1.5</sub>
151+
152+
Sets the default dropdown item on initialisation. The value can be a the value of the targeted dropdown item, or its index value.
153+
144154
##### onChange (eventHandler)
145155

146156
onChange: function(event, value, requiredValues) { }

demo.html

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ <h1>jQuery Cascading Dropdown <small>Examples</small></h1>
5454
<h4>Basic</h4>
5555
<p>This is a basic dropdown setup where the second dropdown is dependent on the first dropdown having a value, and the third dropdown is dependent on either the first or second one having a value.</p>
5656

57+
<p>This example also shows how you can set the selected dropdown item when you initialise the plugin. This can be done either in code, or by including the HTML selected attribute on the targeted dropdown item.</p>
58+
5759
<div id="example1" class="bs-docs-example">
5860
<h4>Phone finder</h4>
5961

@@ -66,7 +68,7 @@ <h4>Phone finder</h4>
6668
</select>
6769
<select class="step2" name="resolution">
6870
<option value="">Screen resolution</option>
69-
<option value="540">540p</option>
71+
<option value="540" selected>540p</option>
7072
<option value="720">720p</option>
7173
<option value="1080">1080p</option>
7274
</select>
@@ -93,7 +95,8 @@ <h6>Code</h6>
9395
<pre class="brush: js">$('#example1').cascadingDropdown({
9496
selectBoxes: [
9597
{
96-
selector: '.step1'
98+
selector: '.step1',
99+
selected: '4.3'
97100
},
98101
{
99102
selector: '.step2',
@@ -213,9 +216,6 @@ <h4>Phone finder</h4>
213216
</select>
214217
<select class="step2" name="resolution">
215218
<option value="">Screen resolution</option>
216-
<option value="540">540p</option>
217-
<option value="720">720p</option>
218-
<option value="1080">1080p</option>
219219
</select>
220220
<select class="step3" name="storage">
221221
<option value="">Storage size</option>
@@ -297,7 +297,8 @@ <h6>Code</h6>
297297
$('#example1').cascadingDropdown({
298298
selectBoxes: [
299299
{
300-
selector: '.step1'
300+
selector: '.step1',
301+
selected: '4.3'
301302
},
302303
{
303304
selector: '.step2',
@@ -379,7 +380,18 @@ <h6>Code</h6>
379380
selector: '.step1'
380381
},
381382
{
382-
selector: '.step2'
383+
selector: '.step2',
384+
source: function(request, response) {
385+
$.getJSON('/api/resolutions', request, function(data) {
386+
response($.map(data, function(item, index) {
387+
return {
388+
label: item + 'p',
389+
value: item,
390+
selected: index == 0
391+
};
392+
}));
393+
});
394+
}
383395
},
384396
{
385397
selector: '.step3',

jquery.cascadingdropdown.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
// Disable it first
147147
self.disable();
148148

149+
// Set selected dropdown item if defined
150+
if(!self.initialised) {
151+
self.options.selected && self.setSelected(self.options.selected);
152+
}
153+
149154
// If required dropdowns have no value, return
150155
if(!self._requirementsMet()) {
151156
self._triggerReady();
@@ -248,12 +253,10 @@
248253
// Set the dropdown item
249254
self.el[0].selectedIndex = indexOrValue;
250255

251-
if(!triggerChange) {
252-
return;
253-
}
254-
255256
// Trigger change event
256-
self.el.change();
257+
if(triggerChange) {
258+
self.el.change();
259+
}
257260

258261
return self.el;
259262
}
@@ -269,6 +272,8 @@
269272
_init: function() {
270273
var self = this;
271274

275+
self.pending = 0;
276+
272277
// Instance array
273278
self.dropdowns = [];
274279

@@ -284,15 +289,17 @@
284289
self.options.onReady.call(self, event, self.getValues());
285290
}
286291
}
292+
293+
function changeEventHandler(event) {
294+
self.options.onChange.call(self, event, self.getValues());
295+
}
287296

288297
if(typeof self.options.onReady === 'function') {
289298
dropdowns.bind('ready', readyEventHandler);
290299
}
291300

292301
if(typeof self.options.onChange === 'function') {
293-
dropdowns.change(function(event) {
294-
self.options.onChange.call(self, event, self.getValues());
295-
});
302+
dropdowns.bind('change', changeEventHandler);
296303
}
297304

298305
// Init dropdowns

0 commit comments

Comments
 (0)