Browse Source

Added basic templating to results

Kevin Brown 10 years ago
parent
commit
a3fa516761

+ 12 - 3
dist/js/select2.amd.full.js

@@ -264,7 +264,7 @@ define('select2/results',[
         .removeAttr('aria-selected');
 
       var $label = $('<strong class="group-label"></strong>');
-      $label.html(data.text);
+      this.template(data, $label);
 
       var $children = [];
 
@@ -283,7 +283,7 @@ define('select2/results',[
       $option.append($label);
       $option.append($childrenContainer);
     } else {
-      $option.html(data.text);
+      this.template(data, $option);
     }
 
     if (data.disabled) {
@@ -513,6 +513,12 @@ define('select2/results',[
     }
   };
 
+  Results.prototype.template = function (result, $container) {
+    var template = this.options.get('templateResult');
+
+    $container.html(template(result));
+  };
+
   return Results;
 });
 
@@ -1616,7 +1622,10 @@ define('select2/defaults',[
 
   Defaults.prototype.reset = function () {
     this.defaults = {
-      language: ['select2/i18n/en']
+      language: ['select2/i18n/en'],
+      templateResult: function (result) {
+        return result.text;
+      }
     };
   };
 

+ 12 - 3
dist/js/select2.amd.js

@@ -264,7 +264,7 @@ define('select2/results',[
         .removeAttr('aria-selected');
 
       var $label = $('<strong class="group-label"></strong>');
-      $label.html(data.text);
+      this.template(data, $label);
 
       var $children = [];
 
@@ -283,7 +283,7 @@ define('select2/results',[
       $option.append($label);
       $option.append($childrenContainer);
     } else {
-      $option.html(data.text);
+      this.template(data, $option);
     }
 
     if (data.disabled) {
@@ -513,6 +513,12 @@ define('select2/results',[
     }
   };
 
+  Results.prototype.template = function (result, $container) {
+    var template = this.options.get('templateResult');
+
+    $container.html(template(result));
+  };
+
   return Results;
 });
 
@@ -1616,7 +1622,10 @@ define('select2/defaults',[
 
   Defaults.prototype.reset = function () {
     this.defaults = {
-      language: ['select2/i18n/en']
+      language: ['select2/i18n/en'],
+      templateResult: function (result) {
+        return result.text;
+      }
     };
   };
 

+ 12 - 3
dist/js/select2.full.js

@@ -9799,7 +9799,7 @@ define('select2/results',[
         .removeAttr('aria-selected');
 
       var $label = $('<strong class="group-label"></strong>');
-      $label.html(data.text);
+      this.template(data, $label);
 
       var $children = [];
 
@@ -9818,7 +9818,7 @@ define('select2/results',[
       $option.append($label);
       $option.append($childrenContainer);
     } else {
-      $option.html(data.text);
+      this.template(data, $option);
     }
 
     if (data.disabled) {
@@ -10048,6 +10048,12 @@ define('select2/results',[
     }
   };
 
+  Results.prototype.template = function (result, $container) {
+    var template = this.options.get('templateResult');
+
+    $container.html(template(result));
+  };
+
   return Results;
 });
 
@@ -11151,7 +11157,10 @@ define('select2/defaults',[
 
   Defaults.prototype.reset = function () {
     this.defaults = {
-      language: ['select2/i18n/en']
+      language: ['select2/i18n/en'],
+      templateResult: function (result) {
+        return result.text;
+      }
     };
   };
 

File diff suppressed because it is too large
+ 0 - 0
dist/js/select2.full.min.js


+ 12 - 3
dist/js/select2.js

@@ -692,7 +692,7 @@ define('select2/results',[
         .removeAttr('aria-selected');
 
       var $label = $('<strong class="group-label"></strong>');
-      $label.html(data.text);
+      this.template(data, $label);
 
       var $children = [];
 
@@ -711,7 +711,7 @@ define('select2/results',[
       $option.append($label);
       $option.append($childrenContainer);
     } else {
-      $option.html(data.text);
+      this.template(data, $option);
     }
 
     if (data.disabled) {
@@ -941,6 +941,12 @@ define('select2/results',[
     }
   };
 
+  Results.prototype.template = function (result, $container) {
+    var template = this.options.get('templateResult');
+
+    $container.html(template(result));
+  };
+
   return Results;
 });
 
@@ -2044,7 +2050,10 @@ define('select2/defaults',[
 
   Defaults.prototype.reset = function () {
     this.defaults = {
-      language: ['select2/i18n/en']
+      language: ['select2/i18n/en'],
+      templateResult: function (result) {
+        return result.text;
+      }
     };
   };
 

File diff suppressed because it is too large
+ 0 - 0
dist/js/select2.min.js


+ 6 - 0
docs/examples.html

@@ -462,6 +462,12 @@ $.fn.select2.amd.require(["select2/core", "select2/utils"], function (Select2, U
         return data.items;
       },
       cache: true
+    },
+    templateResult: function (repo) {
+      return repo.full_name;
+    },
+    templateSelection: function (repo) {
+      return repo.full_name;
     }
   });
 

+ 4 - 1
src/js/select2/defaults.js

@@ -113,7 +113,10 @@ define([
 
   Defaults.prototype.reset = function () {
     this.defaults = {
-      language: ['select2/i18n/en']
+      language: ['select2/i18n/en'],
+      templateResult: function (result) {
+        return result.text;
+      }
     };
   };
 

+ 8 - 2
src/js/select2/results.js

@@ -110,7 +110,7 @@ define([
         .removeAttr('aria-selected');
 
       var $label = $('<strong class="group-label"></strong>');
-      $label.html(data.text);
+      this.template(data, $label);
 
       var $children = [];
 
@@ -129,7 +129,7 @@ define([
       $option.append($label);
       $option.append($childrenContainer);
     } else {
-      $option.html(data.text);
+      this.template(data, $option);
     }
 
     if (data.disabled) {
@@ -359,5 +359,11 @@ define([
     }
   };
 
+  Results.prototype.template = function (result, $container) {
+    var template = this.options.get('templateResult');
+
+    $container.html(template(result));
+  };
+
   return Results;
 });

Some files were not shown because too many files changed in this diff