Ich möchte eine bedingte Formatierung in meinen Ansichten vornehmen.Bedingte Textfarbformatierung in Rails 4.2.1
Finden Sie das Beispielbild für die Referenz.
Ich will "cash received" in roter Farbe angezeigt werden soll.
Zuerst möchte ich den Zustand so prüfen, dass, wenn "cash" in der Beschreibung steht, der ganze String, der "cash received" ist, in roter Farbe angezeigt werden muss.
<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 == "TT" || 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>
neu editierten index.html
<div class="container-fluid">
<h1>361° YAZD Statement</h1>
<% 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>
<td class="col-3"><%= span_with_possibly_red_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>
statements_helper.rb
require 'active_support/concern'
module StatementsHelper
extend ActiveSupport::Concern
include ActionView::Helpers::TagHelper
def span_with_possibly_red_color(content)
classes = if content.split.map(&:downcase).include?("cash")
{class: "red"}
else
{}
end
content_tag(:span, content, classes)
end
end
HTML-Ausgabeelemente für nicht bestanden haben;
für Bargeld
<td class="col-3"><span class="red">cash</span></td>
für Bargeld
<td class="col-3"><span class="red">cash received</span></td>
Dies wäre besser aus mit jQuery als seine DOM-Manipulation getaggt. So viel Arbeit auf dem Server zu leisten ist in den meisten Fällen keine effiziente Lösung. – meshpi
Danke für die Antwort. Könnten Sie bitte erläutern, wie ich das auf einfache Art und Weise tun würde? –
Siehe meine Lösung unten und verknüpfte jsfiddle, um es in Aktion zu sehen. – meshpi