Skip to content

Commit b917754

Browse files
committed
Merge pull request select2#3122 from cristi-badila/master
Fixes issues with inputData module. When unselecting an item it would previously unselect all items
2 parents fff04c3 + 24f3c47 commit b917754

6 files changed

Lines changed: 42 additions & 9 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5115,14 +5115,14 @@ define('select2/compat/inputData',[
51155115
this.current(function (allData) {
51165116
var values = [];
51175117

5118-
for (var d = 0; d < allData; d++) {
5118+
for (var d = 0; d < allData.length; d++) {
51195119
var item = allData[d];
51205120

51215121
if (data.id == item.id) {
51225122
continue;
51235123
}
51245124

5125-
values.push(data.id);
5125+
values.push(item.id);
51265126
}
51275127

51285128
self.$element.val(values.join(self._valueSeparator));

dist/js/select2.full.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5554,14 +5554,14 @@ define('select2/compat/inputData',[
55545554
this.current(function (allData) {
55555555
var values = [];
55565556

5557-
for (var d = 0; d < allData; d++) {
5557+
for (var d = 0; d < allData.length; d++) {
55585558
var item = allData[d];
55595559

55605560
if (data.id == item.id) {
55615561
continue;
55625562
}
55635563

5564-
values.push(data.id);
5564+
values.push(item.id);
55655565
}
55665566

55675567
self.$element.val(values.join(self._valueSeparator));

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.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/compat/inputData.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ define([
8282
this.current(function (allData) {
8383
var values = [];
8484

85-
for (var d = 0; d < allData; d++) {
85+
for (var d = 0; d < allData.length; d++) {
8686
var item = allData[d];
8787

8888
if (data.id == item.id) {
8989
continue;
9090
}
9191

92-
values.push(data.id);
92+
values.push(item.id);
9393
}
9494

9595
self.$element.val(values.join(self._valueSeparator));

tests/data/inputData-tests.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test('test that options can be selected', function (assert) {
3434
);
3535
});
3636

37-
test('test that options can be unselected', function (assert) {
37+
test('unselect the single selected option clears the value', function (assert) {
3838
var options = new Options({
3939
data: [
4040
{
@@ -59,6 +59,39 @@ test('test that options can be unselected', function (assert) {
5959
);
6060
});
6161

62+
test('options can be unselected individually', function (assert) {
63+
var options = new Options({
64+
data: [
65+
{
66+
id: 'test',
67+
text: 'Test'
68+
},
69+
{
70+
id: 'test2',
71+
text: 'Test2'
72+
},
73+
{
74+
id: 'test3',
75+
text: 'Test3'
76+
}
77+
]
78+
});
79+
var $element = $('<input />');
80+
$element.val('test,test2,test3');
81+
82+
var adapter = new InputAdapter($element, options);
83+
84+
adapter.unselect({
85+
id: 'test2'
86+
});
87+
88+
assert.equal(
89+
$element.val(),
90+
'test,test3',
91+
'The value should contain all the still selected options'
92+
);
93+
});
94+
6295
test('default values can be set', function (assert) {
6396
expect(4);
6497

0 commit comments

Comments
 (0)