【发布时间】:2017-03-03 18:34:16
【问题描述】:
我正在按日期创建多个表,并且我希望我的 html 表行根据多个条件变为不同的颜色。在this question 的帮助下,我在班次视图中尝试了以下内容:
<% (Date.current - 6.months..Date.current + 6.months).each do |date|%>
<% if !Shift.where(date: date).blank? %>
<% @shifts = LaunchShift.where(date: date).all.order(start_time: :asc, booked: :desc) %>
<h2><%= date.strftime('%e %b %Y') %></h2>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>...</th>
</tr>
</thead>
<% @shifts.each_with_index do |shift, index| %>
<% if shift.booked? %>
<% @booker = Member.find(shift.booked_by) %>
<% end %>
<tbody>
<% if shift.booked? %> <---Trying this for changing colour
<tr style="background-color:red">
<% else %>
<tr>
<% end %>
<td><%= shift.start_time.strftime("%H:%M") %></td>
<% if shift.booked? %>
<td>Booked</td>
<td><%= link_to @booker.name, member_path(@booker) %></td>
<% else %>
<td>Free</td>
<td></td>
<% end %>
[...]
</tr>
</tbody>
<% end %>
</table>
</div>
<% end %>
但只有一些标记为已预订的行是红色的,尽管 shifts.booked?返回为真。这种方法的工作方式与我认为的不同吗?有没有更好的方法来做到这一点,它不使用 JS/JQuery(不知道那些)。有什么建议吗? 谢谢!
【问题讨论】:
标签: css ruby-on-rails html-table