add default initselection for single and multi
This commit is contained in:
parent
3570ab523e
commit
88a86e5953
35
select2.js
35
select2.js
@ -1663,19 +1663,16 @@ the specific language governing permissions and limitations under the Apache Lic
|
||||
} else if ("data" in opts) {
|
||||
// install default initSelection when applied to hidden input and data is local
|
||||
opts.initSelection = opts.initSelection || function (element, callback) {
|
||||
var data,
|
||||
id = element.val();
|
||||
|
||||
$.each(opts.data, function(k, v){
|
||||
if(id == opts.id(v)) {
|
||||
data = v;
|
||||
return false;
|
||||
var id = element.val();
|
||||
//search in data by id
|
||||
opts.query({
|
||||
matcher: function(term, text, el){
|
||||
return equal(id, opts.id(el));
|
||||
},
|
||||
callback: !$.isFunction(callback) ? $.noop : function(filtered) {
|
||||
callback(filtered.results.length ? filtered.results[0] : null);
|
||||
}
|
||||
});
|
||||
|
||||
if ($.isFunction(callback)) {
|
||||
callback(data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -1864,6 +1861,22 @@ the specific language governing permissions and limitations under the Apache Lic
|
||||
if ($.isFunction(callback))
|
||||
callback(data);
|
||||
};
|
||||
} else if ("data" in opts) {
|
||||
// install default initSelection when applied to hidden input and data is local
|
||||
opts.initSelection = opts.initSelection || function (element, callback) {
|
||||
var ids = splitVal(element.val(), opts.separator);
|
||||
//search in data by array of ids
|
||||
opts.query({
|
||||
matcher: function(term, text, el){
|
||||
return $.grep(ids, function(id) {
|
||||
return equal(id, opts.id(el));
|
||||
}).length;
|
||||
},
|
||||
callback: !$.isFunction(callback) ? $.noop : function(filtered) {
|
||||
callback(filtered.results);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
return opts;
|
||||
|
Loading…
x
Reference in New Issue
Block a user