I'm trying to implement a table with the option to expand and collapse each row using ajax.
I'm using link_to :remote => true
and works fine, the problem is when I click multiple times, all the calls stay alive. I would like to know how to track those calls and discard them if the user collapsed or if he requested a new one. Since I'm not making the $.ajax call my self I don't know how to do it.
Here is my code.
index.html.haml
:
%tr{ :class => 'cmp_row'}
%td
= link_to "+", user_company_details_path(company_id), :remote => true , :method => :post, :cache => false, :class => "cmp_exp_link", :id => "#{company_id}
application.js.erb
:
$('.cmp_exp_link').click(function (e) {
var cmp_id = e.target.id
if($(this).text() === "+"){
$("<tr id='details_row_"+cmp_id+"'><td colspan = 11 id='details_cell_"+cmp_id+"' style='text-align:center' > </td> </tr>").insertAfter($(this).closest(".cmp_row"));
$("#details_cell_"+cmp_id).html("<img src='/assets/colorbox/loading.gif' >")
$(this).text("-");
}else{
$(this).text("+");
$("#details_row_"+cmp_id).remove();
return false;
}
});
company_details.js.erb
// remove the loading icon
$("#details_cell_<%= params[:cmp_id] %>").html("");
// load partial
$("#details_cell_<%= params[:cmp_id] %>").html("<%= escape_javascript(render(:partial => "company_details", :locals => { :results => @results })) %>");
user_controller.rb
def company_details
@results = @data
respond_with(@results)
end
Thanks a lot in advance!!