使用Ruby on Rails以列而非行显示数据 [英] Display data in columns not rows using Ruby on Rails

查看:46
本文介绍了使用Ruby on Rails以列而非行显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网页中以列"而不是行"显示数据.

I would like to display data in Columns rather than Rows in my web page.

使用Ruby on Rails最有效的方法是什么?

What is the most efficient way to do this using Ruby on Rails?

非常感谢您的帮助!

推荐答案

简单的解决方案是使用数组旋转"信息.像这样的(伪)代码(无法检查atm)
控制器代码:

Simple solution would be to 'rotate' the information, using an array. Like this (pseudo) code (cannot check if atm)
Controller code:

@stuffs = Stuff.find(:all)
@rotated = []
@rotated[0] = [] # Stuff column0
@rotated[1] = [] # Stuff column1
# etc
index = 0
# Now put the stuff in an array for easy(ier) access
@stuffs.each do |stuff|
  @rotated[0][index] = stuff.detail0
  @rotated[1][index] = stuff.detail1
  index += 1
end

在您的视图中,您将需要以下内容:

In your View you'd need something like this:

<table>
Table head here!
<% 2.times do |row| %>  # Build table (we have 2 stuff details, hence the 2 here)
  <tr>
  <% index.times do |column| %>
    <td><%= @rotated[row][column] %></td>
  <% end %>
  </tr>
<% end %>
<table>

当然有更好的解决方案,但这对我来说似乎是最简单/一般的.旋转某些模型中的一些数据.

There are of course better solution, but this seems the most simple/general to me. To rotate some data from some Model.

就像其他人所说的那样,我们可能会更好地帮助您!

Like others said with more information we can help you probably much better!

这篇关于使用Ruby on Rails以列而非行显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆