Skip to content

Commit 2e34e70

Browse files
committed
Merge pull request #122 from draza67/Enum-Localization
Enum localization
2 parents b0cf3a4 + 3935968 commit 2e34e70

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public TTarget Select(params string[] options)
134134
{
135135
_colDef.Filter.type = "select";
136136
_colDef.Filter.values = options.Cast<object>().ToArray();
137+
if (_colDef.Type.IsEnum)
138+
{
139+
_colDef.Filter.values = _colDef.Type.EnumValLabPairs();
140+
}
137141
return _target;
138142
}
139143
public TTarget NumberRange()
@@ -158,6 +162,10 @@ public TTarget CheckBoxes(params string[] options)
158162
{
159163
_colDef.Filter.type = "checkbox";
160164
_colDef.Filter.values = options.Cast<object>().ToArray();
165+
if (_colDef.Type.IsEnum)
166+
{
167+
_colDef.Filter.values = _colDef.Type.EnumValLabPairs();
168+
}
161169
return _target;
162170
}
163171

@@ -294,4 +302,4 @@ private static IDictionary<string, object> ConvertObjectToDictionary(object obj)
294302
return new Dictionary<string, object>(new RouteValueDictionary(obj));
295303
}
296304
}
297-
}
305+
}

Mvc.JQuery.Datatables/FilterDef.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ private void SetDefaultValuesAccordingToColumnType(Type t)
4242
else if (t.IsEnum)
4343
{
4444
type = "checkbox";
45-
values = Enum.GetNames(t).Cast<object>().ToArray();
45+
//values = Enum.GetNames(t).Cast<object>().ToArray();
46+
values = t.EnumValLabPairs();
4647
}
4748
else
4849
{
4950
type = "text";
5051
}
5152
}
5253
}
53-
}
54+
}

Mvc.JQuery.Datatables/ResourceHelper.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Text;
6+
using System.ComponentModel.DataAnnotations;
67

78
namespace Mvc.JQuery.Datatables
89
{
@@ -23,4 +24,55 @@ public static T GetResourceLookup<T>(Type resourceType, string resourceName)
2324
return default(T);
2425
}
2526
}
27+
28+
/// <summary>
29+
/// Group of helpers that gets Display attributes values from Enum members
30+
/// DražaM
31+
/// </summary>
32+
public static class EnumHelper
33+
{
34+
public static string DisplayName(this Enum value)
35+
{
36+
Type enumType = value.GetType();
37+
var enumValue = Enum.GetName(enumType, value);
38+
MemberInfo member = enumType.GetMember(enumValue)[0];
39+
40+
var attrs = member.GetCustomAttributes(typeof(DisplayAttribute), false);
41+
var outString = member.Name;
42+
if (attrs.Length > 0)
43+
{
44+
45+
if (((DisplayAttribute)attrs[0]).ResourceType != null)
46+
{
47+
outString = ((DisplayAttribute)attrs[0]).GetName();
48+
}
49+
else
50+
{
51+
outString = ((DisplayAttribute)attrs[0]).Name;
52+
}
53+
}
54+
return outString;
55+
}
56+
57+
public static List<string> AllDisplayNames(this Type tip)
58+
{
59+
List<string> exitList = new List<string>();
60+
foreach (string r in Enum.GetNames(tip))
61+
{
62+
exitList.Add(((Enum)Enum.Parse(tip, r)).DisplayName());
63+
};
64+
return exitList;
65+
}
66+
67+
public static object[] EnumValLabPairs(this Type type)
68+
{
69+
var vals = Enum.GetNames(type).Cast<object>().ToArray();
70+
var lbls = type.AllDisplayNames().Cast<object>().ToArray();
71+
var result = new List<object>();
72+
73+
for (var x = 0; x <= vals.Length - 1; x++) { result.Add(new { value = vals[x], label = lbls[x] }); }
74+
75+
return result.ToArray<object>();
76+
}
77+
}
2678
}

0 commit comments

Comments
 (0)