Hi there, Is there a neat and easy way to implement pagination with letters rather than numbers eg : A B C D ... Z Rob
Robert Zolkos wrote:> Hi there, > > Is there a neat and easy way to implement pagination with letters > rather than numbers eg : > > A B C D ... ZI''ve implemented somehting akin to this in my latest Rails app. I have a partial, called _filter.rhtml, that looks like this: <div id="filterButtons"> <% LETTERS.each do |l| %> <%= if params[:filter] == l link_to(l, { :filter => l }, { :id => ''selectedFilterButton'' }) else link_to(l, :filter => l) end -%> <% end %> </div> This creates links for all 26 letters across the top of my list page. Then, in the controller, I use the :filter param value like so: def list params[:filter] = ''All'' unless params.has_key?(:filter) if params[:filter] == ''All'' @server_pages, @servers = paginate(:servers, :per_page => 10, :order_by => ''name'') else @server_pages, @servers = paginate(:servers, :per_page => 10, :conditions => lambda { |r| r.name[0..0].upcase == params[:filter] }, :order_by => ''name'') end end So, if a user clicks on "B", it will show all servers whose name begins with B. Note that in the :condtions hash value above, I am using a lambda. This is because I am using Ackbar/KirbyBase as the dbms. If you are using a regular dbms like MySQL, you would want to use a SQL statement as your :conditions value. HTH, Jamey Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
I am a bit concerned that the direct use of the params hash in your conditions would leave you open to SQL injection attacks. Since you are using a different database, it may be a non-issue, but in general it is dangerous to put stuff from your params hash directly into your find statements without some sort of sanitizing.> > def list > params[:filter] = ''All'' unless params.has_key?(:filter) > > if params[:filter] == ''All'' > @server_pages, @servers = paginate(:servers, :per_page => 10, > :order_by => ''name'') > else > @server_pages, @servers = paginate(:servers, :per_page => 10, > :conditions => lambda { |r| r.name[0..0].upcase =>params[:filter] }, > :order_by => ''name'') > end > end >_Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Well, this is an intranet app for my company, so I wasn''t too concerned about injection attacks when I wrote it, but you do have a good point. Also, since the lambda is actually a Ruby code block, it wouldn''t be a SQL injection attack that would have to be used, but, again, in general, your point is well taken. Jamey Kevin Olbrich wrote:>I am a bit concerned that the direct use of the params hash in your >conditions would leave you open to SQL injection attacks. Since you are >using a different database, it may be a non-issue, but in general it is >dangerous to put stuff from your params hash directly into your find >statements without some sort of sanitizing. > > > >> def list >> params[:filter] = ''All'' unless params.has_key?(:filter) >> >> if params[:filter] == ''All'' >> @server_pages, @servers = paginate(:servers, :per_page => 10, >> :order_by => ''name'') >> else >> @server_pages, @servers = paginate(:servers, :per_page => 10, >> :conditions => lambda { |r| r.name[0..0].upcase =>>params[:filter] }, >> :order_by => ''name'') >> end >> end >> >> >> > >_Kevin > > >Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
How about # your table and field names will vary... @alpha = find_by_sql(''select distinct substr(short_desc,1,1) from listings order by short_desc'') the resultant AR recordset provides only the letters used (why use the whole alphabet if only certain letters are used. Then, your pagination links look like: <!-- crude attempt at link generation :) --> <% @alpha.each do |a| %> <%= link_to(a, :action => show_page, :start_with => a) %> <% end %> -- View this message in context: http://www.nabble.com/Pagination-with-letter-%28A-B-C-D-...-Z%29-t1424204.html#a3852071 Sent from the RubyOnRails Users forum at Nabble.com.
I''d just like to point out that "A".upto("Z") do ... is a lot better than LETTERS = %w(A B C D .... LETTERS.each do -- Kyle Maxwell Chief Technologist E Factor Media // FN Interactive kyle@efactormedia.com 1-866-263-3261
Kyle Maxwell wrote:>I''d just like to point out that > >"A".upto("Z") do ... > >is a lot better than > >LETTERS = %w(A B C D .... >LETTERS.each do > >Except for the fact that my LETTERS constant looks like this: LETTERS = [''All'', ''0'', ''1'', ''2'', ... ''X'', ''Y'', ''Z''] Because our server names can begin with a number and I also want to give the user the ability to select all servers. So, I probably should have picked a different name for the constant, but I can''t just use #upto like you suggest. Jamey
<...>> Except for the fact that my LETTERS constant looks like this: > > LETTERS = [''All'', ''0'', ''1'', ''2'', ... ''X'', ''Y'', ''Z'']> So, I probably should have picked a different name for the constant, but > I can''t just use #upto like you suggest.Some typing... LETTERS = [''All'',(0..1).to_a,(''A''..''Z'').to_a].flatten Still not sure if this is most optimal way. Regards, Rimantas -- http://rimantas.com/
On 11/04/2006, at 11:10 PM, Jamey Cribbs wrote:>> I''d just like to point out that >> >> "A".upto("Z") do ... >> >> is a lot better than >> >> LETTERS = %w(A B C D .... >> LETTERS.each do >> > Except for the fact that my LETTERS constant looks like this: > > LETTERS = [''All'', ''0'', ''1'', ''2'', ... ''X'', ''Y'', ''Z''] > > Because our server names can begin with a number and I also want to > give the user the ability to select all servers. > > So, I probably should have picked a different name for the > constant, but I can''t just use #upto like you suggest.LETTERS = [''All'']+("1".."9").to_a+("A".."Z").to_a Avoids all that pesky typing ;) -- Phillip Hutchings phillip.hutchings@sitharus.com http://www.sitharus.com/