我如何在Rails的嵌套表中打印id和sub_id

问题描述 投票:0回答:1

我正在将嵌套表打印在导轨中。现在我想将其id打印为1,2,3,依此类推,将嵌套内容sub_id打印为1.1,1.2 ... 3.1,依此类推。我该怎么做?

<div class="table-scroll">
  <table>
    <thead>
      <tr>
        <th width="300">Task Name</th>
        <th width="40">Planned start date</th>
        <th width="40">Planned end date</th>
      </tr>
    </thead>

    <tbody>
      <% @stages.each do |stage| %>
        <tr class="stage">
          <td><%= stage.stage %></td>
          <td><%= stage.planned_start_date.strftime("%d-%m-%Y") %></td>
          <td><%= stage.planned_end_date.strftime("%d-%m-%Y") %></td>
        </tr>

        <% stage.tasks.each do |task| %>
          <tr>
            <td class="text-center"><%= task.task_name %></td>
            <td><%= task.planned_start_date.strftime("%d-%m-%Y") %></td>
            <td><%= task.planned_end_date.strftime("%d-%m-%Y") %></td>
          </tr>

          <% task.sub_tasks.each do |sub_task| %>
            <tr>
              <td class="text-right"><%= sub_task.sub_task_name %></td>
              <td><%= sub_task.planned_start_date.strftime("%d-%m-%Y") %></td>
              <td><%= sub_task.planned_end_date.strftime("%d-%m-%Y") %></td>
            </tr>
          <% end %>
        <% end %>
      <% end %>
    </tbody>
  </table>
</div>

我如何向用户添加列并为任务打印1,为sub_task打印1.1,1.2...。我不能从表中获取ID,因为它们是多个用户(配置文件)。我想从前端打印ID。

ruby-on-rails ruby-on-rails-5
1个回答
0
投票

听起来您需要一个任务计数器和一个子任务计数器。

...
<% stage.tasks.each_with_index do |task, task_index| %>
  <tr>
    <td class="text-center"><%= "#{task_index + 1} #{task.task_name}"%></td>
    <td><%= task.planned_start_date.strftime("%d-%m-%Y") %></td>
    <td><%= task.planned_end_date.strftime("%d-%m-%Y") %></td>
  </tr>

  <% task.sub_tasks.each_with_index do |sub_task, sub_task_index| %>
    <tr>
      <td class="text-right"><%= "#{task_index + 1}.#{sub_task_index + 1} #{sub_task.sub_task_name }"%></td>
      <td><%= sub_task.planned_start_date.strftime("%d-%m-%Y") %></td>
      <td><%= sub_task.planned_end_date.strftime("%d-%m-%Y") %></td>
    </tr>
  <% end %>
<% end %>

阅读有关Enumerable#each_with_index的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.