I modified Julien Barnier''s code in "How to paginate, sort, and search a table using Ajax and Rails" (http://dev.nozav.org/rails_ajax_table.html) a bit for my application. However, each row of record in my app consists of fields from 2 tables, events and locations. The list action is in my event_controller so the sort works for the fields belonging to the events table but not those belonging to the locations table, which has a has_many relationship with the events table. I tried a couple of things (as a newbie) to make the sorting works for all columns but to no avail. Is it possible to do this? Perhaps I need to sort the locations table in the location_controller and then somehow combine the two? Please help! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten
2007-Apr-19 15:34 UTC
Re: How to sort columns from more than one table using Ajax?
some code would help. can you show e.g. the controller? generally i would say you have to include the table name in the property to sort by, e.g. @events = Event.find(:all, :include => :location, :order => "location.name") so "location.name" (or "location.name_reverse") would be the param in the sort link to sort by location name. i''m just guessing here what the problem might be, as i said show some code that might clear things up. On 19 Apr., 10:41, Kevin Lee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I modified Julien Barnier''s code in "How to paginate, sort, and search a > table using Ajax and Rails" (http://dev.nozav.org/rails_ajax_table.html) > a bit for my application. However, each row of record in my app > consists of fields from 2 tables, events and locations. The list action > is in my event_controller so the sort works for the fields belonging to > the events table but not those belonging to the locations table, which > has a has_many relationship with the events table. I tried a couple of > things (as a newbie) to make the sorting works for all columns but to no > avail. Is it possible to do this? Perhaps I need to sort the locations > table in the location_controller and then somehow combine the two? > Please help! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wai Tsang
2007-Apr-19 16:36 UTC
Re: How to sort columns from more than one table using Ajax?
Kevin Lee wrote:> I modified Julien Barnier''s code in "How to paginate, sort, and search a > table using Ajax and Rails" (http://dev.nozav.org/rails_ajax_table.html) > a bit for my application. However, each row of record in my app > consists of fields from 2 tables, events and locations. The list action > is in my event_controller so the sort works for the fields belonging to > the events table but not those belonging to the locations table, which > has a has_many relationship with the events table. I tried a couple of > things (as a newbie) to make the sorting works for all columns but to no > avail. Is it possible to do this? Perhaps I need to sort the locations > table in the location_controller and then somehow combine the two? > Please help!You may add another parameter (e.g. table) in order to distinguish which table the user is updating like this: def sort_link_helper(text, param, table, spinnerid) key = param key += " DESC" if params[:sort] == param options = { :url => {:action => ''index'', :params => params.merge({:sort => key, :table => table})}, :update => table, :before => "Element.show(''#{spinnerid}'')", :success => "Element.hide(''#{spinnerid}'')" } html_options = { :title => "Sort by this field", :href => url_for(:action => ''index'', :params => params.merge({:sort => key, :table => table})) } link_to_remote(text, options, html_options) end and use params[''table''] in the controller for different pagination when request is xhr. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kevin Lee
2007-Apr-23 05:28 UTC
Re: How to sort columns from more than one table using Ajax?
I modified sort_link_helper method as you suggested but isn''t clear on how to use params[''table''] in the controller. I tried a lot of things (combinations) without success. As before, the sort on columns that belong to events table works but the sort on the column that belong to the locations table does not. Here is the relevant code: # this is the Event model # AND I think you can tell what I did wrong in the snippet of codes below by # looking at the model class Event < ActiveRecord::Base belongs_to :location acts_as_ferret :store_class_name => true, :additional_fields => [:location_city] def location_city return location.city end end # here is the snippet of my partial: <table> <thead> <tr> <td <%= sort_td_class_helper "city", "locations" %>> <%= sort_link_helper "City", "city", "locations" %> </td> </tr> </thead> <tbody> <% @events.each do |e| %> <tr class="<%= cycle("even","odd") %>"> <td><%= e.location_city %></td> </tr> <% end %> </tbody> </table> # and here is the SNIPPET of my list action in the event_controller.rb: def list sort = case @params[''sort''] when "location_city" then "location_city" when "location_city_reverse" then "location_city DESC" end @events = Event.find(:all, :include => :location) ) @events_pages, @events = paginate :events, :order => sort, :conditions => conditions, :per_page => events_per_page if request.xml_http_request? render :partial => "events_list", :layout => false, :additional_fields => [:location_city] end I think my problem is I don''t know how to implement the :additional_fields of the event model in the list action and the partial. Please help again! Wai Tsang wrote:> Kevin Lee wrote: >> I modified Julien Barnier''s code in "How to paginate, sort, and search a >> table using Ajax and Rails" (http://dev.nozav.org/rails_ajax_table.html) >> a bit for my application. However, each row of record in my app >> consists of fields from 2 tables, events and locations. The list action >> is in my event_controller so the sort works for the fields belonging to >> the events table but not those belonging to the locations table, which >> has a has_many relationship with the events table. I tried a couple of >> things (as a newbie) to make the sorting works for all columns but to no >> avail. Is it possible to do this? Perhaps I need to sort the locations >> table in the location_controller and then somehow combine the two? >> Please help! > > You may add another parameter (e.g. table) in order to distinguish which > table the user is updating like this: > > def sort_link_helper(text, param, table, spinnerid) > key = param > key += " DESC" if params[:sort] == param > > options = { > :url => {:action => ''index'', :params => params.merge({:sort => > key, :table => table})}, > :update => table, > :before => "Element.show(''#{spinnerid}'')", > :success => "Element.hide(''#{spinnerid}'')" > } > html_options = { > :title => "Sort by this field", > :href => url_for(:action => ''index'', :params => > params.merge({:sort => key, :table => table})) > } > link_to_remote(text, options, html_options) > end > > and use params[''table''] in the controller for different pagination when > request is xhr.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---