2015-11-01 63 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源代碼 - 我需要查看哪些類正在分配以及它們如何工作。給我看屏幕上的一堆顏色並沒有幫助 –

相關問題