代码之家  ›  专栏  ›  技术社区  ›  Sascha

将单个模型值(字符串)作为参数传递给可重用的javascript函数,以在MVC视图中设置表单元类。

  •  0
  • Sascha  · 技术社区  · 6 年前

    “我的视图”显示一个表,其中包含按列分组的某些数据的状态:

    • 准备好
    • 多恩
    • 跳过
    • 不适用

    我每次大约有6列和多行(下面是简化的示例) 我需要设置单元格背景颜色,这很容易通过CSS完成,但是每次通过视图中的重复代码设置样式类对于每一列来说都是非常耗时的,并且容易出错。

    所以我考虑使用一个javascript函数,使用模型值(上面列出的状态)的参数来动态地设置类css样式。

    我不完全确定该怎么做。

    样例代码:

    @using NS.Data
    @model NS.Models.PublicationsModel
    
    
    @{
        ViewBag.Title = "PublicationState";
        Layout = "~/Views/Shared/_Layout.cshtml";
        var PublicationState = Model.PublicationState.ToList();
    }
    @*Below the idea of the intended "reusable" javascript function*@
    <script type="text/javascript" language="javascript">
        $(function ToggleCellColour(StepState) {
    
            switch (StepState) {
                case "Ready":
                    return "cssReadyState";
                    break;
                case "Done":
                    return "cssDoneState";
                    break;
                case "Skipped":
                    return "cssSkippedState";
                    break;
                default:
                    return "";
                    break;
            }
        });
    </script>
    <h2>Publication State</h2>
    
    @Html.ActionLink("Back to previous page", "ActionName", "ControllerName", new { returnActive = true }, null)
    <table class="table">
        <tr>
            <th>Client</th>
            <th>Availability Communicated to Team</th>
            <th>Schedule Test</th>
            <th>Tested</th>
            <th>Planned Prod Implementation</th>
            <th>Implemented to Prod</th>
        </tr>
        @*Returned Publication States: N/A, Ready, Skipped, Done*@
        @foreach (var PubSt in PublicationState)
        {
               <tr>
                    <td>@PubSt.Client</td>
                    <td align="center" class="ToggleCellColour(@PubSt.Availability_Communicated_to_Team)"> @*This is where I thought of calling the javascript method to determine the css formatting for the cell*@
                        @if (PubSt.Availability_Communicated_to_Team != "N/A")
                        {
                            if (PubSt.Availability_Communicated_to_Team == "Done" || PubSt.Availability_Communicated_to_Team == "Skipped")
                            {
                                <img src="~/Images/MailClosed.png" />
                            }
                            else @*The "Ready" State*@
                            {
                                <img src="~/Images/MailOpen.png" />
                            }
                        }
                        else
                        {
                            @PubSt.Availability_Communicated_to_Team
                        }
                    </td>
                    <td align="center" class="ToggleCellColour(@PubSt.Schedule_Test)">
                        @if (PubSt.Schedule_Test != "N/A")
                        {
                            if (PubSt.Schedule_Test == "Done" || PubSt.Schedule_Test == "Skipped")
                            {
                                <img src="~/Images/MailClosed.png" />
                            }
                            else
                            {
                                <img src="~/Images/MailOpen.png" />
                            }
                        }
                        else
                        {
                            @PubSt.Schedule_Test
                        }
                    </td>
                    <td align="center" class="ToggleCellColour(@PubSt.Tested)">
                        @if (PubSt.Tested != "N/A")
                        {
                            if (PubSt.Tested == "Done" || PubSt.Tested == "Skipped")
                            {
                                <img src="~/Images/MailClosed.png" />
                            }
                            else
                            {
                                <img src="~/Images/MailOpen.png" />
                            }
                        }
                        else
                        {
                            @PubSt.Tested
                        }
                    </td>
                    <td align="center" class="ToggleCellColour(@PubSt.Planned_Prod_Implementation)">
                        @if (PubSt.Planned_Prod_Implementation != "N/A")
                        {
                            if (PubSt.Planned_Prod_Implementation == "Done" || PubSt.Planned_Prod_Implementation == "Skipped")
                            {
                                <img src="~/Images/MailClosed.png" />
                            }
                            else
                            {
                                <img src="~/Images/MailOpen.png" />
                            }
                        }
                        else
                        {
                            @PubSt.Planned_Prod_Implementation
                        }
                    </td>
                    <td align="center" class="ToggleCellColour(@PubSt.Implemented_on_Prod)">
                        @if (PubSt.Implemented_on_Prod != "N/A")
                        {
                            if (PubSt.Implemented_on_Prod == "Done" || PubSt.Implemented_on_Prod == "Skipped")
                            {
                                <img src="~/Images/MailClosed.png" />
                            }
                            else
                            {
                                <img src="~/Images/MailOpen.png" />
                            }
                        }
                        else
                        {
                            @PubSt.Implemented_on_Prod
                        }
                    </td>
                </tr>
            }
        }
    </table>
    

    可以像显示图像那样重复if-else语句代码,但是我认为通过使用javascript函数来简化操作,可以省去麻烦并引入较少的错误。

    任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  2
  •   JuanR    6 年前

    如果您使用jquery,可以向单元格添加数据属性以及一些标识符(例如类名),使您可以将其作为目标,然后让不引人注目的JS为您设置CSS类:

    <tr>
        <td class="coloredCell" data-availability="@PubSt.Planned_Prod_Implementation">stuff</td>
    </tr>
    

    然后使用JS将文档的颜色设置为就绪:

    $(function ()
    {
        $('.coloredCell').each(function (index, element)
        {
            var availability = $(element).attr("data-availability");
            var cssClass;
            switch (availability)
            {
                case "Ready":
                    cssClass = "cssReadyState";
                    break;
                case "Done":
                    cssClass = "cssDoneState";
                    break;
                case "Skipped":
                    cssClass = "cssSkippedState";
                    break;
                default:
                    cssClass = "";
                    break;
            }
            $(element).addClass(cssClass);
        });
    });