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
25 changes: 19 additions & 6 deletions Mvc.JQuery.Datatables/TypeFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Mvc.JQuery.Datatables
{
static class TypeFilters
{
private static readonly Func<string, Type, object> ParseValue =
private static readonly Func<string, Type, object> ParseValue =
(input, t) => t.IsEnum ? Enum.Parse(t, input) : Convert.ChangeType(input, t);

internal static string FilterMethod(string q, List<object> parametersForLinqQuery, Type type)
{

Expand Down Expand Up @@ -51,7 +51,7 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
var clause = null as string;
try
{
parametersForLinqQuery.Add(Convert.ChangeType(parts[0], colInfo.Type));
parametersForLinqQuery.Add(ChangeType(colInfo, parts[0]));
clause = string.Format("{0} >= @{1}", columnname, parametersForLinqQuery.Count - 1);
}
catch (FormatException)
Expand All @@ -60,7 +60,7 @@ public static string NumericFilter(string query, string columnname, ColInfo colI

try
{
parametersForLinqQuery.Add(Convert.ChangeType(parts[1], colInfo.Type));
parametersForLinqQuery.Add(ChangeType(colInfo, parts[1]));
if (clause != null) clause += " and ";
clause += string.Format("{0} <= @{1}", columnname, parametersForLinqQuery.Count - 1);
}
Expand All @@ -74,7 +74,7 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
{
try
{
parametersForLinqQuery.Add(Convert.ChangeType(query, colInfo.Type));
parametersForLinqQuery.Add(ChangeType(colInfo, query));
return string.Format("{0} == @{1}", columnname, parametersForLinqQuery.Count - 1);
}
catch (FormatException)
Expand All @@ -84,6 +84,19 @@ public static string NumericFilter(string query, string columnname, ColInfo colI
}
}

private static object ChangeType(ColInfo colInfo, string query)
{
if (colInfo.Type.IsGenericType && colInfo.Type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type u = Nullable.GetUnderlyingType(colInfo.Type);
return Convert.ChangeType(query, u);
}
else
{
return Convert.ChangeType(query, colInfo.Type);
}
}

public static string DateTimeOffsetFilter(string query, string columnname, ColInfo colInfo, List<object> parametersForLinqQuery)
{
if (query == "~") return string.Empty;
Expand Down Expand Up @@ -188,7 +201,7 @@ public static string StringFilter(string q, string columnname, ColInfo columntyp

public static string EnumFilter(string q, string columnname, ColInfo colInfo, List<object> parametersForLinqQuery)
{

if (q.StartsWith("^")) q = q.Substring(1);
if (q.EndsWith("$")) q = q.Substring(0, q.Length - 1);
parametersForLinqQuery.Add(ParseValue(q, colInfo.Type));
Expand Down
1 change: 1 addition & 0 deletions Mvc.Jquery.DataTables.Tests/DummyPocos/SomeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class SomeModel
public double Scale { get; set; }
public DateTime When { get; set; }
public bool Discounted { get; set; }
public decimal? Cost { get; set; }
}
}
5 changes: 4 additions & 1 deletion Mvc.Jquery.DataTables.Tests/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public void Setup()
DisplayName = "Cheddar",
Id = 123,
Scale = 123.456d,
Discounted = true
Discounted = true,
Cost = 123
}
}.AsQueryable();

Expand All @@ -50,6 +51,8 @@ public void Setup()
[TestCase("^456$", typeof(int), false)] //exact query, isnt match
[TestCase("123", typeof(int), true)] //query, is match
[TestCase("456", typeof(int), false)] //query, isnt match
[TestCase("123", typeof(decimal?), true)] //query, is match
[TestCase("456", typeof(decimal?), false)] //query, isnt match
public void SearchQueryTests(string searchString, Type colType, bool returnsResult)
{
var col = columns.First(c => c.Item2.Type == colType).Item1;
Expand Down