From f63a10ccb1a14d27a11df4b056904b1afad33b9f Mon Sep 17 00:00:00 2001 From: vitalets Date: Wed, 16 Jan 2013 21:29:17 +0400 Subject: [PATCH 1/3] ready for single --- select2.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/select2.js b/select2.js index 9ffc0282..8e0b095a 100644 --- a/select2.js +++ b/select2.js @@ -1660,6 +1660,23 @@ the specific language governing permissions and limitations under the Apache Lic if ($.isFunction(callback)) callback({id: selected.attr("value"), text: selected.text(), element:selected}); }; + } 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; + } + }); + + if ($.isFunction(callback)) { + callback(data); + } + }; } return opts; From 3570ab523e7d038d204805c3e660c91b8715afee Mon Sep 17 00:00:00 2001 From: vitalets Date: Wed, 16 Jan 2013 22:09:31 +0400 Subject: [PATCH 2/3] add third param to matcher in local --- select2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/select2.js b/select2.js index 8e0b095a..03857b3b 100644 --- a/select2.js +++ b/select2.js @@ -380,11 +380,11 @@ the specific language governing permissions and limitations under the Apache Lic } group.children=[]; $(datum.children).each2(function(i, childDatum) { process(childDatum, group.children); }); - if (group.children.length || query.matcher(t, text(group))) { + if (group.children.length || query.matcher(t, text(group), datum)) { collection.push(group); } } else { - if (query.matcher(t, text(datum))) { + if (query.matcher(t, text(datum), datum)) { collection.push(datum); } } From 88a86e595321c557292408eab437ec14371b85fe Mon Sep 17 00:00:00 2001 From: vitalets Date: Wed, 16 Jan 2013 22:35:24 +0400 Subject: [PATCH 3/3] add default initselection for single and multi --- select2.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/select2.js b/select2.js index 03857b3b..e62f453d 100644 --- a/select2.js +++ b/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;