简体   繁体   中英

Update multiple table rows with Ajax

I have a table in a view with non-unique ids. I have an Ajax enabled submit button in one of the columns and I would like the Ajax results to update all the rows in the table that match the id. Is there anyway to do that? I have only been able to get it to update the first occurrence of the id.
Thanks for any help!

Here is the relevant code;

table code in my view;

<% @gifts.each do |gift| %>

<tr id="<%= gift.parent_id %>">
  <% if gift.key_indicator == "Individual" %>
    <td><%= gift.first_name %> <%= gift.last_name %></td>
  <% else %>
    <td><%= gift.organization_name %></td>
  <% end %>
  <td><%= l gift.gift_date.to_date, :format => :default %></td>
  <td><%= number_to_currency(gift.fund_split_amount) %></td>
  <td><%= if gift.fund_id =~ /\A[Xx]/ then :CND else :US end%></td>
  <td><%= gift.preferred_primary_email_number %></td>
  <td><% if @subscriber_ids.include?(gift.constituent_id) %>
        Yes
      <% else %>
        <%= form_for(@teacher.mailing_list_edits.build(:parent_id => gift.parent_id),remote: true) do |f| %>
            <div><%= f.hidden_field :parent_id %></div>
        <%= f.submit "Add", :class => "btn btn-large btn-primary" %>
        <% end %>
    <% end %>
  </td>
 </tr>
 <% end %>

Here is my create.js.erb

$('#<%= params[:mailing_list_edit][:parent_id] %>').html("foobar")

Go ahead and change to

<tr id="<%= gift.parent_id %>" class="gift_<%= gift.parent_id %>">

Then in your create.js.erb:

$('.gift_<%= params[:mailing_list_edit][:parent_id] %>').html("foobar")

That way you can access multiple elements (by using classes), and replace them all easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM