2012-01-31 86 views
0

我正在循環這個TR,它重複5次。
我想爲每個單元使用DYNAMIC ID(以便第一行的單元格的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 %> 

它打印表格THIS WAY

<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

利用數組的索引使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 %>