simple_form, оберните радиокнопки и флажки

Есть ли возможность объединить флажки и переключатели в неупорядоченный список?

Когда нет, как я могу отображать их вертикально?

Я знаю, что это связано с макетом, но все же вопрос программирования.


person astropanic    schedule 21.10.2010    source источник


Ответы (3)


В качестве быстрого хака, чтобы двигаться дальше, добавление этого в нижнюю часть помощника приложения работает, чтобы просто обернуть каждую пару метка/ввод в 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
person davidfurber    schedule 27.10.2010
comment
Спасибо, нашел в форке Свена Фукса - person astropanic; 02.11.2010

Это более чистый и лучший способ переопределить входные данные, например:

создать новый каталог «приложение/входы»,

создайте в нем файл 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.inputs_discovery» в config/initializers/simple_form.rb.

Вуаля!

Теперь этот элемент управления будет использоваться вместо элемента управления RadioButtons по умолчанию simple_form, и вы можете использовать любое форматирование.

person Max Prokopov    schedule 25.11.2012

Я только что сделал это с помощью CSS. Я налепил div с class="radio-buttons" вокруг кнопок и ярлыка. Затем я добавил это в свою таблицу стилей (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;
 }

Это сделает радиокнопки встроенными во ВСЕ фреймворки, хотя это настроено так, чтобы оно выглядело лучше всего для Zurb Foundation. ;)

person Jenny Lang    schedule 28.09.2013