将动态ID添加到ruby中的表格单元格

时间:2012-01-31 09:19:09

标签: ruby dynamic html-table

我正在循环这个TR,它重复了5次 我想为每个单元格使用DYNAMIC ID(这样第一行单元格的ID应该不同于第二行单元格的ID,依此类推)。

我的代码: -

<% @timesheetconsultants.each do |timesheetconsultant| %> 
          <tr>            
            <td><%= timesheetconsultant.HourType %></td>
            <td onclick="showlayer('time-picker',this,'monR')" id="MonR"><%= timesheetconsultant.Mo %></td>
            <td onclick="showlayer('time-picker',this,'tueR')" id="tueR"><%= timesheetconsultant.Tu %></td>
            <td onclick="showlayer('time-picker',this,'wedR')" id="wedR"><%= timesheetconsultant.We %></td>
            <td onclick="showlayer('time-picker',this,'thuR')" id="thuR"><%= timesheetconsultant.Th %></td>
            <td onclick="showlayer('time-picker',this,'friR')" id="friR"><%= timesheetconsultant.Fr %></td>
            <td onclick="showlayer('time-picker',this,'satR')" id="satR"><%= timesheetconsultant.Sa %></td>
            <td onclick="showlayer('time-picker',this,'sunR')" id="sunR"><%= timesheetconsultant.Su %></td>
            <td id="totalR"><%= timesheetconsultant.TotalHour %></td>
          </tr>
          <% end %>

这是以这种方式打印表

<table>
          <tr>            
            <td><%= timesheetconsultant.HourType %></td>
            <td onclick="showlayer('time-picker',this,'monR')" id="MonR"><%= timesheetconsultant.Mo %></td>
            <td onclick="showlayer('time-picker',this,'tueR')" id="tueR"><%= timesheetconsultant.Tu %></td>
            <td onclick="showlayer('time-picker',this,'wedR')" id="wedR"><%= timesheetconsultant.We %></td>
            <td onclick="showlayer('time-picker',this,'thuR')" id="thuR"><%= timesheetconsultant.Th %></td>
            <td onclick="showlayer('time-picker',this,'friR')" id="friR"><%= timesheetconsultant.Fr %></td>
            <td onclick="showlayer('time-picker',this,'satR')" id="satR"><%= timesheetconsultant.Sa %></td>
            <td onclick="showlayer('time-picker',this,'sunR')" id="sunR"><%= timesheetconsultant.Su %></td>
            <td id="totalR"><%= timesheetconsultant.TotalHour %></td>
          </tr>
          <tr>            
            <td><%= timesheetconsultant.HourType %></td>
            <td onclick="showlayer('time-picker',this,'monR')" id="MonR"><%= timesheetconsultant.Mo %></td>
            <td onclick="showlayer('time-picker',this,'tueR')" id="tueR"><%= timesheetconsultant.Tu %></td>
            <td onclick="showlayer('time-picker',this,'wedR')" id="wedR"><%= timesheetconsultant.We %></td>
            <td onclick="showlayer('time-picker',this,'thuR')" id="thuR"><%= timesheetconsultant.Th %></td>
            <td onclick="showlayer('time-picker',this,'friR')" id="friR"><%= timesheetconsultant.Fr %></td>
            <td onclick="showlayer('time-picker',this,'satR')" id="satR"><%= timesheetconsultant.Sa %></td>
            <td onclick="showlayer('time-picker',this,'sunR')" id="sunR"><%= timesheetconsultant.Su %></td>
            <td id="totalR"><%= timesheetconsultant.TotalHour %></td>
          </tr>
</table>

但我想要这张表

<table>
          <tr>            
            <td><%= timesheetconsultant.HourType %></td>
            <td onclick="showlayer('time-picker',this,'monR')" id="MonR"><%= timesheetconsultant.Mo %></td>
            <td onclick="showlayer('time-picker',this,'tueR')" id="tueR"><%= timesheetconsultant.Tu %></td>
            <td onclick="showlayer('time-picker',this,'wedR')" id="wedR"><%= timesheetconsultant.We %></td>
            <td onclick="showlayer('time-picker',this,'thuR')" id="thuR"><%= timesheetconsultant.Th %></td>
            <td onclick="showlayer('time-picker',this,'friR')" id="friR"><%= timesheetconsultant.Fr %></td>
            <td onclick="showlayer('time-picker',this,'satR')" id="satR"><%= timesheetconsultant.Sa %></td>
            <td onclick="showlayer('time-picker',this,'sunR')" id="sunR"><%= timesheetconsultant.Su %></td>
            <td id="totalR"><%= timesheetconsultant.TotalHour %></td>
          </tr>
          <tr>            
            <td><%= timesheetconsultant.HourType %></td>
            <td onclick="showlayer('time-picker',this,'monR')" id="MonOt"><%= timesheetconsultant.Mo %></td>
            <td onclick="showlayer('time-picker',this,'tueR')" id="tueOt"><%= timesheetconsultant.Tu %></td>
            <td onclick="showlayer('time-picker',this,'wedR')" id="wedOt"><%= timesheetconsultant.We %></td>
            <td onclick="showlayer('time-picker',this,'thuR')" id="thuOt"><%= timesheetconsultant.Th %></td>
            <td onclick="showlayer('time-picker',this,'friR')" id="friOt"><%= timesheetconsultant.Fr %></td>
            <td onclick="showlayer('time-picker',this,'satR')" id="satOt"><%= timesheetconsultant.Sa %></td>
            <td onclick="showlayer('time-picker',this,'sunR')" id="sunOt"><%= timesheetconsultant.Su %></td>
            <td id="totalR"><%= timesheetconsultant.TotalHour %></td>
          </tr>
</table>

1 个答案:

答案 0 :(得分:1)

利用数组索引使id唯一。试试这个

<% @timesheetconsultants.each_with_index do |timesheetconsultant,i| %> 
          <tr>            
            <td><%= timesheetconsultant.HourType %></td>
            <td onclick="showlayer('time-picker',this,'monR')" id="Mon<%=i%>"<%= timesheetconsultant.Mo %></td>
            <td onclick="showlayer('time-picker',this,'tueR')" id="tue<%=i%>"><%= timesheetconsultant.Tu %></td>
            <td onclick="showlayer('time-picker',this,'wedR')" id="wed<%=i%>"><%= timesheetconsultant.We %></td>
            <td onclick="showlayer('time-picker',this,'thuR')" id="thu<%=i%>"><%= timesheetconsultant.Th %></td>
            <td onclick="showlayer('time-picker',this,'friR')" id="fri<%=i%>"><%= timesheetconsultant.Fr %></td>
            <td onclick="showlayer('time-picker',this,'satR')" id="sat<%=i%>"><%= timesheetconsultant.Sa %></td>
            <td onclick="showlayer('time-picker',this,'sunR')" id="sun<%=i%>"><%= timesheetconsultant.Su %></td>
            <td id="totalR"><%= timesheetconsultant.TotalHour %></td>
          </tr>
          <% end %>