1

https://github.com/nathanvda/cocoon嗨 cocoon:和 datetimepicker有问题:http://xdsoft.net/jqplugins/datetimepicker/。当我通过茧新的嵌套字段添加时,我的日历没有显示。我认为我必须在我的javascript文件中插入事件之后使用茧,但我尝试了各种方法,但这是行不通的。这是我的观点:cost.html.haml

= simple_form_for [:partners, @car], url: wizard_path do |f|
  = f.simple_fields_for :costs do |cost|
    = render 'cost_fields', f: cost
  #links
    = link_to_add_association '+', f, :costs
  = link_to t('cars.back'), previous_wizard_path
  = f.submit t('cars.next'), class: 'btn btn-primary'

和我的 cost_fields 部分:

.nested-fields
  %table.table.table-striped.table-bordered.dupa
    %thead
      %th.field
        = f.association :cost_type
      %th.field  
        = f.input :netto_price
      %th.field
        = f.input :document_number

      %th.field
        = f.association :vat  

      %th.field
        = f.input :type_of_cost
      %th.field
        = f.input :date, as: :string , :input_html => { :class => 'date' } 
  = link_to_remove_association "X", f

有任何想法吗?

4

1 回答 1

3

正如这篇文章所言,这是因为动态添加的元素还没有在 DOM 中。

尝试在嵌套的简单字段之前添加一个类或 id(这已经在 DOM 中),例如:

#container
    = f.simple_fields_for :costs do |cost|
        = render 'cost_fields', f: cost    

然后在你的 javascript 文件中:

$(document).on('ready page:change', function() {
    $('#container').on('cocoon:after-insert', function() {
       $('.datetimepicker').datetimepicker();
    })
})

另外,我使用了这个datepicker gem,它允许你生成一个包装器输入/自定义日期/时间字段

= f.input :date, :as => :date_picker
于 2016-05-02T02:51:17.297 回答