gpt4 book ai didi

simple-form - simple_form 集合包装器(单选按钮): double encapsulation of items

转载 作者:行者123 更新时间:2023-12-03 16:56:25 25 4
gpt4 key购买 nike

我想用 simple_form 重现这个 html 单选按钮序列,以便使 simple_form 与 http://semantic-ui.com/ 一起使用句法 :

  <div class="grouped inline fields">
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="fruit" checked="">
<label>Apples</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="fruit">
<label>Oranges</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="fruit">
<label>Pears</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="fruit">
<label>Grapefruit</label>
</div>
</div>
</div>

所以我准备了一个自定义包装器:
config.wrappers :semantic_radios, tag: 'div', class: "grouped fields", error_class:   'error', hint_class: 'with_hint' do |b|
b.use :html5
b.use :label
b.use :input
end

设置一些选项:
config.item_wrapper_tag = :div
config.item_wrapper_class = 'ui radio checkbox'

并以我的形式调用此代码:
=f.input :child_care_type, collection: [["option 1", 1],["option 2", 2]], as: :radio_buttons, wrapper: :semantic_radios

我不知道在哪里自定义 div.field 封装:
    <div class="field">
<div class="ui radio checkbox">
<input type="radio" name="fruit" checked="">
<label>Apples</label>
</div>
</div>

我的代码只呈现这个:
  <div class="ui radio checkbox">
<input type="radio" name="fruit" checked="">
<label>Apples</label>
</div>

你能帮助我吗 ?我没有找到更多包装器的集合定制:s

最佳答案

概括:

我之前做过类似的事情,创建了一个继承自 SimpleForm::Inputs::CollectionRadioButtonsInput 的自定义输入。并且只重载几个方法。有关自定义输入组件的更多信息,请参阅 https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components .

在任何情况下,下面的代码几乎完全使用 simple_form v2.1.0 和 rails v3.2.15 生成您想要的 html 标记。

代码:

# File: app/inputs/semantic_ui_radio_buttons_input.rb

class SemanticUiRadioButtonsInput < SimpleForm::Inputs::CollectionRadioButtonsInput

# Creates a radio button set for use with Semantic UI

def input
label_method, value_method = detect_collection_methods
iopts = {
:checked => 1,
:item_wrapper_tag => 'div',
:item_wrapper_class => 'field',
:collection_wrapper_tag => 'div',
:collection_wrapper_class => 'grouped inline fields'
}
return @builder.send(
"collection_radio_buttons",
attribute_name,
collection,
value_method,
label_method,
iopts,
input_html_options,
&collection_block_for_nested_boolean_style
)
end # method

protected

def build_nested_boolean_style_item_tag(collection_builder)
tag = String.new
tag << '<div class="ui radio checkbox">'.html_safe
tag << collection_builder.radio_button + collection_builder.label
tag << '</div>'.html_safe
return tag.html_safe
end # method

end # class

然后,在您的表单中,只需执行以下操作:
-# File: app/views/<resource>/_form.html.haml

-# Define the collection
- child_care_coll = %w( Infant Toddler Preschool Kindergarten ).map!.with_index(1).to_a

-# Render the radio inputs
= f.input :child_care_type,
:collection => child_care_coll,
:label_method => :first,
:value_method => :last,
:as => :semantic_ui_radio_buttons

结果:
<div class="input semantic_ui_radio_buttons optional childcare_child_care_type">

<label class="semantic_ui_radio_buttons optional control-label">
Child care type
</label>

<div class="grouped inline fields">

<div class="field">
<div class="ui radio checkbox">
<input checked="checked" class="semantic_ui_radio_buttons optional" id="childcare_child_care_type_1" name="childcare[child_care_type]" type="radio" value="1">
<label for="childcare_child_care_type_1">Infant</label>
</div>
</div>

...

<div class="field">
<div class="ui radio checkbox">
<input class="semantic_ui_radio_buttons optional" id="childcare_child_care_type_4" name="childcare[child_care_type]" type="radio" value="4">
<label for="childcare_child_care_type_4">Kindergarten</label>
</div>
</div>

</div>

</div>

enter image description here

关于simple-form - simple_form 集合包装器(单选按钮): double encapsulation of items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com