Skip to content

Commit 27ac50a

Browse files
committed
Fixed problems with non-string ids
We have to enforce ids being strings as the values of options within a select will always be an id. This fixes an issue that we had with array selections not being highlighted in the results.
1 parent ac7e7da commit 27ac50a

10 files changed

Lines changed: 56 additions & 16 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module.exports = function (grunt) {
100100
optimize: "none",
101101
name: "select2/core",
102102
out: "dist/js/select2.amd.js",
103+
include: includes,
103104
paths: {
104105
jquery: "empty:"
105106
}

dist/js/select2.amd.full.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ define('select2/results',[
181181
var self = this;
182182

183183
this.data.current(function (selected) {
184-
selected = $.map(selected, function (s) { return s.id; });
184+
var selectedIds = $.map(selected, function (s) {
185+
return s.id.toString();
186+
});
185187

186188
self.$results.find('.option.selected').removeClass('selected');
187189

@@ -191,7 +193,7 @@ define('select2/results',[
191193
var $option = $(this);
192194
var item = $option.data('data');
193195

194-
if (selected.indexOf(item.id.toString()) > -1) {
196+
if (selectedIds.indexOf(item.id.toString()) > -1) {
195197
$option.addClass('selected');
196198
}
197199
});

dist/js/select2.amd.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ define('select2/results',[
181181
var self = this;
182182

183183
this.data.current(function (selected) {
184-
selected = $.map(selected, function (s) { return s.id; });
184+
var selectedIds = $.map(selected, function (s) {
185+
return s.id.toString();
186+
});
185187

186188
self.$results.find('.option.selected').removeClass('selected');
187189

@@ -191,7 +193,7 @@ define('select2/results',[
191193
var $option = $(this);
192194
var item = $option.data('data');
193195

194-
if (selected.indexOf(item.id.toString()) > -1) {
196+
if (selectedIds.indexOf(item.id.toString()) > -1) {
195197
$option.addClass('selected');
196198
}
197199
});
@@ -937,3 +939,28 @@ define('select2/core',[
937939
return Select2;
938940
});
939941

942+
define('jquery.select2',[
943+
'jquery',
944+
'select2/core'
945+
], function ($, Select2) {
946+
if ($.fn.select2 == null) {
947+
$.fn.select2 = function (options) {
948+
options = options || {};
949+
950+
if (typeof options === 'object') {
951+
this.each(function () {
952+
var instance = new Select2($(this), options);
953+
});
954+
} else if (typeof options === 'string') {
955+
var instance = this.data('select2');
956+
957+
instance[options](arguments.slice(1));
958+
} else {
959+
throw new Error('Invalid arguments for Select2: ' + options);
960+
}
961+
};
962+
}
963+
964+
return Select2;
965+
});
966+

dist/js/select2.full.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9719,7 +9719,9 @@ define('select2/results',[
97199719
var self = this;
97209720

97219721
this.data.current(function (selected) {
9722-
selected = $.map(selected, function (s) { return s.id; });
9722+
var selectedIds = $.map(selected, function (s) {
9723+
return s.id.toString();
9724+
});
97239725

97249726
self.$results.find('.option.selected').removeClass('selected');
97259727

@@ -9729,7 +9731,7 @@ define('select2/results',[
97299731
var $option = $(this);
97309732
var item = $option.data('data');
97319733

9732-
if (selected.indexOf(item.id.toString()) > -1) {
9734+
if (selectedIds.indexOf(item.id.toString()) > -1) {
97339735
$option.addClass('selected');
97349736
}
97359737
});

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,9 @@ define('select2/results',[
610610
var self = this;
611611

612612
this.data.current(function (selected) {
613-
selected = $.map(selected, function (s) { return s.id; });
613+
var selectedIds = $.map(selected, function (s) {
614+
return s.id.toString();
615+
});
614616

615617
self.$results.find('.option.selected').removeClass('selected');
616618

@@ -620,7 +622,7 @@ define('select2/results',[
620622
var $option = $(this);
621623
var item = $option.data('data');
622624

623-
if (selected.indexOf(item.id.toString()) > -1) {
625+
if (selectedIds.indexOf(item.id.toString()) > -1) {
624626
$option.addClass('selected');
625627
}
626628
});

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/results.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ define([
4242
var self = this;
4343

4444
this.data.current(function (selected) {
45-
selected = $.map(selected, function (s) { return s.id; });
45+
var selectedIds = $.map(selected, function (s) {
46+
return s.id.toString();
47+
});
4648

4749
self.$results.find('.option.selected').removeClass('selected');
4850

@@ -52,7 +54,7 @@ define([
5254
var $option = $(this);
5355
var item = $option.data('data');
5456

55-
if (selected.indexOf(item.id.toString()) > -1) {
57+
if (selectedIds.indexOf(item.id.toString()) > -1) {
5658
$option.addClass('selected');
5759
}
5860
});

tests/data/array-tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ test('current works with existing selections', function (assert) {
6161
val,
6262
[{
6363
id: '3',
64-
text: 'Three'
64+
text: 'Three',
65+
disabled: false
6566
}],
6667
'The text and id should match the value and text for the option tag.'
6768
);

tests/data/select-tests.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ test('current gets default for single', function (assert) {
1616
val,
1717
[{
1818
id: 'default',
19-
text: 'Default'
19+
text: 'Default',
20+
disabled: false
2021
}],
2122
'The first option should be selected by default (by the browser).'
2223
);
@@ -49,7 +50,8 @@ test('current gets options with explicit value', function (assert) {
4950
val,
5051
[{
5152
id: '1',
52-
text: 'One'
53+
text: 'One',
54+
disabled: false
5355
}],
5456
'The text and id should match the value and text for the option tag.'
5557
);
@@ -68,7 +70,8 @@ test('current gets options with implicit value', function (assert) {
6870
val,
6971
[{
7072
id: '2',
71-
text: '2'
73+
text: '2',
74+
disabled: false
7275
}],
7376
'The text and id should match the text within the option tag.'
7477
);

0 commit comments

Comments
 (0)