simple_form, обернуть радиопроизводительные кнопки и флажки
-
10-10-2019 - |
Вопрос
Есть ли возможность обернуть флажки и радиопроизводительные кнопки вместе в неупорядоченном списке?
Когда нет, как я могу отобразить их вертикально?
Я знаю, что это связано с макетом, но все же вопрос программирования.
Решение
Как быстрый взлом, чтобы двигаться дальше с жизнью, добавление этого в нижнюю часть приложения 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
Другие советы
Это более чистый и лучший способ переопределить входные данные, как следующее:
Создать новый каталог «приложение/входы»,
Создать файл '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 по умолчанию, и вы можете использовать любое из форматирования.
Я только что сделал это с CSS. Я хлопнул 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;
}
Это сделает радиопроизводительные кнопки встроенными на всех фреймворках, хотя это настраивается, чтобы выглядеть лучше всего для Zurb Foundation. ;)