2015-11-01 34 views
1

index.html.erbRails,是否可以根据包含余额的表的列值更改颜色?

<% balance = 0 %> 

    <table align="center" width="50%" cellpadding="1" cellspacing="5"> 
     <tr> 
      <td>Amount</td> 
      <td>Discount</td> 
      <td>Paid</td> 
      <td>Balance</td> 
     </tr> 

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

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

    <td class="col-1"><%= statement.date %></td> 
    <td class="col-3"><%= 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> 

<center><p><b><%= number_to_currency(balance.abs, :unit => 'AED ', :delimiter => ",", :precision => 2) %></b></p></center> 

我想有一个是平衡实体的颜色变化时余额是正就必须表现出黑色和变负时,它必须显示为红色。

这里是我的application.css.scss

.pos { color: #000; } 
.neg { color: #f00; } 

我没有得到的结果如预期。

请看下面的结果;

Sample data

picture

+0

是的,这是可能的。然而,当你加载页面时,你看到了什么? –

+0

是的,你可以通过点击帖子底部的示例数据链接来查看结果。 –

+0

毕竟,我找不到解决方案。请指导... –

回答

0

里克是正确的,但有一个小故障。

正如我在上面的评论中提到的那样,在计算新值之前,正在使用余额值来分配类.neg or .pos。这是负责它采取以前的balance价值给它的类如附件中所示的样本。

下面是你如何使用它来得到它正常工作之前计算出新的价值:

<% balance = 0 %> 

<table align="center" width="50%" cellpadding="1" cellspacing="5"> 
    <tr> 
     <td>Amount</td> 
     <td>Discount</td> 
     <td>Paid</td> 
     <td>Balance</td> 
    </tr> 

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

    <tr> 

     <td><%= statement.amount %></td> 

     <td class="neg"><%= statement.discount %></td> 
     <td class="neg"><%= statement.paid %></td> 

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

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

     <td class="<%= color %>"><%= balance %></td> 

    </tr> 

    <% end %> 

</table> 

发现,我们首先在任何操作在使用它之前计算的balance新值。这样,在分配课程时,balance保留了当前循环的真实和正确的值。

试试这个,让我们知道你在视图上得到了什么......

+0

非常感谢,最后它工作!第二件事我想展示没有负面信号的负平衡,只有红色。再次感谢您的时间和辛勤工作。保持!!! –

+0

你只需使用'absolute'方法=>'.abs'就可以了。 2.0.0:004> a = -66.55 => -66.55 a.abs => 66.55 ' –

+0

谢谢先生,我明白了。 –

0

当然,just use CSS

#app/assets/stylesheets/application.css 
tr.pos { color: #000; } 
tr.neg { color: #f00; } 

#app/views/controller/index.html.erb 
<% color = balance >= 0 ? "pos" : "neg" %> 

<td class="col-1 <%= color %>"><%= number_with_precision(statement.discount, :delimiter => ",", :precision => 2) %></td> 
<td class="col-1 <%= color %>"><%= number_with_precision(statement.paid, :delimiter => ",", :precision => 2) %></td> 
<td class="col-1 <%= color %>"><%= number_with_precision(balance += statement.amount.to_f - statement.discount.to_f - statement.paid.to_f, :delimiter => ",", :precision => 2) %></td> 
+0

你是什么意思的“不提供所需的结果”?你能给我一个关于发生什么的描述吗? –

+0

我看到颜色 - 问题是什么? –

+0

今天我的猫醒了。请帮忙!哦?你需要更多的信息? ....您需要为页面提供呈现的HTML源代码 - 我需要查看哪些类正在分配以及它们如何工作。给我看屏幕上的一堆颜色并没有帮助 –