1
0
mirror of synced 2025-02-16 20:13:16 +03:00

Adding container support #3222

Fixes issue #3222
Now templateSelection can have access to the parent container so that
classes can be added for styling
This commit is contained in:
Stretch 2015-05-07 21:59:51 +10:00
parent ac1680d81d
commit 13cead0bd0
2 changed files with 38 additions and 3 deletions

View File

@ -50,11 +50,11 @@ define([
this.$selection.find('.select2-selection__rendered').empty();
};
MultipleSelection.prototype.display = function (data) {
MultipleSelection.prototype.display = function (data, container) {
var template = this.options.get('templateSelection');
var escapeMarkup = this.options.get('escapeMarkup');
return escapeMarkup(template(data));
return escapeMarkup(template(data, container));
};
MultipleSelection.prototype.selectionContainer = function () {
@ -81,8 +81,8 @@ define([
for (var d = 0; d < data.length; d++) {
var selection = data[d];
var formatted = this.display(selection);
var $selection = this.selectionContainer();
var formatted = this.display(selection, $selection);
$selection.append(formatted);
$selection.prop('title', selection.title || selection.text);

View File

@ -33,6 +33,41 @@ test('display uses templateSelection', function (assert) {
assert.equal(out, 'test');
});
test('templateSelection can addClass', function (assert) {
var called = false, found = false;
var templateOptions = new Options({
templateSelection: function (data, container) {
called = true;
container.addClass('testclass');
return data.text;
}
});
var selection = new MultipleSelection(
$('#qunit-fixture .multiple'),
templateOptions
);
var $container = selection.selectionContainer();
var out = selection.display({
text: 'test'
}, $container);
for (var i = 0; i < $container[0].classList.length; i += 1) {
if ($container[0].classList[i] === 'testclass') {
found = true;
}
}
assert.ok(called);
assert.equal(out, 'test');
assert.ok(called);
});
test('empty update clears the selection', function (assert) {
var selection = new MultipleSelection(
$('#qunit-fixture .multiple'),