Topic: AJAX and check_box_tag problem

Hi all,

I have a table with each row associated with a check_box just like when you check Google or Yahoo mail. I want when a check_box is selected its associated row will change background accordingly, such as change to yellow background just like Google does. I have tried observe_field for each check_box but it's seem not a good solution. Anyone have a better solution?

I also have check all, un-check all and some buttons at the bottom of table that could apply action to selected rows(selected by check_box) such as delete, activate, de-activate. But the problem is when user click one of those buttons how can i know which row are currently selected to apply action to.

Has anyone experienced these cases? Please share with me

Many thanks


Re: AJAX and check_box_tag problem

You need to supply a dom id to the row in question

<% for object in @objects %>
<tr id="object_<%= "" %>">
  <td>etc etc </td>
<% end %>

You can then apply AJAX to that id no problem. Of course to me this solution is messy now that I use the simply_helpful plugin, which gives you the awesome dom_id method...very handy

Re: AJAX and check_box_tag problem

Thank pimpmaster,

Yes, i assigned each particular row with a unique DOM id and an observe_field, it does work. However, my concern is the response time and efficiency since apply observe_field for each row is seem causing overhead. For example, it take a time for changing row's background. I wonder there is any other solution instead of observe field.

My second problem is how to get value of check_boxes that are associated with each row (checked/unchecked), when user click delete button for example. Do i need to wrap them in a form? 
Actually, can we get value of a check_box in controller if it is not wrapped in a form?

Thank again pimp big_smile. I deadly wait for solutions. Please help.

Re: AJAX and check_box_tag problem

Yeah, I definitely would not use observe field on this. It would be a lot faster to just use :onchange

<%= check_box :object, :method, {}, :onchange => "$('object_#{}').toggleClassName('unchecked_row', 'checked_row');" %>

I haven't tested this but it seems like the simplest solution. If that doesnt work then check out this thread

In regards to your second question, this is one way you could perform an action on multiple items.

def destroy_me
    objects = params[:destroy_me].map { |o| Object.find_by_id(o) }
    object.each { |o| o.destroy }     
  flash[:notice] = 'The selected objects were successfully deleted.'
  redirect_to objects_path

<%= form_tag(:action => 'destroy_me') %>
  <% for object in @objects %>
    <tr id="object_<%= %>" class="unchecked_row">
      <td>etc etc </td>
      <td><%= check_box_tag 'destroy_me[]',,
      :onchange => "$('object_#{}').toggleClassName('unchecked_row', 'checked_row');" %></td>
  <% end %>
<% end %>

Re: AJAX and check_box_tag problem

Hi pimp, thank so much

Everything worked as expected. However, i still have a problem. I had this associated with each check_box_tag so that each time the check_box is clicked, associated background will change. It worked fine if you manually click on a particular one.

:onchange => "$('#{position}').toggleClassName('selected');")

I also have a "check all" and "uncheck all" buttons that allow all check boxs are selected or unselected. It also worked. However, thsi time despite that all the check boxs change state, the AJAX function for changing background is not triggered (means the background does not change) 

<%= button_to_function "Check All", update_page {|page|'#collection_listing  input').each {|x| x.checked = true}}%>

Do you know what is the problem?