Skip to content

Commit a3fa516

Browse files
committed
Added basic templating to results
1 parent be4d091 commit a3fa516

9 files changed

Lines changed: 69 additions & 18 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ define('select2/results',[
264264
.removeAttr('aria-selected');
265265

266266
var $label = $('<strong class="group-label"></strong>');
267-
$label.html(data.text);
267+
this.template(data, $label);
268268

269269
var $children = [];
270270

@@ -283,7 +283,7 @@ define('select2/results',[
283283
$option.append($label);
284284
$option.append($childrenContainer);
285285
} else {
286-
$option.html(data.text);
286+
this.template(data, $option);
287287
}
288288

289289
if (data.disabled) {
@@ -513,6 +513,12 @@ define('select2/results',[
513513
}
514514
};
515515

516+
Results.prototype.template = function (result, $container) {
517+
var template = this.options.get('templateResult');
518+
519+
$container.html(template(result));
520+
};
521+
516522
return Results;
517523
});
518524

@@ -1616,7 +1622,10 @@ define('select2/defaults',[
16161622

16171623
Defaults.prototype.reset = function () {
16181624
this.defaults = {
1619-
language: ['select2/i18n/en']
1625+
language: ['select2/i18n/en'],
1626+
templateResult: function (result) {
1627+
return result.text;
1628+
}
16201629
};
16211630
};
16221631

dist/js/select2.amd.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ define('select2/results',[
264264
.removeAttr('aria-selected');
265265

266266
var $label = $('<strong class="group-label"></strong>');
267-
$label.html(data.text);
267+
this.template(data, $label);
268268

269269
var $children = [];
270270

@@ -283,7 +283,7 @@ define('select2/results',[
283283
$option.append($label);
284284
$option.append($childrenContainer);
285285
} else {
286-
$option.html(data.text);
286+
this.template(data, $option);
287287
}
288288

289289
if (data.disabled) {
@@ -513,6 +513,12 @@ define('select2/results',[
513513
}
514514
};
515515

516+
Results.prototype.template = function (result, $container) {
517+
var template = this.options.get('templateResult');
518+
519+
$container.html(template(result));
520+
};
521+
516522
return Results;
517523
});
518524

@@ -1616,7 +1622,10 @@ define('select2/defaults',[
16161622

16171623
Defaults.prototype.reset = function () {
16181624
this.defaults = {
1619-
language: ['select2/i18n/en']
1625+
language: ['select2/i18n/en'],
1626+
templateResult: function (result) {
1627+
return result.text;
1628+
}
16201629
};
16211630
};
16221631

dist/js/select2.full.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9799,7 +9799,7 @@ define('select2/results',[
97999799
.removeAttr('aria-selected');
98009800

98019801
var $label = $('<strong class="group-label"></strong>');
9802-
$label.html(data.text);
9802+
this.template(data, $label);
98039803

98049804
var $children = [];
98059805

@@ -9818,7 +9818,7 @@ define('select2/results',[
98189818
$option.append($label);
98199819
$option.append($childrenContainer);
98209820
} else {
9821-
$option.html(data.text);
9821+
this.template(data, $option);
98229822
}
98239823

98249824
if (data.disabled) {
@@ -10048,6 +10048,12 @@ define('select2/results',[
1004810048
}
1004910049
};
1005010050

10051+
Results.prototype.template = function (result, $container) {
10052+
var template = this.options.get('templateResult');
10053+
10054+
$container.html(template(result));
10055+
};
10056+
1005110057
return Results;
1005210058
});
1005310059

@@ -11151,7 +11157,10 @@ define('select2/defaults',[
1115111157

1115211158
Defaults.prototype.reset = function () {
1115311159
this.defaults = {
11154-
language: ['select2/i18n/en']
11160+
language: ['select2/i18n/en'],
11161+
templateResult: function (result) {
11162+
return result.text;
11163+
}
1115511164
};
1115611165
};
1115711166

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ define('select2/results',[
692692
.removeAttr('aria-selected');
693693

694694
var $label = $('<strong class="group-label"></strong>');
695-
$label.html(data.text);
695+
this.template(data, $label);
696696

697697
var $children = [];
698698

@@ -711,7 +711,7 @@ define('select2/results',[
711711
$option.append($label);
712712
$option.append($childrenContainer);
713713
} else {
714-
$option.html(data.text);
714+
this.template(data, $option);
715715
}
716716

717717
if (data.disabled) {
@@ -941,6 +941,12 @@ define('select2/results',[
941941
}
942942
};
943943

944+
Results.prototype.template = function (result, $container) {
945+
var template = this.options.get('templateResult');
946+
947+
$container.html(template(result));
948+
};
949+
944950
return Results;
945951
});
946952

@@ -2044,7 +2050,10 @@ define('select2/defaults',[
20442050

20452051
Defaults.prototype.reset = function () {
20462052
this.defaults = {
2047-
language: ['select2/i18n/en']
2053+
language: ['select2/i18n/en'],
2054+
templateResult: function (result) {
2055+
return result.text;
2056+
}
20482057
};
20492058
};
20502059

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ <h2>Example code</h2>
462462
return data.items;
463463
},
464464
cache: true
465+
},
466+
templateResult: function (repo) {
467+
return repo.full_name;
468+
},
469+
templateSelection: function (repo) {
470+
return repo.full_name;
465471
}
466472
});
467473

src/js/select2/defaults.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ define([
113113

114114
Defaults.prototype.reset = function () {
115115
this.defaults = {
116-
language: ['select2/i18n/en']
116+
language: ['select2/i18n/en'],
117+
templateResult: function (result) {
118+
return result.text;
119+
}
117120
};
118121
};
119122

src/js/select2/results.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ define([
110110
.removeAttr('aria-selected');
111111

112112
var $label = $('<strong class="group-label"></strong>');
113-
$label.html(data.text);
113+
this.template(data, $label);
114114

115115
var $children = [];
116116

@@ -129,7 +129,7 @@ define([
129129
$option.append($label);
130130
$option.append($childrenContainer);
131131
} else {
132-
$option.html(data.text);
132+
this.template(data, $option);
133133
}
134134

135135
if (data.disabled) {
@@ -359,5 +359,11 @@ define([
359359
}
360360
};
361361

362+
Results.prototype.template = function (result, $container) {
363+
var template = this.options.get('templateResult');
364+
365+
$container.html(template(result));
366+
};
367+
362368
return Results;
363369
});

0 commit comments

Comments
 (0)