Make initSelection works asynchronously
This commit is contained in:
parent
4ba25b8095
commit
af1c398076
55
select2.js
55
select2.js
@ -509,12 +509,14 @@
|
|||||||
} else if ("tags" in opts) {
|
} else if ("tags" in opts) {
|
||||||
opts.query = tags(opts.tags);
|
opts.query = tags(opts.tags);
|
||||||
opts.createSearchChoice = function (term) { return {id: term, text: term}; };
|
opts.createSearchChoice = function (term) { return {id: term, text: term}; };
|
||||||
opts.initSelection = function (element) {
|
opts.initSelection = function (element, callback) {
|
||||||
var data = [];
|
var data = [];
|
||||||
$(splitVal(element.val(), ",")).each(function () {
|
$(splitVal(element.val(), ",")).each(function () {
|
||||||
data.push({id: this, text: this});
|
data.push({id: this, text: this});
|
||||||
});
|
});
|
||||||
return data;
|
|
||||||
|
if ($.isFunction(callback))
|
||||||
|
callback(data);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -958,15 +960,18 @@
|
|||||||
var selected;
|
var selected;
|
||||||
if (this.opts.element.val() === "") {
|
if (this.opts.element.val() === "") {
|
||||||
this.updateSelection({id: "", text: ""});
|
this.updateSelection({id: "", text: ""});
|
||||||
} else {
|
|
||||||
selected = this.opts.initSelection.call(null, this.opts.element);
|
|
||||||
if (selected !== undefined && selected !== null) {
|
|
||||||
this.updateSelection(selected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.close();
|
this.close();
|
||||||
this.setPlaceholder();
|
this.setPlaceholder();
|
||||||
|
} else {
|
||||||
|
var self = this;
|
||||||
|
this.opts.initSelection.call(null, this.opts.element, function(selected){
|
||||||
|
if (selected !== undefined && selected !== null) {
|
||||||
|
self.updateSelection(selected);
|
||||||
|
self.close();
|
||||||
|
self.setPlaceholder();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
prepareOpts: function () {
|
prepareOpts: function () {
|
||||||
@ -974,10 +979,11 @@
|
|||||||
|
|
||||||
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
||||||
// install sthe selection initializer
|
// install sthe selection initializer
|
||||||
opts.initSelection = function (element) {
|
opts.initSelection = function (element, callback) {
|
||||||
var selected = element.find(":selected");
|
var selected = element.find(":selected");
|
||||||
// a single select box always has a value, no need to null check 'selected'
|
// a single select box always has a value, no need to null check 'selected'
|
||||||
return {id: selected.attr("value"), text: selected.text()};
|
if ($.isFunction(callback))
|
||||||
|
callback({id: selected.attr("value"), text: selected.text()});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1116,12 +1122,14 @@
|
|||||||
|
|
||||||
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
if (opts.element.get(0).tagName.toLowerCase() === "select") {
|
||||||
// install sthe selection initializer
|
// install sthe selection initializer
|
||||||
opts.initSelection = function (element) {
|
opts.initSelection = function (element,callback) {
|
||||||
var data = [];
|
var data = [];
|
||||||
element.find(":selected").each(function () {
|
element.find(":selected").each(function () {
|
||||||
data.push({id: $(this).attr("value"), text: $(this).text()});
|
data.push({id: $(this).attr("value"), text: $(this).text()});
|
||||||
});
|
});
|
||||||
return data;
|
|
||||||
|
if ($.isFunction(callback))
|
||||||
|
callback(data);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1228,18 +1236,21 @@
|
|||||||
var data;
|
var data;
|
||||||
if (this.opts.element.val() === "") {
|
if (this.opts.element.val() === "") {
|
||||||
this.updateSelection([]);
|
this.updateSelection([]);
|
||||||
}
|
|
||||||
if (this.select || this.opts.element.val() !== "") {
|
|
||||||
data = this.opts.initSelection.call(null, this.opts.element);
|
|
||||||
if (data !== undefined && data !== null) {
|
|
||||||
this.updateSelection(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.close();
|
this.close();
|
||||||
|
|
||||||
// set the placeholder if necessary
|
// set the placeholder if necessary
|
||||||
this.clearSearch();
|
this.clearSearch();
|
||||||
|
}
|
||||||
|
if (this.select || this.opts.element.val() !== "") {
|
||||||
|
var self = this;
|
||||||
|
this.opts.initSelection.call(null, this.opts.element, function(data){
|
||||||
|
if (data !== undefined && data !== null) {
|
||||||
|
self.updateSelection(data);
|
||||||
|
self.close();
|
||||||
|
// set the placeholder if necessary
|
||||||
|
self.clearSearch();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
clearSearch: function () {
|
clearSearch: function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user