Skip to content

Commit c8f2944

Browse files
committed
Added "allowEmptyOption" option (selectize#163).
1 parent 6a071a2 commit c8f2944

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

docs/usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ $(function() {
9191
<td valign="top"><code>boolean</code></td>
9292
<td valign="top"><code>false</code></td>
9393
</tr>
94+
<tr>
95+
<td valign="top"><code>allowEmptyOption</code></td>
96+
<td valign="top">If true, Selectize will treat any options with a "" value like normal. This defaults to false to accomodate the common &lt;select&gt; practice of having the first empty option act as a placeholder.</td>
97+
<td valign="top"><code>boolean</code></td>
98+
<td valign="top"><code>false</code></td>
99+
</tr>
94100
<tr>
95101
<td valign="top"><code>scrollDuration</code></td>
96102
<td valign="top">The animation duration (in milliseconds) of the scroll animation triggered when going [up] and [down] in the options dropdown.</td>

examples/basic.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ <h2>&lt;select&gt;</h2>
5959
</script>
6060
</div>
6161

62+
<div class="demo">
63+
<h2>&lt;select&gt; (allow empty)</h2>
64+
<div class="control-group">
65+
<label for="select-beast-empty">Beast:</label>
66+
<select id="select-beast-empty" class="demo-default" data-placeholder="Select a person...">
67+
<option value="">None</option>
68+
<option value="4">Thomas Edison</option>
69+
<option value="1">Nikola</option>
70+
<option value="3">Nikola Tesla</option>
71+
<option value="5">Arnold Schwarzenegger</option>
72+
</select>
73+
</div>
74+
<script>
75+
$('#select-beast-empty').selectize({
76+
allowEmptyOption: true,
77+
create: true
78+
});
79+
</script>
80+
</div>
81+
6282
<div class="demo">
6383
<h2>&lt;select&gt; (disabled)</h2>
6484
<div class="control-group">

src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Selectize.defaults = {
1515
addPrecedence: false,
1616
selectOnTab: false,
1717
preload: false,
18+
allowEmptyOption: false,
1819

1920
scrollDuration: 60,
2021
loadThrottle: 300,

src/selectize.jquery.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $.fn.selectize = function(settings_user) {
1616
*/
1717
var init_textbox = function($input, settings_element) {
1818
var i, n, values, option, value = $.trim($input.val() || '');
19-
if (!value.length) return;
19+
if (!settings.allowEmptyOption && !value.length) return;
2020

2121
values = value.split(settings.delimiter);
2222
for (i = 0, n = values.length; i < n; i++) {
@@ -54,7 +54,7 @@ $.fn.selectize = function(settings_user) {
5454
$option = $($option);
5555

5656
value = $option.attr('value') || '';
57-
if (!value.length) return;
57+
if (!value.length && !settings.allowEmptyOption) return;
5858

5959
// if the option already exists, it's probably been
6060
// duplicated in another optgroup. in this case, push
@@ -124,8 +124,13 @@ $.fn.selectize = function(settings_user) {
124124
var instance;
125125
var $input = $(this);
126126
var tag_name = this.tagName.toLowerCase();
127+
var placeholder = $input.attr('placeholder') || $input.attr('data-placeholder');
128+
if (!placeholder && !settings.allowEmptyOption) {
129+
placeholder = $input.children('option[value=""]').text();
130+
}
131+
127132
var settings_element = {
128-
'placeholder' : $input.children('option[value=""]').text() || $input.attr('placeholder'),
133+
'placeholder' : placeholder,
129134
'options' : {},
130135
'optgroups' : {},
131136
'items' : []

src/selectize.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ $.extend(Selectize.prototype, {
619619
self.createItem();
620620
} else {
621621
value = $target.attr('data-value');
622-
if (value) {
622+
if (typeof value !== 'undefined') {
623623
self.lastQuery = null;
624624
self.setTextboxValue('');
625625
self.addItem(value);
@@ -1110,7 +1110,7 @@ $.extend(Selectize.prototype, {
11101110
}
11111111

11121112
value = hash_key(data[self.settings.valueField]);
1113-
if (!value || self.options.hasOwnProperty(value)) return;
1113+
if (typeof value !== 'string' || self.options.hasOwnProperty(value)) return;
11141114

11151115
self.userOptions[value] = true;
11161116
self.options[value] = data;
@@ -1147,8 +1147,9 @@ $.extend(Selectize.prototype, {
11471147
value_new = hash_key(data[self.settings.valueField]);
11481148

11491149
// sanity checks
1150+
if (value === null) return;
11501151
if (!self.options.hasOwnProperty(value)) return;
1151-
if (!value_new) throw new Error('Value must be set in option data');
1152+
if (typeof value_new !== 'string') throw new Error('Value must be set in option data');
11521153

11531154
// update references
11541155
if (value_new !== value) {
@@ -1260,7 +1261,7 @@ $.extend(Selectize.prototype, {
12601261
getElementWithValue: function(value, $els) {
12611262
value = hash_key(value);
12621263

1263-
if (value) {
1264+
if (typeof value !== 'undefined' && value !== null) {
12641265
for (var i = 0, n = $els.length; i < n; i++) {
12651266
if ($els[i].getAttribute('data-value') === value) {
12661267
return $($els[i]);
@@ -1426,7 +1427,7 @@ $.extend(Selectize.prototype, {
14261427

14271428
if (!data || typeof data !== 'object') return;
14281429
var value = hash_key(data[self.settings.valueField]);
1429-
if (!value) return;
1430+
if (typeof value !== 'string') return;
14301431

14311432
self.setTextboxValue('');
14321433
self.addOption(data);

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ var isset = function(object) {
2222
* 1 -> '1'
2323
*
2424
* @param {string} value
25-
* @returns {string}
25+
* @returns {string|null}
2626
*/
2727
var hash_key = function(value) {
28-
if (typeof value === 'undefined' || value === null) return '';
28+
if (typeof value === 'undefined' || value === null) return null;
2929
if (typeof value === 'boolean') return value ? '1' : '0';
3030
return value + '';
3131
};

0 commit comments

Comments
 (0)