【问题标题】:Simple Form- control width of label and input (with bootstrap)简单表单-控制标签和输入的宽度(带引导程序)
【发布时间】:2019-03-10 01:11:53
【问题描述】:

我正在使用 Simple_form 和 Bootstrap。

我想使用水平包装,但我需要使标签更宽,输入更窄。我想用 bootstrap 4 vs custom css 来做到这一点。

标签和输入应该在同一行(内联),并且右对齐。

col-md-8 用于标签,col-md-4 用于输入。我该怎么做?

 <%= f.input :role, :wrapper =>:horizontal_range, input_wrapper_html: { class: "col-md-4"}, label_wrapper_html: {class: "col-md-8"} %>

编辑:我应该补充一点,f.input :role 包含在

<div class='row'>
  <div class='col-md-6"> 
      f.input :role 
  </div>
</div>

第二,

我需要在输入的末尾添加一个 %。但是,它似乎不起作用。

 <%= f.input :pct_of_car , append: "%",  :wrapper =>:horizontal_range %>

但是,这似乎不起作用。我的语法有什么问题?

第三,如何将垂直collection_inline 修改为2 或3 列?

  # vertical input for inline radio buttons and check boxes
  config.wrappers :bs_vertical_collection_inline, item_wrapper_class: 'form-check form-check-inline', tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b|
    b.use :html5
    b.optional :readonly
    b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba|
      ba.use :label_text
    end
    b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid'
    b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' }
    b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
  end

目前所有的复选框都是内联列出的,没有结构。我需要在 2 或 3 列中列出选项,以便于阅读。这也是一个很长的列表,因此可能需要将选项包装在滚动框中。

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap simple-form ruby-on-rails-5.2


    【解决方案1】:

    您需要定义自己的自定义包装器或修改默认包装器。

    首先,确保您的存储库中有默认包装器。

    如果找不到app/initializers/simple_form_bootstrap.rb,请运行rails g simple_form:install --bootstrap

    其次,在此初始化程序中定位 Bootstrap 网格类。对于水平形式,它们位于如下所示的块中:

    config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
        b.use :html5
        b.use :placeholder
        b.optional :maxlength
        b.optional :minlength
        b.optional :pattern
        b.optional :min_max
        b.optional :readonly
        b.use :label, class: 'col-sm-3 control-label'
    
        b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
          ba.use :input, class: 'form-control'
          ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
          ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
        end
      end
    

    第三,换包装!

    在这个 sn-p 中,将 col-sm-9 更改为 col-sm-8。 同时将col-sm-3 更改为col-sm-4

    完成

    保存。重新启动 Rails 服务器。现在所有表单都将有 33% 宽度的标签和 66% 宽度的输入。

    如果您不想全局应用此功能,请将 wrapper: :my_wrapper 选项传递给任何 input 并创建一个新的自定义包装器来执行您希望它执行的操作。

    一定喜欢 SimpleForm!

    【讨论】:

      【解决方案2】:

      要为inputlabel设置类,请使用input_htmllabel_html

      <%= f.input :role, :wrapper => :horizontal_range, input_html: { class: "col-md-4"}, label_html: {class: "col-md-8"} %>
      

      关于第二个问题 - 如果您不使用提示,可能最简单的解决方案是这样做:

      <%= f.input :pct_of_car , hint: "%",  :wrapper =>:horizontal_range %>
      

      然后,您可以通过使用hint_html 选项(类似于input_htmllabel_html)向提示添加自定义类来设置它的样式。

      更新

      添加自定义文本的更好解决方案是使用块作为输入:

      <%= f.input(:role, :wrapper => :horizontal_range, label_html: {class: "col-md-8"}) do %>
        <div class='col-md-4'>
          <%= f.input(:role, components: [:input], :wrapper => false, label: false) %>
          <span>%</span>
        </div>
      <% end %>
      

      多亏了这一点,您的标签具有 col-md-8 类,并且带有您的文本的输入被包装在具有 col-md-4 类的 div 中。

      【讨论】:

      • 我需要标签和输入都在同一行(所有内联)。出于某种原因,您的方法将输入放到下一行,有什么想法吗?我
      • 我猜这是因为:horizontal_range 结构。你能把它添加到问题中吗?
      • @user2012677 by :horizontal_range 我的意思是包装器的代码,它可能在config/initializers/simple_form.rb 中定义:)
      猜你喜欢
      • 2013-01-20
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2020-09-10
      • 1970-01-01
      • 2014-04-11
      • 2016-09-25
      相关资源
      最近更新 更多