Created
May 22, 2013 11:48
-
-
Save stex/5626973 to your computer and use it in GitHub Desktop.
Converts rails checkboxes to bootstrap buttonsets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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