MVC表如何改变文字颜色的条件 [英] Mvc table how to change the text color conditional

查看:92
本文介绍了MVC表如何改变文字颜色的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果模型值超过-等于-等于0,我想更改特定表列的文本颜色.详细说明,如果值超过0则为绿色,红色为零且为蓝色.相等.

i would like to change the text color of a specific table column, if the model value is over - unter - equal of 0. Detailed, it will be green if the value is over zero, red unter zero and blue when it is equal.

我的桌子是上面的

<div class="row">
    <table id="RealtimeSharesTable" class="dataTable table table-hover table-striped table-bordered">
         <thead>
            <tr>
                <th>@Html.DisplayNameFor(model => dummy.RealtimeOrder)</th>
                <th>@Html.DisplayNameFor(model => dummy.AlternativeName)</th>
                <th>@Html.DisplayNameFor(model => dummy.Last)</th>
                <th>@Html.DisplayNameFor(model => dummy.PctChange)</th>
                <th>@Html.DisplayNameFor(model => dummy.Bid)</th>
                <th>@Html.DisplayNameFor(model => dummy.BSize)</th>
                <th>@Html.DisplayNameFor(model => dummy.Ask)</th>
                <th>@Html.DisplayNameFor(model => dummy.ASize)</th>
                <th>@Html.DisplayNameFor(model => dummy.Capital)</th>
                <th>@Html.DisplayNameFor(model => dummy.High)</th>
                <th>@Html.DisplayNameFor(model => dummy.Low)</th>
                <th>@Html.DisplayNameFor(model => dummy.Volume)</th>
            </tr>
        </thead>
        <tbody>
            @foreach (ShareViewModel item in Model.Shares)
            {
                <tr id="@item.Symbol.Replace(".", "_").Replace("=","_")">
                    <td>@Html.DisplayFor(model => item.RealtimeOrder)</td>
                    <td>@Html.DisplayFor(model => item.AlternativeName)</td>
                    <td>@Html.DisplayFor(model => item.Last)</td>
                    <td>@Html.DisplayFor(model => item.PctChange) %</td>
                    <td>@Html.DisplayFor(model => item.Bid)</td>
                    <td>@Html.DisplayFor(model => item.BSize)</td>
                    <td>@Html.DisplayFor(model => item.Ask)</td>
                    <td>@Html.DisplayFor(model => item.ASize)</td>
                    <td>@Html.DisplayFor(model => item.Capital)</td>
                    <td>@Html.DisplayFor(model => item.High)</td>
                    <td>@Html.DisplayFor(model => item.Low)</td>
                    <td>@Html.DisplayFor(model => item.Volume)</td>
                </tr>
            }
        </tbody>
    </table>
</div>

我需要这样做的列是"item.PctChange",具体取决于他的价格.我尝试了许多解决方案,但我想征求专家的建议.

The column that i need this to done is the "item.PctChange", depending of his price. I have tried many solution but i would like to have a suggestion from an expert.

谢谢.

推荐答案

这是我这样做的方法..

Here is my way of doing this ..

直接在td

三元运算符将为if else if else

ie:item.PctChange > 0? "green" : item.PctChange == 0? "blue" : "red"

向要着色的td元素添加一个类.

Add a class to the td element which you want to color.

因此,如下更改您的td.

<td class="@( item.PctChange > 0? "green" : item.PctChange == 0? "blue" : "red")">@Html.DisplayFor(model => item.PctChange) %</td>

对于类,也有一个CSS规则,可以为td相应地着色.

Also have a CSS rule for the classes to color the td accordingly.

.green{
  background-color:green;
 }

.blue{
  background-color:blue;
 }

.green{
  background-color:red;
 }

这篇关于MVC表如何改变文字颜色的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆