Hello, I''m looking for a simple way to sort the data in my results by the different columns when users click on them, and to toggle ASC/ DESC on columns as well. Is there a simple way to do this without using posts or links and having to look up the data again? Are there any useful helpers either way? Thanks, Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
It''s not rails related, but the sorttable javascript library does what you need: http://www.kryogenix.org/code/browser/sorttable/ You give your table a special class, include the js file, and it makes it sortable. Very cool. On Thu, 24 Mar 2005 09:41:52 -0500, Williams, Chris <Chris.Williams-yLTBD9Em4aGakBO8gow8eQ@public.gmane.org> wrote:> > > Hello, > > I''m looking for a simple way to sort the data in my results by the > different columns when users click on them, and to toggle ASC/ DESC on > columns as well. Is there a simple way to do this without using posts or > links and having to look up the data again? Are there any useful helpers > either way? > > Thanks, > Chris > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- rick http://techno-weenie.net
You can combine "click to sort headers" with the pagination helper like this: Modify view template to show headers with links back to the list action and pass the clicked column: <% for column in Person.content_columns %> <th><%= link_to column.human_name, :action => ''list'', :column => column.name %></th> <% end %> and add the list of pages somewhere in the template: <div align="center"><%= pagination_links(@person_pages) %></div> Then modify the list action so instead of: @people = Person.find_all the content is: @session[''person_sort''] = @params[''column''] || @session[''person_sort''] || ''default_sort_column'' #ie: sort by date_added? @person_pages, @people = paginate :people, :per_page => 10 , :order_by => @session[''person_sort''] Hope that helps! On Saturday 26 March 2005 12:08, Michael Champanis wrote:> There is a helper for Rails called SortHelper ( :P ) on the internet, > I don''t have the url at the moment, but try look on google. > > > Hello, > > > > I’m looking for a simple way to sort the data in my results by > > the different columns when users click on them, and to toggle ASC/ > > DESC on columns as well. Is there a simple way to do this without > > using posts or links and having to look up the data again? Are there > > any useful helpers either way? > > > > Thanks, > > Chris > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails
Michael Champanis
2005-Mar-26 12:08 UTC
Re: How to sort html table rows by clicking headers?
There is a helper for Rails called SortHelper ( :P ) on the internet, I don''t have the url at the moment, but try look on google.> Hello, > > I’m looking for a simple way to sort the data in my results by > the different columns when users click on them, and to toggle ASC/ > DESC on columns as well. Is there a simple way to do this without > using posts or links and having to look up the data again? Are there > any useful helpers either way? > > Thanks, > Chris > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I''ve been working on something that live updates, sorts and filters a table here: Its not powered by rails yet, I just wanted to do the front end to figure out how it will all work (will likely need revising to match up with the rails-ajax stuff) http://kevin.is-a-geek.net/projects/livetable Kevin
Paul Rogers
2005-Mar-27 03:20 UTC
RE: Re: How to sort html table rows by clicking headers?
Why not do it client side? http://www.brainjar.com/dhtml/tablesort/demo.html -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Kevin Davis Sent: 26 March 2005 19:05 To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] Re: How to sort html table rows by clicking headers? I''ve been working on something that live updates, sorts and filters a table here: Its not powered by rails yet, I just wanted to do the front end to figure out how it will all work (will likely need revising to match up with the rails-ajax stuff) http://kevin.is-a-geek.net/projects/livetable Kevin _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Here''s a sort helper I wrote that you might find useful. It persists sorts within a session and displays a sort icon in the sorted column. NOTE: Requires Rails 0.11.0 Code: http://www.methods.co.nz/misc/sort_helper.rb Screenshot: http://www.methods.co.nz/misc/sort_helper_screenshot.png It''s use is straight-forward: Controller: helper :sort include SortHelper def list sort_init ''last_name'' sort_update @items = Contact.find_all nil, sort_clause end View (table header in list.rhtml): <thead> <tr> <%= sort_header_tag(''id'', :title => ''Sort by contact ID'') %> <%= sort_header_tag(''last_name'', :caption => ''Name'') %> <%= sort_header_tag(''phone'') %> <%= sort_header_tag(''address'', :width => 200) %> </tr> </thead> Cheers, Stuart Williams, Chris wrote:> Hello, > > I’m looking for a simple way to sort the data in my results by > the different columns when users click on them, and to toggle ASC/ DESC > on columns as well. Is there a simple way to do this without using posts > or links and having to look up the data again? Are there any useful > helpers either way? > > Thanks, > Chris > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Kevin Davis
2005-Mar-27 06:00 UTC
Re: Re: How to sort html table rows by clicking headers?
I was thinking it could be slow clientside as I''d be looking at thousands of rows. Also.. the idea that databases are good at sorting, returning sets of records etc. and as such the DB should do that. Kind of a ''let things do what they''re meant for'' philosophy of sorts I guess ;0) Kevin On Sat, 26 Mar 2005 20:20:40 -0700, Paul Rogers <paul.rogers-fVOoFLC7IWo@public.gmane.org> wrote:> Why not do it client side? > http://www.brainjar.com/dhtml/tablesort/demo.html > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Kevin Davis > Sent: 26 March 2005 19:05 > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] Re: How to sort html table rows by clicking headers? > > I''ve been working on something that live updates, sorts and filters a > table here: > > Its not powered by rails yet, I just wanted to do the front end to > figure out how it will all work (will likely need revising to match up > with the rails-ajax stuff) > > http://kevin.is-a-geek.net/projects/livetable > > Kevin > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >