2017-06-11 14 views
0

CoffeeScriptを理解し、RailsでCocoonのBefore-Insertアクションを呼び出すために苦労しています...選択した会社に関連するプロジェクトをフィルタリングする必要があります。サプライヤーフィルターは、Cocoonとは関係ないので問題なく機能しますが、Projectフィルターは機能しません。助けてください?挿入(およびフィルタ)するRails Cocoon CoffeeScript Before-Insert

私のCoffeeScript

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) 

マイフォーム

<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" %> 

マイフィールド

<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> 

答えて

0

問題は、正しい要素を見つけた、私のCoffeeScriptはに更新されました:

$('#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() 
関連する問題