diff --git a/GridMvc.Site/Models/Domain/Order.cs b/GridMvc.Site/Models/Domain/Order.cs index 75c486d..9aa63b7 100644 --- a/GridMvc.Site/Models/Domain/Order.cs +++ b/GridMvc.Site/Models/Domain/Order.cs @@ -67,6 +67,26 @@ public Order() public virtual ICollection OrderDetails { get; set; } //public virtual Shipper Shipper { get; set; } - } + [NotMappedColumn] + public StatusType Status + { + get + { + //Get random value without messing with database + Random r = new Random(); + return (StatusType)r.Next(0, 3); + } + } + public enum StatusType + { + None = 0, + [Display(Name = "Order prepared")] + Prepared = 1, + [Display(Name = "Order sent")] + Sent = 2, + [Display(Name = "Order received")] + Received = 3 + } + } } diff --git a/GridMvc.Site/Views/Home/_OrdersGrid.cshtml b/GridMvc.Site/Views/Home/_OrdersGrid.cshtml index 7ff104f..b8182ec 100644 --- a/GridMvc.Site/Views/Home/_OrdersGrid.cshtml +++ b/GridMvc.Site/Views/Home/_OrdersGrid.cshtml @@ -52,6 +52,10 @@ .SetWidth(70) .Css("hidden-xs") //hide on phones .RenderValueAs(o => CustomRenderingOfColumn(o)); + /* Adding enum column: */ + columns.Add(o => o.Status) + .Titled("Status") + .SetWidth(70); }).SetRowCssClasses(item => item.Customer.IsVip ? "success" : string.Empty).WithPaging(15).Sortable().Filterable().WithMultipleFilters() diff --git a/GridMvc.Site/Views/Home/_OrdersGridCode.cshtml b/GridMvc.Site/Views/Home/_OrdersGridCode.cshtml index 55d4f68..2440202 100644 --- a/GridMvc.Site/Views/Home/_OrdersGridCode.cshtml +++ b/GridMvc.Site/Views/Home/_OrdersGridCode.cshtml @@ -39,6 +39,11 @@ .SetWidth(150) .RenderValueAs(o => CustomRenderingOfColumn(o)); + /* Adding enum column: */ + columns.Add(o => o.Status) + .Titled("Status") + .SetWidth(70); + }).SetRowCssClasses(item => item.Customer.IsVip ? "warning" : string.Empty).WithPaging(15).Sortable().Filterable().WithMultipleFilters() \ No newline at end of file diff --git a/GridMvc.Tests/Columns/ColumnTests.cs b/GridMvc.Tests/Columns/ColumnTests.cs index ddb1835..555eebc 100644 --- a/GridMvc.Tests/Columns/ColumnTests.cs +++ b/GridMvc.Tests/Columns/ColumnTests.cs @@ -62,6 +62,32 @@ public void TestRenderingEmptyValueIfNullReferenceOccurs() Assert.AreEqual(cell.Value, string.Empty); } + [TestMethod] + public void TestRenderingCorrectEnumValueIfDisplayAttributeExists() + { + var addedColumn = _columns.Add(x => x.TestEnum); + + var cell = addedColumn.GetCell(new TestModel + { + TestEnum = TestModel.TestEnumType.A + }); + + Assert.AreEqual(cell.Value, "TestEnumValueA"); + } + + [TestMethod] + public void TestRenderingCorrectEnumValueIfDisplayAttributeDoesNotExists() + { + var addedColumn = _columns.Add(x => x.TestEnum); + + var cell = addedColumn.GetCell(new TestModel + { + TestEnum = TestModel.TestEnumType.None + }); + + Assert.AreEqual(cell.Value, "None"); + } + [TestMethod] public void TestColumnsRetriveByNameWithCustomName() { diff --git a/GridMvc.Tests/TestModel.cs b/GridMvc.Tests/TestModel.cs index 4976ffd..1ffbb56 100644 --- a/GridMvc.Tests/TestModel.cs +++ b/GridMvc.Tests/TestModel.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel.DataAnnotations; namespace GridMvc.Tests { @@ -8,6 +9,7 @@ public class TestModel public string Title { get; set; } public DateTime Created { get; set; } public TestModelChild Child { get; set; } + public TestEnumType TestEnum { get; set; } public TestModelChild[] List { get; set; } @@ -32,6 +34,15 @@ public override bool Equals(object obj) && compareObject.UInt32Field == UInt32Field && compareObject.UInt64Field == UInt64Field; } + + public enum TestEnumType + { + None = 0, + [Display(Name = "TestEnumValueA")] + A = 1, + [Display(Name = "TestEnumValueB")] + B = 2 + } } public class TestModelChild diff --git a/GridMvc/Columns/GridColumn.cs b/GridMvc/Columns/GridColumn.cs index cf18ae2..bd3c513 100644 --- a/GridMvc/Columns/GridColumn.cs +++ b/GridMvc/Columns/GridColumn.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Linq.Expressions; using GridMvc.Filtering; using GridMvc.Sorting; @@ -194,6 +196,17 @@ public override IGridCell GetValue(T instance) textValue = string.Empty; else if (!string.IsNullOrEmpty(ValuePattern)) textValue = string.Format(ValuePattern, value); + else if (typeof (TDataType).IsEnum) + { + var attributes = typeof (TDataType).GetMember(value.ToString()) + .First() + .GetCustomAttributes(false); + + //If DisplayAttribute is not present then fallback to raw property name + textValue = attributes.OfType().Any() + ? attributes.OfType().First().Name + : value.ToString(); + } else textValue = value.ToString(); }