Reputation: 479
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>
<!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>
Upvotes: 2
Views: 8461
Reputation: 420
In my case, just add the column you want conditional color:
columns.Add(p => p.DifferenceQty).Encoded(false).Sanitized(false).RenderValueAs(p => RenderCustomCSS(p.LogicQty, p.RealQty)).Titled("Cantidad total").SetWidth(70).Sortable(true).Filterable(true);
and create a helper to set the custom render value (in the same view)
@helper RenderCustomCSS(double Value, double ValueCompared)
{
<p class="@(Value !=ValueCompared ? "gridDangerCell" : "")">@Value</p>
}
in the css:
.gridDangerCell
{
color: red;
}
Upvotes: 2
Reputation: 4992
There is a built-in server size method for this. SetRowCssClasses().
In Index.cshtml, use below
@Html.Grid(Model).SetRowCssClasses(
item => item.Response=="ERROR"? "cssClassRed" : string.Empty)
.Columns(columns =>
{
"cssClassRed" is injected to rows that satisfied the condition.
<tr class="grid-row cssClassRed">
Upvotes: 3
Reputation: 13997
Try something like the following:
$('.grid-table tr td:nth-child(4)').filter(function() {
return $(this).text().indexOf('ERROR') != -1;
}).closest('tr').css("backgroundColor", "red");
Upvotes: 2