How to create a two dimensional table

15 views
Skip to first unread message

Valentino Lun

unread,
Jan 14, 2009, 5:12:35 AM1/14/09
to rubyonra...@googlegroups.com
Dear all

I have the following data in a table and I want to create a
2-dimensional table to display it.

Table: services
col01 col02 col03
AH AS 1
AH CS 2
AH MS 3
BH BS 1
BH CS 1
BH MS 1
CH IS 1
CH BS 2
CH MS 1
DH CS 1
DH MS 1

I want to have this present on web

AS BS CS IS MS
AH 1 2 3
BH 1 1 1
CH 2 1 1
DH 1 1


I only have the idea to display the column and row title. Can anyone
give me some idea how to generate this dynamically?. Thanks

controllers/dashboard_controller.rb
def main
@hospital_code = Service.find_by_sql("select distinct hospital_code
from services")
@service_code = Service.find_by_sql("select distinct service_code
from services")
end


views/dashboard/main.html.erb
<table border="1">
<tr>
<th></th>
<% for s in @service_code %>
<th><%=s.col02 %></th>
<% end %>
</tr>

<% for h in @hospital_code %>
<tr>
<td><%=h.col01 %></td>
</tr>
<% end %>
</table>

Many thanks
Valentino
--
Posted via http://www.ruby-forum.com/.

Valentino Lun

unread,
Jan 14, 2009, 8:42:50 PM1/14/09
to rubyonra...@googlegroups.com
Dear all

Would you give me some hints on this? I don't know how to do...

Thank you.

Colin Law

unread,
Jan 15, 2009, 4:14:11 AM1/15/09
to Ruby on Rails: Talk
I would suggest building a two dimensional array @values[][] in the
controller, iterating through the records and filling in the cells.
This can then be displayed as rows and columns in the view. I suspect
there may be more elegant ways to do this in Ruby however.
Colin

On Jan 14, 10:12 am, Valentino Lun <rails-mailing-l...@andreas-s.net>
wrote:
> Dear all
>
> I have the following data in atableand I want to create a
> 2-dimensionaltableto display it.
> <tableborder="1">

Colin Law

unread,
Jan 15, 2009, 4:21:10 AM1/15/09
to rubyonra...@googlegroups.com
I would suggest building a two dimensional array @values[][] in the controller, iterating through the records and filling in the cells. This can then be displayed as rows and columns in the view. I suspect there may be more elegant ways to do this in Ruby however.
Colin

2009/1/15 Valentino Lun <rails-mai...@andreas-s.net>

robokos

unread,
Jan 15, 2009, 9:24:03 AM1/15/09
to Ruby on Rails: Talk
You can create a sql query that returns the data formatted the way you
want :

MODEL
-------------------------------------------------------------
class Service < ActiveRecord::Base

def self.getResults
self.find_by_sql("SELECT col01,
sum(if(col02='AS',col03, null )) as 'AS',
sum(if(col02='BS',col03, null )) as 'BS',
sum(if(col02='CS',col03, null )) as 'CS',
sum(if(col02='IS',col03, null )) as 'IS',
sum(if(col02='MS',col03, null )) as 'MS'
from services group by col01")
end

end

CONTROLLER
---------------------------------------------
def index
@services = Service.getResults

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @services }
end
end


VIEW
--------------------------------------------
<table border="1">
<tr>
<th></th>
<th>AS</th><th>BS</th><th>CS</th><th>IS</th><th>MS</th>
</tr>

<% for srv in @services %>
<tr>
<td><%= srv.col01 %></td>
<td><%= srv.AS %></td>
<td><%= srv.BS %></td>
<td><%= srv.CS %></td>
<td><%= srv.IS %></td>
<td><%= srv.MS %></td>
</tr>
<% end %>
</table>


Hope this helps.

Rob


On Jan 15, 8:21 pm, "Colin Law" <clan...@googlemail.com> wrote:
> I would suggest building a two dimensional array @values[][] in the
> controller, iterating through the records and filling in the cells. This can
> then be displayed as rows and columns in the view. I suspect there may be
> more elegant ways to do this in Ruby however.
> Colin
>
> 2009/1/15 Valentino Lun <rails-mailing-l...@andreas-s.net>

Saro V.

unread,
Jun 25, 2013, 5:48:38 AM6/25/13
to rubyonra...@googlegroups.com
If the content of Col1 and Col2 is dynamic?

I have this

MODEL
---------------------------------------------
class Forecast < ActiveRecord::Base

belongs_to :employee
belongs_to :prefshift

attr_accessible :employee_id, :giorno, :prefshift_id

end
----------------------------------------------

and i want to have this present on web

VIEW
-----------------------------------------------------------
01/07/2013 | 02/07/2013 | 03/07/2013 | ....
prefshift1 employee3 employee1
prefshit2 employee8 employee5
prefshift3 employee6
prefshift4 employee3 employee1 employee5
....

--------------------------------------------------------

Colin Law

unread,
Jun 25, 2013, 3:49:05 PM6/25/13
to rubyonra...@googlegroups.com
On 25 June 2013 10:48, Saro V. <li...@ruby-forum.com> wrote:
> If the content of Col1 and Col2 is dynamic?

Did you realise that you have replied to a thread from 2009? I hope
the OP has not been waiting all this time for an answer.

Colin
Reply all
Reply to author
Forward
0 new messages