Skip to content

Instantly share code, notes, and snippets.

@klebervirgilio
Created November 10, 2015 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klebervirgilio/299caa8e9eaa67160d99 to your computer and use it in GitHub Desktop.
Save klebervirgilio/299caa8e9eaa67160d99 to your computer and use it in GitHub Desktop.
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