浏览代码

add default initselection for single and multi

vitalets 12 年之前
父节点
当前提交
88a86e5953
共有 1 个文件被更改,包括 24 次插入11 次删除
  1. 24 11
      select2.js

+ 24 - 11
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;