In a previous thread some people were interested in ways to sort a table. I mentioned a simple method for doing so by using links in the titles. Having actually tried to implement this strategy, I have come across several refinements that I would like to share. First... your table view should look like this... ... <table> <tr> <th><%= link_to ''Column1'', :sort=>''column1'' %> <th><%= link_to ''Column2'', :sort=>''column2'' %> </tr> ..... </table> ... your ''model'' controller ''list'' action should look like this.... def list sorthash = {"column1"=>"column1, updated_at", "column2"=>"column2 DESC"} sorthash.default = "column1, updated_at" sort = params[:sort] || session[:model][:sort] session[:model][:sort]=sort @model_pages, @models = paginate :model, :per_page => 10, :order=>sorthash[sort] end === What this does == The view passes a hash key as the :sort parameter. This gets looked up in the sorthash, which returns the actual columns needed to sort. That gets passed to the paginator. Key features... * the sort order gets saved in the session, so the view will remember the sort order until the user logs off. Without this, even switching to another page will cause the view to forget the sort order. * by putting the valid sort orders into a hash and defining a default sort order, we avoid the risk that a malicious user can inject SQL into the paginator. It also keeps the view code prettier. * all the sorting is done by the database, so this puts more strain on the server. It also causes a page refresh, which can be annoying. It does not require javascript to work. A similar technique can be used to ''filter'' the view. Any comments, feedback, tweaks, etc... would be appreciated. -- Posted via http://www.ruby-forum.com/.
Christian Klauser
2006-Apr-16 23:28 UTC
[Rails] Re: Semi-Dynamic table sorting (without AJAX)
> Any comments, feedback, tweaks, etc... would be appreciated.I like that implementation... thanks for the snippet. :-) -- Posted via http://www.ruby-forum.com/.
On 4/16/06, Christian Klauser <ch27k89@gmail.com> wrote:> > Any comments, feedback, tweaks, etc... would be appreciated. > > I like that implementation... thanks for the snippet. :-) > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >I tried it a couple months ago and had issues with pagination. When I went to the second page the sort order was lost. I did not try to resolve it. I was a definate newbie at that point. Greg -- Greg Freemyer The Norcross Group Forensics for the 21st Century
The trick for this method is to store the persistent information in the session store so that you can reload it on the next page. I have since moved to a similar (and cleaner) method that doesn''t use the session store as much. I''ll try to write up an article about this soon after things settle down at work. On Monday, April 17, 2006, at 8:25 AM, Greg Freemyer wrote:>On 4/16/06, Christian Klauser <ch27k89@gmail.com> wrote: >> > Any comments, feedback, tweaks, etc... would be appreciated. >> >> I like that implementation... thanks for the snippet. :-) >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > >I tried it a couple months ago and had issues with pagination. > >When I went to the second page the sort order was lost. > >I did not try to resolve it. I was a definate newbie at that point. > >Greg >-- >Greg Freemyer >The Norcross Group >Forensics for the 21st Century >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Seemingly Similar Threads
- Split data.frames depeding values of a column
- R Shiny Help - Trouble passing user input columns to emmeans after ANOVA analysis
- Lighttpd now works for production. Development is broken?
- newbie radrails question
- Re: Manually running dispatch.fcgi fails (Please help!!)