generalized selection initialization behind opts.initSelection
This commit is contained in:
parent
d43b8da796
commit
0ca86701ad
51
select2.js
51
select2.js
@ -788,14 +788,32 @@
|
|||||||
this.triggerChange();
|
this.triggerChange();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (this.select) {
|
if ($.isFunction(this.opts.initSelection)) {
|
||||||
selected = this.select.find(":selected");
|
selected = this.opts.initSelection.call(null, this.opts.element);
|
||||||
this.updateSelection({id: selected.attr("value"), text: selected.text()});
|
if (selected !== undefined && selected != null) {
|
||||||
|
this.updateSelection(selected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setPlaceholder();
|
this.setPlaceholder();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
SingleSelect2.prototype.prepareOpts = function () {
|
||||||
|
var opts = this.parent.prepareOpts.apply(this, arguments);
|
||||||
|
|
||||||
|
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
||||||
|
// install sthe selection initializer
|
||||||
|
this.opts.initSelection = function (element) {
|
||||||
|
var selected = element.find(":selected");
|
||||||
|
// a single select box always has a value, no need to null check 'selected'
|
||||||
|
return {id: selected.attr("value"), text: selected.text()};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
SingleSelect2.prototype.setPlaceholder = function () {
|
SingleSelect2.prototype.setPlaceholder = function () {
|
||||||
var placeholder = this.getPlaceholder();
|
var placeholder = this.getPlaceholder();
|
||||||
|
|
||||||
@ -918,6 +936,23 @@
|
|||||||
"</div>"].join(""));
|
"</div>"].join(""));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
MultiSelect2.prototype.prepareOpts = function () {
|
||||||
|
var opts = this.parent.prepareOpts.apply(this, arguments);
|
||||||
|
|
||||||
|
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
||||||
|
// install sthe selection initializer
|
||||||
|
this.opts.initSelection = function (element) {
|
||||||
|
var data = [];
|
||||||
|
element.find(":selected").each(function () {
|
||||||
|
data.push({id: $(this).attr("value"), text: $(this).text()});
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts;
|
||||||
|
};
|
||||||
|
|
||||||
MultiSelect2.prototype.initContainer = function () {
|
MultiSelect2.prototype.initContainer = function () {
|
||||||
|
|
||||||
var selector = ".select2-choices", selection, data;
|
var selector = ".select2-choices", selection, data;
|
||||||
@ -989,14 +1024,12 @@
|
|||||||
this.clearPlaceholder();
|
this.clearPlaceholder();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (this.select) {
|
if ($.isFunction(this.opts.initSelection)) {
|
||||||
data = [];
|
data = this.opts.initSelection.call(null, this.opts.element);
|
||||||
this.select.find(":selected").each(function () {
|
if (data !== undefined && data != null) {
|
||||||
data.push({id: $(this).attr("value"), text: $(this).text()});
|
|
||||||
});
|
|
||||||
|
|
||||||
this.updateSelection(data);
|
this.updateSelection(data);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set the placeholder if necessary
|
// set the placeholder if necessary
|
||||||
this.clearSearch();
|
this.clearSearch();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user