Skip to content
Merged
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
10 changes: 9 additions & 1 deletion Mvc.JQuery.Datatables/DataTableConfigVm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public TTarget Select(params string[] options)
{
_colDef.Filter.type = "select";
_colDef.Filter.values = options.Cast<object>().ToArray();
if (_colDef.Type.IsEnum)
{
_colDef.Filter.values = _colDef.Type.EnumValLabPairs();
}
return _target;
}
public TTarget NumberRange()
Expand All @@ -158,6 +162,10 @@ public TTarget CheckBoxes(params string[] options)
{
_colDef.Filter.type = "checkbox";
_colDef.Filter.values = options.Cast<object>().ToArray();
if (_colDef.Type.IsEnum)
{
_colDef.Filter.values = _colDef.Type.EnumValLabPairs();
}
return _target;
}

Expand Down Expand Up @@ -294,4 +302,4 @@ private static IDictionary<string, object> ConvertObjectToDictionary(object obj)
return new Dictionary<string, object>(new RouteValueDictionary(obj));
}
}
}
}
5 changes: 3 additions & 2 deletions Mvc.JQuery.Datatables/FilterDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ private void SetDefaultValuesAccordingToColumnType(Type t)
else if (t.IsEnum)
{
type = "checkbox";
values = Enum.GetNames(t).Cast<object>().ToArray();
//values = Enum.GetNames(t).Cast<object>().ToArray();
values = t.EnumValLabPairs();
}
else
{
type = "text";
}
}
}
}
}
52 changes: 52 additions & 0 deletions Mvc.JQuery.Datatables/ResourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Mvc.JQuery.Datatables
{
Expand All @@ -23,4 +24,55 @@ public static T GetResourceLookup<T>(Type resourceType, string resourceName)
return default(T);
}
}

/// <summary>
/// Group of helpers that gets Display attributes values from Enum members
/// DražaM
/// </summary>
public static class EnumHelper
{
public static string DisplayName(this Enum value)
{
Type enumType = value.GetType();
var enumValue = Enum.GetName(enumType, value);
MemberInfo member = enumType.GetMember(enumValue)[0];

var attrs = member.GetCustomAttributes(typeof(DisplayAttribute), false);
var outString = member.Name;
if (attrs.Length > 0)
{

if (((DisplayAttribute)attrs[0]).ResourceType != null)
{
outString = ((DisplayAttribute)attrs[0]).GetName();
}
else
{
outString = ((DisplayAttribute)attrs[0]).Name;
}
}
return outString;
}

public static List<string> AllDisplayNames(this Type tip)
{
List<string> exitList = new List<string>();
foreach (string r in Enum.GetNames(tip))
{
exitList.Add(((Enum)Enum.Parse(tip, r)).DisplayName());
};
return exitList;
}

public static object[] EnumValLabPairs(this Type type)
{
var vals = Enum.GetNames(type).Cast<object>().ToArray();
var lbls = type.AllDisplayNames().Cast<object>().ToArray();
var result = new List<object>();

for (var x = 0; x <= vals.Length - 1; x++) { result.Add(new { value = vals[x], label = lbls[x] }); }

return result.ToArray<object>();
}
}
}