2016-02-28 43 views
0

我想在我的视图中做一些条件格式。Rails 4.2中的条件文本颜色格式

请查找示例图片以供参考。

sample.jpg

对于“现金”这是工作正常,但我想这样做的“收到的现金”,“信用票据”等..

index.html.erb

<div class="container-fluid"> 

    <% balance = 0 %> 

    <div class="table-responsive myTable"> 

    <table class="table listing text-center"> 
     <tr class="tr-head"> 
     <td>Date</td> 
     <td>Description</td> 
     <td>Amount</td> 
     <td>Discount</td> 
     <td>Paid</td> 
     <td>Balance</td> 
     </tr> 

     <tr> 
     <td></td> 
     </tr> 

     <% @statements.each do |statement| %> 

     <tr class="tr-<%= cycle('odd', 'even') %>"> 

     <td class="col-1"><%= statement.date %></td> 

     <% color = (statement.description == "cash") ? "neg" : "pos" %> 

     <td class="col-3 <%= color %>"><%= statement.description %></td> 

     <td class="col-1"><%= number_with_precision(statement.amount, :delimiter => ",", :precision => 2) %></td> 

     <td class="col-1 neg"><%= number_with_precision(statement.discount, :delimiter => ",", :precision => 2) %></td> 

     <td class="col-1 neg"><%= number_with_precision(statement.paid, :delimiter => ",", :precision => 2) %></td> 


     <% balance += statement.amount.to_f - statement.discount.to_f - statement.paid.to_f %> 

     <% color = balance >= 0 ? "pos" : "neg" %> 

     <td class="col-1 <%= color %>"><%= number_with_precision(balance.abs, :delimiter => ",", :precision => 2) %></td> 

     </tr> 

     <% end %> 

    </table> 
    </div> 
</div> 
+0

什么是否定的?它是红色的吗? – Sravan

+0

是的,先生.neg {0}颜色:#f00; } –

回答

0

海优条件只适用于cash,所以只给它红色,用此代替index.html.erb

<div class="container-fluid"> 
 

 
    <% balance = 0 %> 
 

 
    <div class="table-responsive myTable"> 
 

 
    <table class="table listing text-center"> 
 
     <tr class="tr-head"> 
 
     <td>Date</td> 
 
     <td>Description</td> 
 
     <td>Amount</td> 
 
     <td>Discount</td> 
 
     <td>Paid</td> 
 
     <td>Balance</td> 
 
     </tr> 
 

 
     <tr> 
 
     <td></td> 
 
     </tr> 
 

 
     <% @statements.each do |statement| %> 
 

 
     <tr class="tr-<%= cycle('odd', 'even') %>"> 
 

 
     <td class="col-1"><%= statement.date %></td> 
 

 
     <% color = (statement.description == "cash" || statement.description == "cash received" || statement.description == "credit note") ? "neg" : "pos" %> 
 

 
     <td class="col-3 <%= color %>"><%= statement.description %></td> 
 

 
     <td class="col-1"><%= number_with_precision(statement.amount, :delimiter => ",", :precision => 2) %></td> 
 

 
     <td class="col-1 neg"><%= number_with_precision(statement.discount, :delimiter => ",", :precision => 2) %></td> 
 

 
     <td class="col-1 neg"><%= number_with_precision(statement.paid, :delimiter => ",", :precision => 2) %></td> 
 

 

 
     <% balance += statement.amount.to_f - statement.discount.to_f - statement.paid.to_f %> 
 

 
     <% color = balance >= 0 ? "pos" : "neg" %> 
 

 
     <td class="col-1 <%= color %>"><%= number_with_precision(balance.abs, :delimiter => ",", :precision => 2) %></td> 
 

 
     </tr> 
 

 
     <% end %> 
 

 
    </table> 
 
    </div> 
 
</div>

就是这样。

+0

你试过这个吗? – Sravan

+0

感谢您的回复,但我不希望所有描述值都显示为红色。我希望第一个值显示为黑色,因为它是发票。等待你的回复.. –

+0

你有多少价值,你需要红色? – Sravan