Skip to content

Commit a785cf6

Browse files
committed
Adds callback option for new opt groups to be added dynamically
1 parent a88f97f commit a785cf6

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<!-- Feel free to put either your handle and/or full name, according to
22
your privacy needs -->
33

4+
* New feature: dynamically add option groups
5+
6+
*@jackbentley*
7+
48
* New feature: allow to disable single options or complete optgroups
59

610
*@zeitiger*

docs/usage.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ $(function() {
228228
<td valign="top"><code>array</code></td>
229229
<td valign="top"><code>[]</code></td>
230230
</tr>
231+
<tr>
232+
<td valign="top"><code>optgroupRegister</code></td>
233+
<td valign="top">
234+
Dynamically register an option group. Return value should
235+
be false or same format as an <code>optgroups</code> obeject.
236+
</td>
237+
<td valign="top"><code>function</code></td>
238+
<td valign="top"><code>null</code></td>
239+
</tr>
231240
<tr>
232241
<td valign="top"><code>dataAttr</code></td>
233242
<td valign="top">The &lt;option&gt; attribute from which to read JSON data about the option.</td>

examples/optgroups.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,46 @@ <h2>Optgroups (programmatic)</h2>
151151
</script>
152152
</div>
153153

154+
<div class="demo">
155+
<h2>Optgroups (dynamic)</h2>
156+
<div class="control-group">
157+
<label for="select-console">Console:</label>
158+
<select id="select-console" class="demo-consoles" placeholder="Select console..."></select>
159+
</div>
160+
<script>
161+
$('#select-console').selectize({
162+
options: [
163+
{manufacturer: 'nintendo', value: "nes", name: "Nintendo Entertainment System"},
164+
{manufacturer: 'nintendo', value: "snes", name: "Super Nintendo Entertainment System"},
165+
{manufacturer: 'nintendo', value: "n64", name: "Nintendo 64"},
166+
{manufacturer: 'nintendo', value: "gamecube", name: "GameCube"},
167+
{manufacturer: 'nintendo', value: "wii", name: "Wii"},
168+
{manufacturer: 'nintendo', value: "wiiu", name: "Wii U"},
169+
{manufacturer: 'sony', value: 'ps1', name: 'PlayStation'},
170+
{manufacturer: 'sony', value: 'ps2', name: 'PlayStation 2'},
171+
{manufacturer: 'sony', value: 'ps3', name: 'PlayStation 3'},
172+
{manufacturer: 'sony', value: 'ps4', name: 'PlayStation 4'},
173+
{manufacturer: 'microsoft', value: 'xbox', name: 'Xbox'},
174+
{manufacturer: 'microsoft', value: '360', name: 'Xbox 360'},
175+
{manufacturer: 'microsoft', value: 'xbone', name: 'Xbox One'}
176+
],
177+
optgroupRegister: function (optgroup) {
178+
var capitalised = optgroup.charAt(0).toUpperCase() + optgroup.substring(1);
179+
var group = {
180+
label: 'Manufacturer: ' + capitalised
181+
};
182+
183+
group[this.settings.optgroupValueField] = optgroup;
184+
185+
return group;
186+
},
187+
optgroupField: 'manufacturer',
188+
labelField: 'name',
189+
searchField: ['name']
190+
});
191+
</script>
192+
</div>
193+
154194
<div class="demo">
155195
<h2>Plugin: "optgroup_columns"</h2>
156196
<div class="control-group" id="select-car-group">

src/defaults.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Selectize.defaults = {
3434
optgroupLabelField: 'label',
3535
optgroupValueField: 'value',
3636
lockOptgroupOrder: false,
37+
/*
38+
optgroupRegister: null, // function(optgroup) { ... }
39+
*/
3740

3841
sortField: '$order',
3942
searchField: ['text'],

src/selectize.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,12 @@ $.extend(Selectize.prototype, {
10821082

10831083
for (j = 0, k = optgroups && optgroups.length; j < k; j++) {
10841084
optgroup = optgroups[j];
1085+
if (!self.optgroups.hasOwnProperty(optgroup) && typeof self.settings.optgroupRegister === 'function') {
1086+
var regGroup;
1087+
if (regGroup = self.settings.optgroupRegister.apply(self, [optgroup])) {
1088+
self.registerOptionGroup(regGroup);
1089+
}
1090+
}
10851091
if (!self.optgroups.hasOwnProperty(optgroup)) {
10861092
optgroup = '';
10871093
}

test/setup.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@
9797
'Group 2': {label: 'Group 2', val: 'Group 2', $order: 4, dis: false}
9898
}, '2');
9999
});
100+
it('should register optgroups if optgroupRegister is set', function() {
101+
var test = setup_test('<select>', {
102+
options: [
103+
{value: 'a', grp: 'someGroup'},
104+
{value: 'b', grp: 'anotherGroup'},
105+
{value: 'c', grp: 'anotherGroup'}
106+
],
107+
optgroupValueField: 'val',
108+
optgroupField: 'grp',
109+
optgroupRegister: function (optgroup) {
110+
var group = {};
111+
group['label'] = optgroup;
112+
group['val'] = optgroup;
113+
114+
return group;
115+
}
116+
});
117+
test.selectize.refreshOptions();
118+
assert.deepEqual(test.selectize.optgroups, {
119+
'someGroup': {label: 'someGroup', val: 'someGroup', $order: 4},
120+
'anotherGroup': {label: 'anotherGroup', val: 'anotherGroup', $order: 5}
121+
}, '2');
122+
});
100123
it('should allow respect disabled flags of option and optgroup', function() {
101124
var test = setup_test(['<select>',
102125
'<optgroup label="Group 1">',

0 commit comments

Comments
 (0)