[英]Using partials, how do I render `striped` table rows in a DRY way?
So I have some view code that looks like this: 所以我有一些看起来像这样的视图代码:
<div class="box-content">
<table class="table table-properties">
<tbody>
<%= render :partial => 'property', collection: @search.listings, as: :listing %>
</tbody>
</table>
</div>
In that _property.html.erb
, I have this: 在
_property.html.erb
,我有这个:
<tr>
<td class="span2">
<%= link_to listing_path(listing), :class => "thumbnail thumb2" do %>
<%= image_tag "room_1.jpg", :alt => "Lucas" %>
<% end %>
</td>
<td>
<h2><%= link_to listing.headline, listing_path(listing) %></h2>
<h5><%= listing.listing_type.name if listing.listing_type "#{listing.neighborhood.name.capitalize}" %></h5>
<h5>Maintenance <%= number_to_currency(listing.maintenance) %></h5>
</td>
<td class="span1">
<h2 class="price"><%= number_to_currency(listing.price)%></h2>
<h5><%= "#{listing.num_bedrooms} bed, #{listing.num_bathrooms} bath" %></h5>
</td>
</tr>
Basically, I want to generate that above code exactly, for each row, the only difference is I want every 2nd row (ie all even numbered rows) to have the class=striped
..ie <tr class=striped>
. 基本上,我想准确地生成上面的代码,对于每一行,唯一的区别是我希望每个第二行(即所有偶数行)都具有
class=striped
..ie <tr class=striped>
。
Thoughts on how to do this in a DRY way? 关于如何以干燥的方式做到这一点的想法?
Have you tried using cycle
and current_cycle
? 你尝试过使用
cycle
和current_cycle
吗?
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-cycle
<tr class="<%= cycle('odd', 'even') -%>">
<!-- etc -->
</tr>
This alternates your classes with odd
and even
and IMHO should also work when render a collection. 这会使您的班级与
odd
和even
交替,恕我直言也应该在渲染集合时起作用。 If you need the value of the actual cycle multiple times, you get it with current_cycle
(because calling cycle
multiple times would mess up your cycles, unless you want that). 如果你需要多次实际循环的值,你可以用
current_cycle
得到它(因为多次调用cycle
会扰乱你的循环,除非你想要那样)。
Won't it be better just to use :nth-child()
css selector? 使用
:nth-child()
css选择器不是更好吗? http://www.w3schools.com/cssref/sel_nth-child.asp http://www.w3schools.com/cssref/sel_nth-child.asp
You can try something like this: 你可以尝试这样的事情:
<tr class="<%=cycle("odd", "even") %>">
<td class="span2">
<%= link_to listing_path(listing), :class => "thumbnail thumb2" do %>
<%= image_tag "room_1.jpg", :alt => "Lucas" %>
<% end %>
</td>
<td>
<h2><%= link_to listing.headline, listing_path(listing) %></h2>
<h5><%= listing.listing_type.name if listing.listing_type "#{listing.neighborhood.name.capitalize}" %></h5>
<h5>Maintenance <%= number_to_currency(listing.maintenance) %></h5>
</td>
<td class="span1">
<h2 class="price"><%= number_to_currency(listing.price)%></h2>
<h5><%= "#{listing.num_bedrooms} bed, #{listing.num_bathrooms} bath" %></h5>
</td>
</tr>
Do it with jquery: 用jquery做:
$(document).ready(function(){
$("table.table-properties > tbody > tr:odd").addClass("odd");
$("table.table-properties > tbody > tr:not(.odd)").addClass("even");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.