Skip to content

Commit cb0b676

Browse files
committed
Added enum filter helper override to pass list items
Made default selection work with enum values
1 parent 1cab10b commit cb0b676

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("1.0.61.0")]
19-
[assembly: AssemblyFileVersion("1.0.61.0")]
18+
[assembly: AssemblyVersion("1.0.62.0")]
19+
[assembly: AssemblyFileVersion("1.0.62.0")]

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@
115115
</ItemGroup>
116116
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
117117
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
118-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
119-
<PropertyGroup>
120-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
121-
</PropertyGroup>
122-
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
123-
</Target>
124118
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
125119
Other similar extension points exist, see Microsoft.Common.targets.
126120
<Target Name="BeforeBuild">

Griddly.Mvc/GriddlyFilter.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void SetSelectedItems(object value)
117117

118118
if (defaultValues == null || value is string)
119119
{
120-
string valueString = value.ToString();
120+
string valueString = GetValueString(value);
121121

122122
foreach (SelectListItem item in SelectableItems)
123123
item.Selected = item.Value == valueString;
@@ -128,7 +128,7 @@ public void SetSelectedItems(object value)
128128
{
129129
if (valueObject != null)
130130
{
131-
string valueString = valueObject.ToString();
131+
string valueString = GetValueString(valueObject);
132132

133133
foreach (SelectListItem item in SelectableItems.Where(x => x.Value == valueString))
134134
item.Selected = true;
@@ -148,7 +148,16 @@ public void SetSelectedItems(object value)
148148
Items[i].Selected = (i == 0);
149149
}
150150
}
151+
}
152+
153+
string GetValueString(object value)
154+
{
155+
Type valueType = Nullable.GetUnderlyingType(value.GetType()) ?? value.GetType();
151156

157+
if (valueType.IsEnum)
158+
return Convert.ChangeType(value, Enum.GetUnderlyingType(valueType)).ToString();
159+
else
160+
return value.ToString();
152161
}
153162
}
154163

Griddly.Mvc/GriddlyFilterExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ public static GriddlyFilterList FilterEnum<T>(this GriddlyColumn column, bool is
114114
return column.FilterList(Extensions.ToSelectListItems<T>().OrderBy(x => x.Text), isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, caption);
115115
}
116116

117+
public static GriddlyFilterList FilterEnum<T>(this GriddlyColumn column, IEnumerable<T> items, bool isMultiple = true, bool defaultSelectAll = true, string nullItemText = null, bool isNoneAll = true, string field = null, string caption = null)
118+
where T : struct
119+
{
120+
return column.FilterList(Extensions.ToSelectListItems(items).OrderBy(x => x.Text), isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, caption);
121+
}
122+
117123
public static GriddlyFilterList FilterBool(this GriddlyColumn column, string trueLabel = "Yes", string falseLabel = "No", string nullItemText = null, bool isMultiple = false, bool defaultSelectAll = false, bool isNoneAll = false, string field = null, string caption = null)
118124
{
119125
return column.FilterList(BuildBoolItems(trueLabel, falseLabel), isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, caption);

Griddly.Mvc/InternalExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ internal static IEnumerable<SelectListItem> ToSelectListItems<T>()
109109
}
110110
}
111111

112+
internal static IEnumerable<SelectListItem> ToSelectListItems<T>(IEnumerable<T> items)
113+
where T : struct
114+
{
115+
Type enumType = typeof(T);
116+
117+
if (!enumType.IsEnum)
118+
throw new InvalidOperationException("T must be an Enum.");
119+
120+
foreach (T item in items)
121+
{
122+
FieldInfo field = enumType.GetField(item.ToString());
123+
124+
yield return new SelectListItem()
125+
{
126+
Value = field.GetValue(null).ToString(),
127+
Text = GetEnumDescription(field)
128+
};
129+
}
130+
}
131+
112132
static string GetEnumDescription(FieldInfo fi)
113133
{
114134
if (fi == null)

0 commit comments

Comments
 (0)