代码之家  ›  专栏  ›  技术社区  ›  astropanic

简单的表单、包装单选按钮和复选框

  •  1
  • astropanic  · 技术社区  · 14 年前

    是否有可能将复选框和单选按钮包装在无序列表中?

    我知道这与布局有关,但仍然是一个编程问题。

    3 回复  |  直到 14 年前
        1
  •  2
  •   Adam Pope    13 年前

    作为继续生活的快速技巧,将其添加到application helper的底部可以将每个标签/输入对简单地包装在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
    
        2
  •  2
  •   Max Prokopov    12 年前

    创建新目录“app/inputs”,

    创建“collection_radio_buttons_input.rb”文件,然后粘贴

    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
    

    在config/initializers/simple_form.rb中打开选项“config.inputs_discovery”

    喂!

    现在,将使用此控件而不是默认的单选按钮简单表单控件,您可以自由使用任何格式。

        3
  •  0
  •   Jenny Lang    11 年前

    我刚用CSS做的。我用class=“radio button”在按钮和标签周围打了一个div。然后我将此添加到我的样式表(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;
     }