Skip to content

Commit 88a86e5

Browse files
committed
add default initselection for single and multi
1 parent 3570ab5 commit 88a86e5

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

select2.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,19 +1663,16 @@ the specific language governing permissions and limitations under the Apache Lic
16631663
} else if ("data" in opts) {
16641664
// install default initSelection when applied to hidden input and data is local
16651665
opts.initSelection = opts.initSelection || function (element, callback) {
1666-
var data,
1667-
id = element.val();
1668-
1669-
$.each(opts.data, function(k, v){
1670-
if(id == opts.id(v)) {
1671-
data = v;
1672-
return false;
1666+
var id = element.val();
1667+
//search in data by id
1668+
opts.query({
1669+
matcher: function(term, text, el){
1670+
return equal(id, opts.id(el));
1671+
},
1672+
callback: !$.isFunction(callback) ? $.noop : function(filtered) {
1673+
callback(filtered.results.length ? filtered.results[0] : null);
16731674
}
16741675
});
1675-
1676-
if ($.isFunction(callback)) {
1677-
callback(data);
1678-
}
16791676
};
16801677
}
16811678

@@ -1864,6 +1861,22 @@ the specific language governing permissions and limitations under the Apache Lic
18641861
if ($.isFunction(callback))
18651862
callback(data);
18661863
};
1864+
} else if ("data" in opts) {
1865+
// install default initSelection when applied to hidden input and data is local
1866+
opts.initSelection = opts.initSelection || function (element, callback) {
1867+
var ids = splitVal(element.val(), opts.separator);
1868+
//search in data by array of ids
1869+
opts.query({
1870+
matcher: function(term, text, el){
1871+
return $.grep(ids, function(id) {
1872+
return equal(id, opts.id(el));
1873+
}).length;
1874+
},
1875+
callback: !$.isFunction(callback) ? $.noop : function(filtered) {
1876+
callback(filtered.results);
1877+
}
1878+
});
1879+
};
18671880
}
18681881

18691882
return opts;

0 commit comments

Comments
 (0)