2013-02-18 6 views
0

양식에 여러 기본값에서 지능적으로 채우기 위해 여러 소스에서 데이터를 가져 오는 양식이 있습니다. 또한 "visit_type"드롭 다운이 선택되면 편집 할 수있는 "visit_price"텍스트 상자를 미리 채우고 싶습니다. 이 드롭 다운 지금까지,드롭 다운 선택시 채우기 양식 텍스트 상자

은 아래 양식이다 (thisthis railscast 비디오에 따라) 이름 검색 필드에서 JQuery와 통해 모델 협회와 커피 스크립트를 ajdusted하는 grouped_collection_select이다. 관련 html 출력도 포함했습니다.

편집 : 분명히 알 수 있듯이 드롭 다운에서 선택한 고객 가격과 관련된 가격을 얻는 방법을 모르겠습니다.

방문 블록 형태로보기

<%= form_for(@visit_block) do |f| %> 

    <div class="controls controls-row"> 
    <div class="form-inputs"> 
     <%= f.text_field :client_name, data: {autocomplete_source: current_user.clients.order(:last_name).map(&:last_name)} %> 
    </div> 
    <div class="form-inputs"> 
     <%= f.grouped_collection_select :visit_type, current_user.clients.order(:last_name), :client_prices, :last_name, :visit_type, :visit_type, include_blank: true %> 
     <%= f.text_field :visit_price %> 
    </div> 
    </div> 
    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

모델

(지금까지)
class VisitBlock < ActiveRecord::Base 
    attr_accessible :visit_price, :visit_type, :client_id 

    belongs_to :client 

    def client_name 
    client.try(:last_name) 
    end 

    def client_name=(last_name) 
    self.client = Client.find_by_last_name(last_name) if last_name.present? 
    end 

end 


class Client < ActiveRecord::Base 
    attr_accessible :active, :address_1, :address_2, :city, :email, :first_name, :last_name, :state, :user_id, :zip, :client_prices_attributes 
    has_many :client_prices, :dependent => :destroy 

end 

class ClientPrice < ActiveRecord::Base 
    attr_accessible :client_id, :price, :visit_type, :id, :default_price_id, :custom, :_destroy 

    belongs_to :client 
    belongs_to :default_price 


end 

커피 스크립트 (지금까지)

jQuery -> 
    $('#visit_block_client_name').autocomplete 
    source: $('#visit_block_client_name').data('autocomplete-source') 

    visit_types = $("#visit_block_visit_type").html() 
    console.log visit_types 
    $("#visit_block_client_name").keyup -> 
    client_name = visit_block_client_name.value 
    options = $(visit_types).filter("optgroup[label=" + client_name + "]").html() 
    console.log options 
    if options 
     $("#visit_block_visit_type").html options 
    else 
     $("#visit_block_visit_type").empty() 

    $("#ui-id-1").click -> 
    client_name = visit_block_client_name.value 
    options = $(visit_types).filter("optgroup[label=" + client_name + "]").html() 
    console.log options 
    if options 
     $("#visit_block_visit_type").html options 
    else 
     $("#visit_block_visit_type").empty() 

HTML 출력

   <h1>New visit_block</h1> 

<form accept-charset="UTF-8" action="/visit_blocks" class="new_visit_block" id="new_visit_block" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="I4xbbvdswv18TtP4LWoZHo7udmABT0J/nvKEPLFfO5Q=" /></div> 

    <div class="controls controls-row"> 
    <div class="form-inputs"> 
     <input data-autocomplete-source="[&quot;Client1&quot;,&quot;Client2&quot;,&quot;Client3&quot;]" id="visit_block_client_name" name="visit_block[client_name]" size="30" type="text" /> 
    </div> 
    <div class="form-inputs"> 
     <select id="visit_block_visit_type" name="visit_block[visit_type]"><option value=""></option> 
<optgroup label="Client1"> 
<option value="morning">morning</option> 
<option value="Morning weekday visit">Morning weekday visit</option> 
</optgroup> 
<optgroup label="Client2"> 
<option value="evening">evening</option> 
<option value="Morning weekday visit">Morning weekday visit</option> 
</optgroup> 
<optgroup label="Client3"> 
<option value="afternoon">afternoon</option> 
<option value="Morning weekday visit">Morning weekday visit</option> 
</optgroup> 
</select> 
     <input id="visit_block_visit_price" name="visit_block[visit_price]" size="30" type="text" /> 
    </div> 
    </div> 
    <div class="form-actions"> 
    <button name="button" type="submit">submit</button> 
    </div> 
</form> 

답변

0

당신이 당신의 드롭 다운에 후크를 넣어 :

<select id="visit_block_visit_type" onchange="fill_in_price(this)"...> 

당신이 가격에 방문 유형을 매핑하는 가격 기능이 가정, 당신은 채우기 : 빅토르의 대답이 옳다 외에

fill_in_price = -> 
    $("#visit_block_visit_price").val(price($(this).val())) 
+0

감사합니다. 가격과 방문 유형은 동일한 데이터베이스 행에 있어야합니다. 나는 정확하게 그걸 어떻게 매핑 할 지 모르겠다. – Shaun

0

, 나는 그것을 믿는다 자바 스크립트 코드가없는 HTML 마크 업을 남겨 두는 것이 훨씬 깔끔하다. 나는이 접근법을 따를 것이다 :

$('#visit_block_visit_type').change(function() { 
    $("#visit_block_visit_price").val(price($(this).val())); 
}); 

그것은 꽤 똑같은 것이다.

+0

감사합니다. 방문 가격에 방문 유형을 매핑하는 방법을 이해하는 데 문제가 있습니다. – Shaun

+0

이렇게하는 방법은 n 가지가 있습니다. ajax를 호출하고 유형 변경시 데이터베이스에서 값을 가져옵니다. 유형 ID와 가격을 사용하여 DOM의 일부 요소를 미리 채우십시오. 유형을 변경하면 DOM 내부의 가격 등을 검색합니다. – MurifoX

관련 문제