Passing a string to select2('val') with initSelection causes it to be used.
Signed-off-by: Igor Vaynberg <igor.vaynberg@gmail.com>
This commit is contained in:
parent
b672d760ac
commit
d54fd4dd0e
22
select2.js
22
select2.js
@ -1429,7 +1429,7 @@
|
|||||||
|
|
||||||
// single
|
// single
|
||||||
val: function () {
|
val: function () {
|
||||||
var val, data = null;
|
var val, data = null, self = this;
|
||||||
|
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
return this.opts.element.val();
|
return this.opts.element.val();
|
||||||
@ -1448,9 +1448,19 @@
|
|||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
} else {
|
} else {
|
||||||
// val is an object. !val is true for [undefined,null,'']
|
// val is an object. !val is true for [undefined,null,'']
|
||||||
|
if (this.opts.initSelection) {
|
||||||
|
that = this;
|
||||||
|
this.opts.initSelection(this.opts.element.val(val), function(data){
|
||||||
|
self.opts.element.val(!data ? "" : self.id(data));
|
||||||
|
self.updateSelection(data);
|
||||||
|
self.setPlaceholder();
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
this.opts.element.val(!val ? "" : this.id(val));
|
this.opts.element.val(!val ? "" : this.id(val));
|
||||||
this.updateSelection(val);
|
this.updateSelection(val);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
this.setPlaceholder();
|
this.setPlaceholder();
|
||||||
|
|
||||||
},
|
},
|
||||||
@ -1904,6 +1914,15 @@
|
|||||||
data.push({id: $(this).attr("value"), text: $(this).text()});
|
data.push({id: $(this).attr("value"), text: $(this).text()});
|
||||||
});
|
});
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
|
} else {
|
||||||
|
if (this.opts.initSelection) {
|
||||||
|
this.opts.initSelection(this.opts.element.val(val), function(newVal){
|
||||||
|
$(newVal).each(function () { data.push(self.id(this)); });
|
||||||
|
self.setVal(data);
|
||||||
|
self.updateSelection(newVal);
|
||||||
|
self.clearSearch();
|
||||||
|
});
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
val = (val === null) ? [] : val;
|
val = (val === null) ? [] : val;
|
||||||
// val is a list of objects
|
// val is a list of objects
|
||||||
@ -1911,6 +1930,7 @@
|
|||||||
this.setVal(data);
|
this.setVal(data);
|
||||||
this.updateSelection(val);
|
this.updateSelection(val);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.clearSearch();
|
this.clearSearch();
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user