Skip to content

Commit 6e77906

Browse files
jandicmcintyre321
authored andcommitted
Fix for an issue with data table filtering and allowing for user defined filters (mcintyre321#175)
* Update DataTablesFiltering.cs Fixed an issue when filtering on a data table throws an exception in cases there are no relevant columns to be filtered. Example: data table contains only numerical columns and a user types 'a letter'. Since the letter cannot be converted to a number, no where clause is generated and data.Where command was failing. * Allowing for custom filter definition Allowing for custom filter definitions by a user and fixing a bug in RegisterFilter method. * Fix for EnumFilter EnumFilter method was crashing when a value to search was not the exact enum value. This prevented any subsequent filters to be processed.
1 parent c4101d3 commit 6e77906

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Mvc.JQuery.DataTables
99
{
10-
internal class DataTablesFiltering
10+
public class DataTablesFiltering
1111
{
1212
public IQueryable<T> ApplyFiltersAndSort<T>(DataTablesParam dtParameters, IQueryable<T> data, DataTablesPropertyInfo[] columns)
1313
{
@@ -30,7 +30,11 @@ public IQueryable<T> ApplyFiltersAndSort<T>(DataTablesParam dtParameters, IQuery
3030
}
3131
}
3232
var values = parts.Where(p => p != null);
33-
data = data.Where(string.Join(" or ", values), parameters.ToArray());
33+
var filterClause = string.Join(" or ", values);
34+
if (string.IsNullOrWhiteSpace(filterClause) == false)
35+
{
36+
data = data.Where(filterClause, parameters.ToArray());
37+
}
3438
}
3539
for (int i = 0; i < dtParameters.sSearchValues.Count; i++)
3640
{
@@ -114,7 +118,7 @@ private static ReturnedFilteredQueryForType Guard(Func<DataTablesPropertyInfo, b
114118

115119
public static void RegisterFilter<T>(GuardedFilter filter)
116120
{
117-
Filters.Add(Guard(arg => arg is T, filter));
121+
Filters.Add(Guard(arg => arg.Type == typeof(T), filter));
118122
}
119123

120124
private static string GetFilterClause(string query, DataTablesPropertyInfo column, List<object> parametersForLinqQuery)

Mvc.JQuery.DataTables.Common/Processing/TypeFilters.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,17 @@ public static string StringFilter(string q, string columnname, DataTablesPropert
251251

252252
public static string EnumFilter(string q, string columnname, DataTablesPropertyInfo propertyInfo, List<object> parametersForLinqQuery)
253253
{
254-
255-
if (q.StartsWith("^")) q = q.Substring(1);
256-
if (q.EndsWith("$")) q = q.Substring(0, q.Length - 1);
257-
parametersForLinqQuery.Add(ParseValue(q, propertyInfo.Type));
258-
return columnname + " == @" + (parametersForLinqQuery.Count - 1);
254+
try
255+
{
256+
if (q.StartsWith("^")) q = q.Substring(1);
257+
if (q.EndsWith("$")) q = q.Substring(0, q.Length - 1);
258+
parametersForLinqQuery.Add(ParseValue(q, propertyInfo.Type));
259+
return columnname + " == @" + (parametersForLinqQuery.Count - 1);
260+
}
261+
catch (Exception)
262+
{
263+
return null;
264+
}
259265
}
260266
}
261267
}

0 commit comments

Comments
 (0)