Question

Is there a possibility to wrap checkboxes and radio buttons together in a unordered list ?

When not, How I can display them vertically ?

I know this is layout related, but still a programming question.

Was it helpful?

Solution

As a quick hack to move on with life, adding this to the bottom of application helper works to simple wrap each label/input pair in a div:

module SimpleForm::ActionViewExtensions::Builder

  def collection_radio(attribute, collection, value_method, text_method, options={}, html_options={})
    collection.map do |item|
      value = item.send value_method
      text  = item.send text_method

      default_html_options = default_html_options_for_collection(item, value, options, html_options)

      @template.content_tag(:div, radio_button(attribute, value, default_html_options) <<
        label("#{attribute}_#{value}", text, :class => "collection_radio"))
    end.join.html_safe
  end

  def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})
    collection.map do |item|
      value = item.send value_method
      text  = item.send text_method

      default_html_options = default_html_options_for_collection(item, value, options, html_options)
      default_html_options[:multiple] = true

      @template.content_tag(:div, 
      check_box(attribute, default_html_options, value, '') <<
        label("#{attribute}_#{value}", text, :class => "collection_check_boxes"))
    end.join.html_safe
  end

end

OTHER TIPS

It's more clean and better way to redefine inputs like following:

create new directory 'app/inputs',

create file 'collection_radio_buttons_input.rb' in it, paste following

class CollectionRadioButtonsInput < SimpleForm::Inputs::CollectionRadioButtonsInput
  def item_wrapper_class
    "radiobox"
  end
  def build_nested_boolean_style_item_tag(collection_builder)
    collection_builder.radio_button + template.content_tag(:span,collection_builder.text)
  end
end

Turn on option 'config.inputs_discovery' in config/initializers/simple_form.rb

Voila!

Now, this control will be used instead of default RadioButtons simple_form control, and you're free to use any of formatting.

I just did it with CSS. I slapped a div with class="radio-buttons" around the buttons and label. Then I added this to my style sheet (SASS):

.radio-buttons {
  margin: .5em 0;
  span input {
    float: left;
    margin-right: .25em;
    margin-top: -.25em;
  }
  #this grabs the MAIN label only for my radio buttons 
  #since the data type for the table column is string--yours may be different
  label.string { 
    margin-bottom: .5em !important;
  }

  clear: both;
}

.form-block {
  clear: both;
  margin-top: .5em;
}

 .radio-buttons span {
  clear: both;
  display:block;
 }

This will make the radio buttons inline on ALL frameworks, though this is tweaked to look the best for Zurb Foundation. ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top