Perhaps I’m misunderstanding the question. This looks like it can be
handled in the database quite nicely.
Did you check out the SQL GROUP BY clause? If your database supports
ROLLUP, it may be what you are looking for. E.g.,
@result_set = MyModel.find_by_sql(‘SELECT c1, c2, c3, SUM(c1), SUM(c2),
SUM(c3) WHERE ? GROUP BY c1, c2, c3 WITH ROLLUP’, my_criterion)
The resultant table will contain sum values for c1, c2, and c3,
respectively, but additional rows are injected on category breaks. At
these category breaks, the fields are set to null. I.e.,
value, value, nil === break for c3
value, nil, nil === break for c2
nil, nil, nil === break for c1
There’s probably some ultra-concise Ruby way to iterate this collection,
setting the appropriate column values to blank except on first
occurrence and to ‘subtotal’ at the break, ‘grand total’ when all are
nil. I haven’t thought about that.
Does this help?
Kevin O. wrote:
Making tables in templates is pretty easy, except for one minor problem.
They tend to be fairly ugly.
If you have a model with three attributes, it’s very easy to create an
html table that looks like this…
Col1 Col2 Col3
A B D
A B E
A C F
A C G
Which becomes difficult to read when you have a lot of repeated data.
What I would really like to generate should look more like this…
Col1 Col2 Col3
A B D
E
C F
G
Using rowspans and align=middle as appropriate. Basically I want to
combine adjacent rows with the same data into a single cell with a
rowspan.
Does anyone know of a simple algorithm to generate such tables (ideally
from a collection of activerecord objects, of course)?
Thanks,
_Kevin