Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion GridMvc.Site/Models/Domain/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public Order()
public virtual ICollection<OrderDetail> 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
}
}
}
4 changes: 4 additions & 0 deletions GridMvc.Site/Views/Home/_OrdersGrid.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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()

5 changes: 5 additions & 0 deletions GridMvc.Site/Views/Home/_OrdersGridCode.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
</pre>
</div>
26 changes: 26 additions & 0 deletions GridMvc.Tests/Columns/ColumnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
11 changes: 11 additions & 0 deletions GridMvc.Tests/TestModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace GridMvc.Tests
{
Expand All @@ -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; }

Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions GridMvc/Columns/GridColumn.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<DisplayAttribute>().Any()
? attributes.OfType<DisplayAttribute>().First().Name
: value.ToString();
}
else
textValue = value.ToString();
}
Expand Down