2017-06-11 3 views
0

Schwierigkeiten, CoffeeScript zu verstehen und die Aktion "Vor dem Einfügen" mit Cocoon in Rails aufzurufen ... Ich muss die Projekte in Bezug auf das ausgewählte Unternehmen herausfiltern. Der Lieferantenfilter funktioniert einwandfrei, da er nicht mit Cocoon zu tun hat, der Projektfilter jedoch nicht funktioniert. Irgendwelche Hilfe bitte?Rails Cocoon CoffeeScript Vor dem Einfügen

Mein Coffee

ready = -> 

    $('#quote_supplier_id').parent().hide() 
    suppliers = $('#quote_supplier_id').html() 
    $('#quote_company_id').change -> 
    company = $('#quote_company_id :selected').text() 
    escaped_company = company.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1') 
    options = $(suppliers).filter("optgroup[label='#{escaped_company}']").html() 
    if options 
     $('#quote_supplier_id').html(options) 
     $('#quote_supplier_id').parent().show() 
    else 
     $('#quote_supplier_id').empty() 
    $('#quote_supplier_id').parent().hide() 

    $('#quote_transactions') 
    .on('cocoon:before-insert' 
     projects = $('.projectselect').html() 
     company = $('#quote_company_id :selected').text() 
     escaped_company = company.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1') 
     optionsprojects = $(projects).filter("optgroup[label='#{escaped_company}']").html() 
     if optionsprojects 
     $('.projectselect').html(optionsprojects) 
     else 
     $('.projectselect').empty() 
    ) 


$(document).ready(ready) 
$(document).on('page:load', ready) 

Meine Form

<tbody id="quote_transactions"> 
    <%= f.fields_for :quote_transactions do |quote_transaction| %> 
     <%= render 'transaction_fields', :f => quote_transaction %> 
    <% end %> 
    </tbody> 
</table> 

    <%= link_to_add_association '<i class="glyphicon glyphicon-plus"> Add Transaction</i>'.html_safe, f, :quote_transactions, :"data-association-insertion-node" => "tbody#quote_transactions", :"data-association-insertion-method" => "append", :partial => "transaction_fields", :type=>"button", :class=>"btn btn-default" %> 

Meine Felder einfügen (und Filter)

<tr class='nested-fields'> 
    <td> 
    <div class="field"> 
     <%= f.grouped_collection_select :project_id, Company.order(:name), :projects, :name, :id, :description, {:include_blank => 'Please Select Project'}, {:class => 'form-control projectselect', :onfocus=>"extractId()", :onclick=>"extractId()"} %> 
    </div> 
    </td> 
    <td> 
    <div class="field"> 
     <%= f.text_field :description, :class=>"form-control" %> 
    </div> 
    </td> 
    <td> 
    <div class="field"> 
     <%= f.text_field :quote_amount, :class=>"form-control" %> 
    </div> 
    </td> 
    <td> 
    <%= link_to_remove_association '<i class="glyphicon glyphicon-remove"></i>'.html_safe, f, :class=>"btn btn-danger" %> 
    </td> 
    </tr> 

Antwort

0

Das Problem war das richtige Element Ortung, mein Coffeescript wurde aktualisiert :

$('#quote_transactions').bind 'cocoon:before-insert', (e, inserted_item) -> 
    projectsElem = inserted_item.find('.projectselect') 
    projects = projectsElem.html() 
    company = $('#quote_company_id :selected').text() 
    escaped_company = company.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1') 
    optionsprojects = $(projects).filter("optgroup[label='#{escaped_company}']").html() 
    if optionsprojects 
     projectsElem.html(optionsprojects) 
    else 
     projectsElem.empty() 
Verwandte Themen