Skip to content

Instantly share code, notes, and snippets.

@dmarchuk
Created November 28, 2017 10:46
Show Gist options
  • Save dmarchuk/53c480e1e1ba7339cbb8d0fe19d9375d to your computer and use it in GitHub Desktop.
Save dmarchuk/53c480e1e1ba7339cbb8d0fe19d9375d to your computer and use it in GitHub Desktop.
/**
* Override default 'renderItems()' function of selectivity
* to accept option with value='' (empty string, item.id === '') as a valid option.
*
* @param items Array of result items.
* @returns HTML-formatted string to display the result items.
*/
$.Selectivity.Dropdown.prototype.renderItems = function (items) {
const selectivity = this.selectivity;
const originalEl = selectivity.options.element;
const $optionElements = $(originalEl.options);
const hasClass = $(originalEl).find('[data-class]').length;
return items.map(function (item) {
if (hasClass) {
$optionElements.each(function (i, el) {
const $el = $(el);
const id = $el.val();
if (id == item.id) {
item.cssClass = $(el).data('class');
return false
}
});
}
let result = selectivity.template(item.id === '' || item.id ? 'resultItem' : 'resultLabel', item);
if (item.children) {
result += selectivity.template('resultChildren', {
childrenHtml: this.renderItems(item.children),
});
}
return result;
}, this).join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment