简体   繁体   English

如何使用simple_form中的twitter bootstrap兼容复选框标签?

[英]Howto make twitter bootstrap compatible checkbox labels in simple_form?

I'm trying to get two labels for one check box as described in Twitter Bootstrap's documentation. 我正在尝试为Twitter Bootstrap的文档中描述的一个复选框获取两个标签。 (see http://twitter.github.com/bootstrap/base-css.html#forms --> horizontal forms --> checkbox (below text input)) (请参阅http://twitter.github.com/bootstrap/base-css.html#forms - >水平表单 - >复选框(在文本输入下方))

So what I want to display is a label for the description on the left, the check box itself and a hint right next to it on the right. 所以我想要显示的是左侧描述的标签,复选框本身以及右侧的旁边的提示。

The standard implementation of twitter bootstrap in simple_form gem creates a <p> tag for displaying the hint since it tries to be consistent for all kinds of inputs. simple_form gem中twitter bootstrap的标准实现创建了一个<p>标签,用于显示提示,因为它试图对所有类型的输入保持一致。

I now want to create a custom wrapper for "bootstrap checkboxes" in the simple_form initializer but I just cannot figure out how to solve this. 我现在想在simple_form初始化程序中为“bootstrap checkboxes”创建一个自定义包装器,但我无法弄清楚如何解决这个问题。

This is how I currently implemented it using bare rails (erb): 这就是我目前使用裸轨(erb)实现的方法:

<div class="control-group">
 <%= f.label :recurring, :class => 'control-label' %>
 <div class="controls">
   <%= f.label :recurring, :class => 'checkbox' do %>
    <%= f.check_box :recurring %>
    <%= t('.recurring_hint') %>
   <% end %>
 </div>
</div>

Can you help me or at least try to explain how these custom wrapper thing works? 你能帮助我或者至少试着解释这些自定义包装器的工作原理吗? Thank you! 谢谢!

Edit: Let me ask my question more precisely: Can I use simple_form's wrappers API to use a label as a wrapper? 编辑:让我更准确地问我的问题:我可以使用simple_form的包装器API将标签用作包装器吗?

Solution: 解:

  1. Update to newest version of simple_form (2.0.2, had 2.0) 更新到最新版本的simple_form(2.0.2,有2.0)
  2. Override BooleanInput: 覆盖BooleanInput:

app/inputs/boolean_input.rb 应用程序/输入/ boolean_input.rb

class BooleanInput < SimpleForm::Inputs::BooleanInput
  def input
    if nested_boolean_style?
        template.label_tag(nil, :class => "checkbox") {
          build_check_box + inline_label
        }
    else
      build_check_box
    end
  end
end

_form.html.erb _form.html.erb

Call in template: 在模板中调用:

<%= f.input :recurring, :inline_label => t('.recurring_hint') %>  

使用最新的simple_form ,该选项可立即使用。

You can install simple_form with bootstrap suport, so you don't need to write the wrappers like control-groups, etc. 您可以使用bootstrap suport安装simple_form,因此您不需要像控件组那样编写包装器等。

Check this link on installation: https://github.com/plataformatec/simple_form#twitter-bootstrap 在安装时检查此链接: https//github.com/plataformatec/simple_form#twitter-bootstrap

What you need to do is just run the command: 你需要做的就是运行命令:

rails generate simple_form:install --bootstrap

In this page you can see some examples, including horizontal checkboxes with bootstrap and simple_form: http://simple-form-bootstrap.plataformatec.com.br/articles/new 在此页面中,您可以看到一些示例,包括带有bootstrap和simple_form的水平复选框: http ://simple-form-b​​ootstrap.plataformatec.com.br/articles/new

If you really want to use it the way you are doing, you need to add the class "inline" along with the "checkbox" on the label. 如果您真的想按照自己的方式使用它,则需要在标签上添加“内联”类和“复选框”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM