简体   繁体   English

Rails部分入侵:每个部分传递2条记录? 还是更好的主意?

[英]Rails partial hacking: pass 2 records per partial? Or a better idea?

I'm creating a view for printing labels. 我正在创建一个用于打印标签的视图。 I've HTML'd / CSS'd a table with the right dimensions for 2 columns of 5 labels: 我已经用HTML / CSS编写了一个表格,其中尺寸正确,包含2个5列的标签:

<div id="inventory_labels">
  <table class="table-bordered" id="avery8163">
    <thead>
      <tr>
        <td style='width: .155in;'></td>
        <td style='width: 4in;'></td>
        <td style='width: .19in;'></td>
        <td style='width: 4in;'></td>
        <td style='width: .155in;'></td>
      </tr>
    </thead>
    <tfoot>
      <tr>
        <td colspan="5"></td>
      </tr>
    </tfoot>
    <tbody>
      <% 40.times do # just a placeholder for the actual loop / partial %>
        <tr>
          <td></td>
          <td>LABEL #1 (should be a record)</td>
          <td></td>
          <td>LABEL #2 (should be a record)</td>
          <td></td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>

And the SCSS: 和SCSS:

#inventory_labels {
  width: 8.5in;
  margin-right: auto;
  margin-left: auto;
}

table#avery8163 {
  table-layout: fixed;
  width: 8.5in;

  thead, tfoot {
    td {
      height: .5in;
    }
  }

  tbody {
    td {
      height: 2in;
    }
  }
}

@media print {
  table#avery8163 {
    thead { 
      display: table-header-group;
    }

    tfoot { 
      display: table-footer-group;
    }
  }
}

I'd like to use a partial to loop over the records in the <tbody> , but I need to loop over 2 at a time, one for each "LABEL". 我想使用局部循环遍历<tbody>的记录,但是我一次需要循环遍历2个,每个“ LABEL”一个。

Any suggestions or ideas? 有什么建议或想法吗?

I did play with formatting this as divs (like this strategy from the dark ages) instead of table columns but the math got real messy since the page-margin and gutters don't line up well. 我确实将其格式化为div(例如,黑暗时代的这种策略 )而不是表格列,但是由于页面边距和装订线排列不正确,因此数学变得非常混乱。

FIRST RUN 首轮

This is really ugly, right? 这真的很丑吧?

@counts is the ActiveRecord array of records I want to loop through, defined in the controller. @counts是我要@counts的ActiveRecord记录数组,在控制器中定义。

<tbody>
  <% n = 0 %>
  <% @loop.times do # @loop = (@counts.count/2).ceil %>
    <tr>
      <td></td>
      <td>
        <%= render partial: 'label', locals: { count: @counts[n] } %>
        <% n += 1 %>
      </td>
      <td></td>
      <td>
        <% if @counts[n].present? %>
          <%= render partial: 'label', locals: { count: @counts[n] } %>
        <% end %>
        <% n += 1 %>
      </td>
      <td></td>
    </tr>

  <% end %>
</tbody>

Seeing that it's not a problem to use your collection as an array, you can use in_groups_of method https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of 看到将集合用作数组不是问题,可以使用in_groups_of方法https://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of

<% @objects.in_groups_of(2, false) do |obj1, obj2| %>
  <tr>
    <td></td>
    <td><%= #something with obj1 %></td>
    <td></td>
    <td><%= #something with obj2, you should check if it's not nil though if the array has an odd length %></td>
    <td</td>
  </tr>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM