【问题标题】:Ruby display array valuesRuby 显示数组值
【发布时间】:2015-06-29 07:26:22
【问题描述】:

我不是 Ruby 开发人员。这是我第一次查看代码。我想构建一个动态表,我在下面的代码中对其进行了管理。但是,除了第一个值和最后一个值之外,我无法显示数组的所有内容。如何显示所有值?

感谢您的帮助!!

<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
  <th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
  <% tempTickets = @subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>  
  <% cnt = tempTickets.length %>
  <% tempTickets.each do |ticket| %>
<div>
<tr>
  <td><%= ticket['perfinaccnt-accountname'].first %></td>
<td><%= ticket['perfinaccnt-accountnumber'].first %></td>
<td><%= ticket['perfinaccnt-accountstatus'].first %></td>
<td><%= ticket['perfinaccnt-producttype'].first %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].first %></td>
</tr>
</div>
<div>
<tr>
  <td><%= ticket['perfinaccnt-accountname'].last %></td>
<td><%= ticket['perfinaccnt-accountnumber'].last %></td>
<td><%= ticket['perfinaccnt-accountstatus'].last %></td>
<td><%= ticket['perfinaccnt-producttype'].last %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].last %></td>
</tr>
</div>

  <% end %>
</table>
</div> 

【问题讨论】:

  • 你知道对象的属性吗?
  • 我完全不确定这些属性。因为,我能够显示数组的第一个和最后一个元素。我如何循环遍历它?
  • 您已经在使用每种方法遍历 tempTickets。
  • 这是您要找的吗? stackoverflow.com/questions/310634/…
  • 根据循环计数,我可以改变下面代码中的索引值吗?票['perfinaccnt-accountname'].

标签: ruby-on-rails ruby


【解决方案1】:

我不确定这里的数据结构,但您可以通过对 tempTickets 和所有后续对象执行 #inspect 来检查它。从外观上看,“ticket['perfinaccnt-accountname']”实际上是一个数组 因此,您可以循环浏览它。不是最快的方法,但它应该只做一个嵌套循环。

<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
  <th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
  <% tempTickets = @subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>
  <% cnt = tempTickets.length %>
  <% tempTickets.each do |ticket| %>
  <div><tr>
    <%  ticket.each do |k,v|
          v.each do |col| %>
            <td><%= col %></td>
          <% end %>
        <% end %>
  </tr></div>
  <% end %>
</table>
</div> 

【讨论】:

    【解决方案2】:

    你可以用三个嵌套循环来做到这一点:

    <%= tempTickets.each do |ticket|
        [
          'perfinaccnt-accountname',
          'perfinaccnt-accountnumber',
          'perfinaccnt-accountstatus',
          'perfinaccnt-producttype',
          'perfinaccnt-accountsrcsystem'
        ].each do |f|
            ticket[f].each do |tf|
                puts "<td>tf</td>";
            end if ticket[f]
        end 
    end %>
    

    【讨论】:

    • 感谢代码 - 但是,代码打印在表格外的文本下方。 "[#<0x69b60a95><0x17e29ce0>
    猜你喜欢
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多