根据条件更改GridMvc列的背景 [英] Changing Background of GridMvc Column on Condition

查看:61
本文介绍了根据条件更改GridMvc列的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net mvc项目中使用Grid MVC.如果有某些条件,例如在需要更改行的背景颜色的情况下,我得到了要求列名称"Response"等于"ERROR",那么我需要将行颜色更改为红色

I am using Grid MVC in asp.net mvc project . I get requirement where i need to change the background color of row if there is certain condition e.g. column name "Response" equals to "ERROR" then i need to change row color to red

@model IEnumerable<Grid.Models.Client>
@using GridMvc.Html

@{
Layout = null;
 }

 <!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
<link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>    

<title>Index</title>
 </head>

@using (Html.BeginForm("Search", "Client"))
{
 <body style="margin:30px">

<br />
<table>
        <tr>
            <td>
                <h4> Search Criteria:</h4>
            </td>
        </tr>
        <tr>
            <td></td>
        </tr>
    <tr>
            <td>
                 <p>Reference :</p>
                 <input type="text" name="ref" style="width:120px" />
            </td>
            <td>
                 <p>Date :</p>
                 <input type="date" name="date" style="width:150px" />
            </td>
            <td>
                 <p>Time :</p>
                 <input type="time" name="time" style="width:120px" />
            </td>
        </tr>
        <tr>
            <td></td>

        </tr>
        <tr>
            <td>
                <input type="submit" style="width:60px" value="Search" />
            </td>
        </tr>
    </table>

<br />

<div style="width:800px;">        
    @Html.Grid(Model).Columns(columns => 
                {                        
                    columns.Add(c => c.Type).Titled("Type").Filterable(true);
                    columns.Add(c => c.Description).Titled("Description");
                    columns.Add(c => c.DateTime).Titled("DateTime");
                    columns.Add(c => c.Reference).Titled("Reference");
                    columns.Add(c => c.Response).Titled("Response");
                }).WithPaging(10).Sortable(true)
</div>

}

@using GridMvc.Columns
@model GridMvc.IGrid
@if (Model == null){ return; }
@{
var gridClinetId = "grid-mvc-" + (string.IsNullOrEmpty(Model.Id) ? Guid.NewGuid().ToString() : Model.Id);
}
<div id="@gridClinetId" class="grid-outer @(Model.EnablePaging ? "paged" : string.Empty)">
@* Draw grid top items infomation panel *@
<div class="grid-wrap">
    <table class="table table-striped grid-table" data-lang="@Model.Language">
        @* Draw grid header *@
        <thead>
            <tr>
                @foreach (IGridColumn column in Model.Columns)
                {
                    @Html.Raw(column.HeaderRenderer.Render(column, column.Title))
                }
            </tr>
        </thead>
        <tbody>
            @if (Model.ItemsCount == 0)
            {
                <tr class="grid-empty-text">
                    <td colspan="@Model.Columns.Count()">
                        @Model.EmptyGridText
                    </td>
                </tr>
            }
            else
            {
                foreach (object item in Model.ItemsToDisplay)
                {
                <tr class="grid-row @Model.GetRowCssClasses(item)">
                    @foreach (IGridColumn column in Model.Columns)
                    {
                        @Html.Raw(column.CellRenderer.Render(column, column.GetCell(item).ToString()))
                    }
                </tr>
                }
            }
        </tbody>
    </table>
    @if (Model.EnablePaging && Model.Pager != null && Model.Pager.PageCount > 1)
    {
        <div class="grid-footer">
            <div class="grid-footer-info">
                Displaying items @(((Model.Pager.CurrentPage - 1) * Model.Pager.PageSize) + 1)
                - @(((Model.Pager.CurrentPage - 1) * Model.Pager.PageSize) + Model.DisplayingItemsCount)
                (@Model.ItemsCount)
            </div>
            @Html.Partial("_GridPager", Model.Pager)
        </div>
    }
@* Draw pager *@
</div>
</div>
@* Init Grid.Mvc client script *@
<script>
if (typeof (jQuery) != 'undefined' && typeof (jQuery.fn.gridmvc) != 'undefined') {
    var grid = $("#@gridClinetId").gridmvc();
    window.gridMvcControl = grid;//when using only 1 grid on the page
    @if (!string.IsNullOrEmpty(Model.Id))
    {
     <text>if (typeof (window.pageGrids) == 'undefined') window.pageGrids = {};  window.pageGrids['@Model.Id'] = grid;</text>
    }
}
</script>

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
@RenderBody()

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>

推荐答案

尝试以下操作:

$('.grid-table tr td:nth-child(4)').filter(function() {
    return $(this).text().indexOf('ERROR') != -1;
}).closest('tr').css("backgroundColor", "red");

这篇关于根据条件更改GridMvc列的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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