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/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dear all Would you give me some hints on this? I don''t know how to do... Thank you. Valentino -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Dear all > > I have the following data in atableand I want to create a > 2-dimensionaltableto 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 > <tableborder="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 viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > Dear all > > Would you give me some hints on this? I don''t know how to do... > > Thank you. > > Valentino > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > > > Dear all > > > Would you give me some hints on this? I don''t know how to do... > > > Thank you. > > > Valentino > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 .... -------------------------------------------------------- -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/58640dfa14004881f8cbe407cd88192e%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On 25 June 2013 10:48, Saro V. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLumWOb0x6-eRe1_zS3eT-GSQxT-gPb2%2BxYzfqGgvwbNBg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.