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) {
|
} else if ("data" in opts) {
|
||||||
// install default initSelection when applied to hidden input and data is local
|
// install default initSelection when applied to hidden input and data is local
|
||||||
opts.initSelection = opts.initSelection || function (element, callback) {
|
opts.initSelection = opts.initSelection || function (element, callback) {
|
||||||
var data,
|
var id = element.val();
|
||||||
id = element.val();
|
//search in data by id
|
||||||
|
opts.query({
|
||||||
$.each(opts.data, function(k, v){
|
matcher: function(term, text, el){
|
||||||
if(id == opts.id(v)) {
|
return equal(id, opts.id(el));
|
||||||
data = v;
|
},
|
||||||
return false;
|
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))
|
if ($.isFunction(callback))
|
||||||
callback(data);
|
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;
|
return opts;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user