Skip to content

Instantly share code, notes, and snippets.

@stex
Created May 22, 2013 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stex/5626973 to your computer and use it in GitHub Desktop.
Save stex/5626973 to your computer and use it in GitHub Desktop.
Converts rails checkboxes to bootstrap buttonsets.
# Example: Weekday Selection
# #weekdays{:data => {:toggle => :checkboxes}}
# %label Weekdays
# - [:mon, :tue, :wed, :thu, :fri, :sat, :sun].each do |wday|
# = f.label wday
# = f.check_box wday
# Creates a bootstrap buttonset and hides the original checkboxes
#--------------------------------------------------------------
initializeCheckboxButtonsets: () ->
jQuery('[data-toggle=checkboxes]').each () ->
elem = jQuery(@)
tag = jQuery('<div class="btn-group" data-toggle="buttons-checkbox"></div>')
elem.find('input[type=checkbox]').each () ->
checkbox = jQuery(@)
label = elem.find("label[for=#{checkbox.attr('id')}]")
#Create a button with the label's caption
buttonClasses = "btn btn-primary checkbox-button"
buttonClasses += " active" if checkbox.prop('checked')
button = jQuery('<button type="button" class="' + buttonClasses + '">' + label.html() + '</button>')
#Attach an onClick handler that toggles the checkbox
jQuery(button).on 'click', () ->
checkbox.prop('checked', !button.hasClass('active'))
jQuery(tag).append(button)
#Hide checkbox and label
label.hide()
checkbox.hide()
elem.append(tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment