简体   繁体   English

select2和bootstrap文本字段不在rails形式中对齐

[英]select2 and bootstrap text field not align in rails form

As you can see in the image below, the text field (using bootstrap) is not align horizontally with select2 dropdown listbox. 如下图所示,文本字段(使用引导程序)未与select2下拉列表框水平对齐。 How can I make it align ? 我如何使其对齐?

not align input

The following is the _form.html.erb code snippet. 以下是_form.html.erb代码段。 The complete code can be found here https://gist.github.com/axilaris/9597320 完整的代码可以在这里找到https://gist.github.com/axilaris/9597320

there 2 are the inputs that are not aligned <%= f.text_field :name %> and <%= select_tag(:option,.. 有2个输入未对齐<%= f.text_field:name%>和<%= select_tag(:option,..

<script>

    $(document).ready(function() {
     $('select#option').select2();
    });

</script>
 <br><br>
 <div class="field">
 <%= f.label :name %><br>
 <%= f.text_field :name %>
 <%= select_tag(:option, 
    options_for_select([['All', 1], ['Co', 2], ['Bought', 3]] )) %>
 </div>

I also use select2 and Bootstrap in few projects. 我还在几个项目中使用select2和Bootstrap。 Select2 got its own style and applies after bootstrap. Select2具有自己的样式,并在引导后应用。 I think you'll have to play with some css margin, padding etc. You can give specific class to your select_tag for a better targeting: 我认为您必须使用一些CSS边距,填充等。您可以为select_tag提供特定的类,以获得更好的定位:

<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3]] ), {class: "my-specific-class-for-select2"}) %>

documentation 文件

EDIT: with data-live-search 编辑:数据实时搜索

<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3]] ), {class: "my-specific-class-for-select2", data:{live_search: "true"}) %>

The tag will get the data-live-search attribute. 标签将获得data-live-search属性。 Note that the underscore (_) will become an -. 请注意,下划线(_)将变为-。

Documentation about the tags and data attributes. 有关标签和数据属性的文档。

EDIT2: EDIT2:

With some adjustments in the html and css I got this working: 通过对html和CSS进行一些调整,我可以正常工作:

application.scss

/*Solution for bootsrap styling compliance*/
a.select2-choice{
  height: 35px !important;
  span{
   padding-top: 4px;
  }
}



In the view

<div class="form-group clearfix">
  <%= f.label :title %><br>
  <div class="col-sm-4"><%= f.text_field :title,  {class: "form-control"} %></div>
  <div class="col-sm-3"><%= select_tag(:type,
  options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5]] ), {style: "width:100%;"}) %></div>
</div>


<div class="actions clearfix">
  <%= f.submit :class => "btn btn-primary" %>
</div>

Hope it helps. 希望能帮助到你。

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

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