Forum: Ruby on Rails inserting column headings between grouped rows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
19c9af375aca3001e3978cb16684803c?d=identicon&s=25 Parker Thompson (Guest)
on 2006-01-25 07:49
(Received via mailing list)
Hello all,

I have what is conceptually a pretty simple poblem, but can't think of
an obvious solution.  I have a group of objects (@items), that I wish
to render as rows.  Items have dates, and I wish to group them by that
date, inserting a header row each time the date changes e.g.:

 header row
 row 11/11/2004
 row  11/11/2004
 header row
 row 11/23/2004

I'm currently rendering the rows like so:

  render :partial => 'item', :collection => @items

I'm curious if there's a a way to keep track of a date value between
insert a spacer each time it changes.  I couldn't find one looking
over the docs.  Anyone had to deal with this?

As always, any help is greatly apreciated,

pt.
--
Parker Thompson
http://www.parkert.com/
510.541.0125
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-01-25 15:28
(Received via mailing list)
Parker:

I hope someone will offer one of those super-efficient solutions. For
now, I'd do what you want without using the partial.  I'd do:

@items.each do |item|

then I could just check the date field against the previous entry and
use an if block to display the row header when the date changes.

That said, there is probably something much better.  Thanks for the
question, I look forward to the answer.

bruce
Bde8d8f24c1ca83f452b5a9d03d5ead6?d=identicon&s=25 Wouter (Guest)
on 2006-01-25 15:56
Might not be really flashy, but it works:

current = nil
@items.each do |item|
  if(current != item)
    print_your_row_header()
    current = item
  end
  print_the_item()
end
This topic is locked and can not be replied to.