简体   繁体   English

Rails 4-nested_form和selected-select

[英]Rails 4 - nested_form and chosen-select

I am using Ryan Bates nested_form_for: https://github.com/ryanb/nested_form . 我正在使用Ryan Bates nested_form_for: https : //github.com/ryanb/nested_form For my select statement, I am using chosen-select: http://harvesthq.github.io/chosen/ . 对于我的select语句,我使用的是selected-select: http : //harvesthq.github.io/chosen/ When I click "Add Skill" I cannot get the select statement to also be class: 'chosen-select'. 当我单击“添加技能”时,我无法将select语句也归类为“ chosen-select”。 A new select field shows up, but it has no custom css styles. 将显示一个新的选择字段,但没有自定义CSS样式。

To be clearer, the original select statements show up correctly and the ones which I add on do not have custom css styles (chosen-select). 更清楚地说,原始的select语句正确显示,而我添加的语句没有自定义css样式(选择)。

<%= nested_form_for(@user) do |f| %>    
    <%= f.fields_for :skills do |builder| %>
            <%= builder.select :skill, data, {},  {class: 'chosen-select'} %>
            <%= builder.date_select :expdate, include_blank: true %>
            <%= builder.link_to_remove "Remove" %>
    <% end %>

        <%= f.link_to_add "Add Skill", :skills %>
<% end %>

Is there a way to make the select statements I add via the link_to_add function use the class 'chosen-select' ? 有没有办法让我通过link_to_add函数添加的select语句使用'chosen-select'类?

i think your problem is that the 'chosen plugin' make the changes on the element when the page is load, once the page is loaded, the new elements added are not changed, at least that you call the function again. 我认为您的问题是“选择插件”会在页面加载时对元素进行更改,一旦页面加载,添加的新元素就不会更改,至少您再次调用该函数。 I would treat call the function on the click event that add the new element. 我会在添加新元素的click事件上调用函数。 Something like this: 像这样:

$('#add-element').on 'click', ->
  $('.chosen-select').chosen()

i am reading the documentation of nested_form, and in your case, i think is like this: 我正在阅读nested_form的文档,就您而言,我认为是这样的:

$(document).on 'nested:fieldAdded, (event)->
  $('.chosen-select').chosen()

Or, more like says the docs 或者,更像是说文档

$(document).on 'nested:fieldAdded, (event)->
  field = event.field
  detefield = field.find('.chosen-select')
  datefield.chosen()

I think this works! 我认为这可行!

PD: the code js is Coffescript. PD:js代码是Coffescript。

PD2: I think that the cocoon gem is best for the job. PD2:我认为茧宝石最适合这份工作。

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

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