Table sorting - no support in base rails?

Hi,

Is it true to say there’s no support for sorting table via clicking on
column headers within the base rails download?

From what I can tell pagination is included, but for sorting you need to
following the “Sort Helper” topic in the wiki at:

http://wiki.rubyonrails.com/rails/pages/Sort+Helper

Tks

Yeah, there’s nothing built-in. But take a look at the code for the
sorting_helper, it’s only 20-25 lines of code for a general purpose
sorter. It goes to show how easy this sort of thing is to accomplish
in Rails. I would probably never bother with this sorter code since
it’s trivially fast to implement exactly what I want.

Tks Gabriel - you don’t have a site I can have a look at? - I’d be
interested in seeing how people have laid-out their sorting links

got it Michael thanks - cool - what did you use on the site for the
“page
forward” / “jump to last page”? Was it the rails paging mechanism?

Greg H wrote:

Tks Gabriel - you don’t have a site I can have a look at? - I’d be
interested in seeing how people have laid-out their sorting links

I use sort_helper - www.crmonrails.com - go to the leads or orders list
and click on any of the column headers. Don’t have it on the activities
list. Not much data in that demo app, but it will give you an idea of
how I did it.

Michael

Greg H wrote:

got it Michael thanks - cool - what did you use on the site for the
“page
forward” / “jump to last page”? Was it the rails paging mechanism?

Here is the code that I put in the application helper file
(application_helper.rb) for the sort colums and for the pagination that
you ask about. I don’t pretend to be an optimization guru - so I’m sure
there is a better way to achieve this. However, this did the trick for
me and is reused across my views.

Regards,

Michael

#LIST VIEW SORT
def list_view_td_sort_column(column_name, display_value)
list_text =
<<-EOL


EOL
list_text = list_text + sort_header_tag(column_name, :title => 

display_value)

end

#NAVIGATION/PAGINATION HELPER
def list_view_paginate(page_object)
return_html =
<<-EOL






 Showing #{ page_object.current.first_item } - #{
page_object.current.last_item } of #{ page_object.item_count }

EOL
if page_object.current.first? then
 return_html = return_html +
 <<-EOL
						<img src="/images/start_disabled.gif" border="0" 

align=“absmiddle”> 
 
EOL
else
return_html = return_html +
<<-EOL
#{ link_to(image_tag(“start.gif”, :border => 0, :align =>
“absmiddle”), { :page => page_object.first_page }) if
page_object.current.previous }
#{ link_to(image_tag(“previous.gif”, :border => 0, :align =>
“absmiddle”), { :page => page_object.current.previous }) if
page_object.current.previous }
EOL
end
if page_object.current.last? then
return_html = return_html +
<<-EOL
 
 
EOL
else
return_html = return_html +
<<-EOL
#{ link_to(image_tag(“next.gif”, :border => 0, :align =>
“absmiddle”), { :page => page_object.current.next }) if
page_object.current.next }
#{ link_to(image_tag(“end.gif”, :border => 0, :align =>
“absmiddle”), { :page => page_object.last }) if page_object.current.next
}
EOL
end

return_html = return_html +
<<-EOL
				</td>
			</tr>
		</table>
	</td>
</tr>
EOL

end

I’m sorry - forgot to answer your question: Yes, it is using the Rails
pagination object which, if you read other posts, may not be the wisest
thing to do (performance)! :slight_smile: I don’t plan on having millions of rows
so it will be fine for me (I think) and I strongly suspect that by the
time it becomes an issue then the core guys will have figured out how to
keep pagination and make it perf friendly. If they don’t then by the
time I have millions of rows I’ll also have other resources available to
help figure it out! :slight_smile:

I hope this has been helpful to you in some way.

Michael

if you’re ever looking to use ajax to do table sorting there’s a few
resources that i’ve picked up over the last few months.

This first one is just a tutorial that walks through building your own
helpers for it. pretty sure it degrades well if the user doesn’t have
javascript too, so it’ll prlly be easy to figure out either way.

http://dev.nozav.org/rails_ajax_table.html

I haven’t actually used this next one in my projects, but it looks
really nice. I’ve played around with it some and it seems like it
requires a little bit more of a learning curve to actually have it do
just what I want. But maybe it can help you.

http://www.ajaxscaffold.com/

I think I found a bug in Sort Helper at
http://wiki.rubyonrails.com/rails/pages/Sort+Helper

I posted a note on the wiki:

=====================================
I think I found a bug in the code. The line:

{ :params => { ‘sort’ => column, ‘order’ => order }.merge(params) }

This is trying to do a “new.merge(old)” however it seems that the merge
works the other way round in terms of ensuring that the new settings
override the old. I tried the following and it seemed to work:

{ :params => params.merge({ ‘sort’ => column, ‘order’ => order }) }

thanks Michael - I’ll digest it over the next day or two :slight_smile: