动态的增加auto_complete

http://www.iteye.com/problems/35097
以前问的一个问题,今天遇到一个同样的情况,解决了.把代码贴出来,纪念下.
解决起来其实也是比较简单的,用后台来增加auto_complete的partial,动态生成一个id,每个id不同就可以.
controller
  # add_purchase_tax,增加purchase_tax
  def add_purchase_tax
    @purchase_order_marketing = PurchaseOrderMarketing.find_by_id(params[:purchase_order_marketing_id])
    render :update do |page|
      page.insert_html :before,
        :total,
        :partial => '/purchase_invoices/purchase_tax',
        :locals => {:purchase_tax => PurchaseTax.new(:category => 'NewTax',:add => 'plus'),:time => Time.now.to_f}
    end
  end

help
  def fields_for_purchase_tax(purchase_tax,time, &block)
    if purchase_tax.new_record?
      fields_for("purchase_order_marketing[new_purchase_tax_attributes][#{time}]", purchase_tax, &block)
    else
      fields_for("purchase_order_marketing[existing_purchase_tax_attributes][]", purchase_tax, &block)
    end
  end

js
function add_purchase_tax(purchase_order_marketing_id){
    new Ajax.Request('/purchase_taxes/add_purchase_tax',{
        method: 'get',
        parameters: 'purchase_order_marketing_id='+purchase_order_marketing_id,
        evalScripts:true
    })
}

function remove_tax(){
    if($$('tr.newtax').size() > 0){
        var old_value = Number($$('tr.newtax').last().down('span').innerHTML);
        $$('tr.newtax').last().remove();
        var total = $('total_value');
        total.innerHTML = Math.round((Number(total.innerHTML) - old_value)*100)/100;
        $$('input[id$=total]').first().value = total.innerHTML
    };
}

html
<% form_remote_for :purchase_order_marketing,
  :url => save_purchase_order_marketing_url(@purchase_order_marketing),
  :html => { :method => 'post' },
  :complete => "Effect.Appear($('csuccess'));setTimeout(\"Effect.Fade($('csuccess'));\",2500);",
  :failure => "error" do |f| %>

        <%= render :partial => 'purchase_tax' ,:collection => @purchase_order_marketing.purchase_taxes %>

        <tr style="height:20px;">
          <td>
            <input type="button" value="Add" onclick="add_purchase_tax(<%= @purchase_order_marketing.id %>);"/>
            <input type="button" value="Remove" onclick="remove_tax();"/>
          </td>

_purchase_tax
<% time ||= Time.now.to_f %>
<% fields_for_purchase_tax(purchase_tax,time) do |purchase_tax_form| %>
  <% prefix = purchase_tax.new_record? ? "new_#{time}" : "existing_#{purchase_tax.id}" %>

      <td class="tax">
        Tax & Duties
      </td>
      <td>
        <%= purchase_tax_form.select :add,PurchaseTax.default_categories,{},{:onchange => 'change_tax(this);'} %>
        <%= auto_complete_for_client(prefix, "payee",nil,{:width => '70px',:source => PurchaseOrder.default_tax_duties,:fields => [:name],:onblur => 'change_purchase_tax(this);'}) %>
        <%= purchase_tax_form.text_field :rate,{:size => 3,:onchange => 'change_tax2(this);',:readonly => 'readonly'} %> %
        <%= purchase_tax_form.hidden_field :tax_duty_id %>
        <%= purchase_tax_form.hidden_field :tax_duty_name %>
      </td>
      <td>
        <span id="<%= prefix %>_span"><%= (@purchase_order_marketing.sub_total)*purchase_tax.rate/100 %></span> <%= @purchase_order_marketing.uom %>
      </td>

你可能感兴趣的:(html,Ajax,F#,Ruby)