How to create a two dimensional table


#1

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

<% for s in @service_code %> <% end %>

<% for h in @hospital_code %>

<% end %>
<%=s.col02 %>
<%=h.col01 %>

Many thanks
Valentino


#2

Dear all

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

Thank you.

Valentino


#3

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 L. removed_email_address@domain.invalid


#4

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

<% for srv in @services %>

<% end %>
AS BS CS IS MS
<%= srv.col01 %> <%= srv.AS %> <%= srv.BS %> <%= srv.CS %> <%= srv.IS %> <%= srv.MS %>

Hope this helps.

Rob


#5

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 L. removed_email_address@domain.invalid


#6

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

MODEL

class Prefshift < ActiveRecord::Base

has_one :forecast

has_many :employee_prefshifts, :dependent => :destroy
has_many :employees, through: :employee_prefshifts, :dependent =>
:destroy

attr_accessible :cod, :descr, :fine, :inizio, :nome, :stato,
:ggsettimana_ids

def self.getCods
self.find_by_sql(“SELECT id,cod FROM prefshifts WHERE stato=true
ORDER BY inizio ASC”)
end

end

MODEL

class Employee < ActiveRecord::Base

has_one :forecast

has_many :employee_prefshifts, :dependent => :destroy
has_many :prefshifts, through: :employee_prefshifts, :dependent =>
:destroy

attr_accessible :acceso, :cognome, :contract_id, :dataAssunzione,
:malattia, :matricola, :nome, :prefshift_ids

after_initialize do
self.acceso ||= true
end
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


Instead of prefshift1,prefshift2 etc i want display the prefshift.cod


#7

On 25 June 2013 10:48, Saro V. removed_email_address@domain.invalid 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