Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds the ability to render html in the list.
$ = jQuery;
$.fn.extend({
chosenDataHtml: function(options) {
return this.each(function(input_field) {
var $this = $(this),
formatSelection = options['formatSelection'] || function(e, data) {
var chosen = data.chosen,
selected = chosen.form_field_jq.find("option:enabled[value!='']:selected:first"),
html = selected.data('html');
if ( html ) {
chosen.container.find('.chosen-single').find('> span:first').html(html);
if ( selected.length )
chosen.container.find('.chosen-single').removeClass('chosen-default')
}
},
formatList = options['formatList'] || function(e, data) {
var chosen = data.chosen,
options = chosen.form_field.options,
isFirstEmpty = options[0].value === '' ? 1
: 0 ;
return $(chosen.container).find('.chosen-drop ul .group-option').each(function(index){
$(this).html($(options[index + isFirstEmpty]).data('html') || $(this).html())
});
};
if ( options !== "destroy") {
$this
.on("chosen:ready", formatSelection)
.on("chosen:hiding_dropdown", formatSelection)
.on("chosen:showing_dropdown", formatList);
}
return $this.chosen(options);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment