Skip to content

Allow render functions in options to return DOM. #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,31 +1008,34 @@ $.extend(Selectize.prototype, {
optgroup = '';
}
if (!groups.hasOwnProperty(optgroup)) {
groups[optgroup] = [];
groups[optgroup] = document.createDocumentFragment();
groups_order.push(optgroup);
}
groups[optgroup].push(option_html);
groups[optgroup].appendChild(option_html);
}
}

// render optgroup headers & join groups
html = [];
html = document.createDocumentFragment();
for (i = 0, n = groups_order.length; i < n; i++) {
optgroup = groups_order[i];
if (self.optgroups.hasOwnProperty(optgroup) && groups[optgroup].length) {
if (self.optgroups.hasOwnProperty(optgroup) && groups[optgroup].childNodes.length) {
// render the optgroup header and options within it,
// then pass it to the wrapper template
html_children = self.render('optgroup_header', self.optgroups[optgroup]) || '';
html_children += groups[optgroup].join('');
html.push(self.render('optgroup', $.extend({}, self.optgroups[optgroup], {
html: html_children
html_children = document.createDocumentFragment();
html_children.appendChild(self.render('optgroup_header', self.optgroups[optgroup]));
html_children.appendChild(groups[optgroup]);

html.appendChild(self.render('optgroup', $.extend({}, self.optgroups[optgroup], {
html: domToString(html_children),
dom: html_children
})));
} else {
html.push(groups[optgroup].join(''));
html.appendChild(groups[optgroup]);
}
}

$dropdown_content.html(html.join(''));
$dropdown_content.html(html);

// highlight matching terms inline
if (self.settings.highlight && results.query.length && results.tokens.length) {
Expand Down Expand Up @@ -1906,26 +1909,26 @@ $.extend(Selectize.prototype, {
}

// render markup
html = self.settings.render[templateName].apply(this, [data, escape_html]);
html = $(self.settings.render[templateName].apply(this, [data, escape_html]));

// add mandatory attributes
if (templateName === 'option' || templateName === 'option_create') {
html = html.replace(regex_tag, '<$1 data-selectable');
html.attr('data-selectable', '');
}
if (templateName === 'optgroup') {
else if (templateName === 'optgroup') {
id = data[self.settings.optgroupValueField] || '';
html = html.replace(regex_tag, '<$1 data-group="' + escape_replace(escape_html(id)) + '"');
html.attr('data-group', id);
}
if (templateName === 'option' || templateName === 'item') {
html = html.replace(regex_tag, '<$1 data-value="' + escape_replace(escape_html(value || '')) + '"');
html.attr('data-value', value || '');
}

// update cache
if (cache) {
self.renderCache[templateName][value] = html;
self.renderCache[templateName][value] = html[0];
}

return html;
return html[0];
},

/**
Expand Down
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,12 @@ var autoGrow = function($input) {

$input.on('keydown keyup update blur', update);
update();
};
};

var domToString = function(d) {
var tmp = document.createElement('div');

tmp.appendChild(d.cloneNode(true));

return tmp.innerHTML;
};
57 changes: 57 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,63 @@
});
});

describe('<select> (custom string render)', function() {
var test;

beforeEach(function() {
test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'</select>', {
render: {
option: function(item, escape) {
return '<div class="option custom-option">' + escape(item.text) + '</div>'
}
}
});
});

it('should render the custom option element', function(done) {
test.selectize.focus();

window.setTimeout(function() {
expect(test.selectize.$dropdown.find('.custom-option').length).to.be.equal(1);
done();
}, 5);
});
});

describe('<select> (custom dom render)', function() {
var test;

beforeEach(function() {
test = setup_test('<select>' +
'<option value="">Select an option...</option>' +
'<option value="a">A</option>' +
'</select>', {
render: {
option: function(item, escape) {
var div = document.createElement('div');

div.className = 'option custom-option';
div.innerHTML = escape(item.text);

return div;
}
}
});
});

it('should render the custom option element', function(done) {
test.selectize.focus();

window.setTimeout(function() {
expect(test.selectize.$dropdown_content.find('.custom-option').length).to.be.equal(1);
done();
}, 0);
});
});

});

})();