Skip to content

Commit d54fd4d

Browse files
Dean Soferivaynberg
authored andcommitted
Passing a string to select2('val') with initSelection causes it to be used.
Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
1 parent b672d76 commit d54fd4d

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

select2.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@
14291429

14301430
// single
14311431
val: function () {
1432-
var val, data = null;
1432+
var val, data = null, self = this;
14331433

14341434
if (arguments.length === 0) {
14351435
return this.opts.element.val();
@@ -1448,8 +1448,18 @@
14481448
this.updateSelection(data);
14491449
} else {
14501450
// val is an object. !val is true for [undefined,null,'']
1451-
this.opts.element.val(!val ? "" : this.id(val));
1452-
this.updateSelection(val);
1451+
if (this.opts.initSelection) {
1452+
that = this;
1453+
this.opts.initSelection(this.opts.element.val(val), function(data){
1454+
self.opts.element.val(!data ? "" : self.id(data));
1455+
self.updateSelection(data);
1456+
self.setPlaceholder();
1457+
});
1458+
return;
1459+
} else {
1460+
this.opts.element.val(!val ? "" : this.id(val));
1461+
this.updateSelection(val);
1462+
}
14531463
}
14541464
this.setPlaceholder();
14551465

@@ -1905,11 +1915,21 @@
19051915
});
19061916
this.updateSelection(data);
19071917
} else {
1908-
val = (val === null) ? [] : val;
1909-
// val is a list of objects
1910-
$(val).each(function () { data.push(self.id(this)); });
1911-
this.setVal(data);
1912-
this.updateSelection(val);
1918+
if (this.opts.initSelection) {
1919+
this.opts.initSelection(this.opts.element.val(val), function(newVal){
1920+
$(newVal).each(function () { data.push(self.id(this)); });
1921+
self.setVal(data);
1922+
self.updateSelection(newVal);
1923+
self.clearSearch();
1924+
});
1925+
return;
1926+
} else {
1927+
val = (val === null) ? [] : val;
1928+
// val is a list of objects
1929+
$(val).each(function () { data.push(self.id(this)); });
1930+
this.setVal(data);
1931+
this.updateSelection(val);
1932+
}
19131933
}
19141934

19151935
this.clearSearch();

0 commit comments

Comments
 (0)