From e1e82ac31a60ad45d4ff787899e95ebca2dd9e80 Mon Sep 17 00:00:00 2001 From: shapirom Date: Thu, 20 Aug 2015 10:22:10 -0400 Subject: [PATCH 1/2] Honor DisplayFormatAttribute when using ToDictionary transform --- .../Reflection/DataTablesTypeInfo.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Mvc.JQuery.Datatables/Reflection/DataTablesTypeInfo.cs b/Mvc.JQuery.Datatables/Reflection/DataTablesTypeInfo.cs index fe2e7a3..10f5a3c 100644 --- a/Mvc.JQuery.Datatables/Reflection/DataTablesTypeInfo.cs +++ b/Mvc.JQuery.Datatables/Reflection/DataTablesTypeInfo.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using Mvc.JQuery.Datatables.Models; +using System.ComponentModel.DataAnnotations; namespace Mvc.JQuery.Datatables.Reflection { @@ -41,11 +42,11 @@ public static Dictionary ToDictionary(T row) var dictionary = new Dictionary(); foreach (var pi in Properties) { - dictionary[pi.PropertyInfo.Name] = pi.PropertyInfo.GetValue(row, null); + dictionary[pi.PropertyInfo.Name] = getValue(row, pi); } if (RowID != null) { - dictionary["DT_RowID"] = RowID.PropertyInfo.GetValue(row, null); + dictionary["DT_RowID"] = getValue(row, RowID); if (!RowID.Attributes.OfType().First().EmitAsColumnName) { dictionary.Remove(RowID.PropertyInfo.Name); @@ -68,7 +69,7 @@ public static Func> ToDictionary(ResponseOptions(); + if (displayFormat != null) + { + value = string.Format(displayFormat.DataFormatString, value); + } + } + return value; + } } } \ No newline at end of file From 2c344f02237d3cdb7b2cc43bcb04457859152177 Mon Sep 17 00:00:00 2001 From: shapirom Date: Thu, 20 Aug 2015 10:25:48 -0400 Subject: [PATCH 2/2] Add support for filtering IEnumerable columns - IEnumerable matches query if IEnumerable.Any(x=>x matches query) --- Mvc.JQuery.Datatables/DataTablesFiltering.cs | 36 +++++++++++++------ Mvc.JQuery.Datatables/DataTablesResult.cs | 8 +++-- .../Processing/TypeFilters.cs | 7 ++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Mvc.JQuery.Datatables/DataTablesFiltering.cs b/Mvc.JQuery.Datatables/DataTablesFiltering.cs index 36af06b..9a2e235 100644 --- a/Mvc.JQuery.Datatables/DataTablesFiltering.cs +++ b/Mvc.JQuery.Datatables/DataTablesFiltering.cs @@ -92,7 +92,7 @@ public delegate string ReturnedFilteredQueryForType( Guard(IsDateTimeOffsetType, TypeFilters.DateTimeOffsetFilter), Guard(IsNumericType, TypeFilters.NumericFilter), Guard(IsEnumType, TypeFilters.EnumFilter), - Guard(arg => arg.Type == typeof (string), TypeFilters.StringFilter), + Guard(IsStringType, TypeFilters.StringFilter), }; @@ -118,15 +118,23 @@ public static void RegisterFilter(GuardedFilter filter) private static string GetFilterClause(string query, DataTablesPropertyInfo column, List parametersForLinqQuery) { + var isCollection = column.Type.IsGenericType && column.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>); Func filterClause = (queryPart) => Filters.Select( - f => f(queryPart, column.PropertyInfo.Name, column, parametersForLinqQuery)) - .FirstOrDefault(filterPart => filterPart != null) ?? ""; + f => f(queryPart, isCollection ? "it" : column.PropertyInfo.Name, column, parametersForLinqQuery)) + .FirstOrDefault(filterPart => filterPart != null) ?? ""; var queryParts = query.Split('|').Select(filterClause).Where(fc => fc != "").ToArray(); if (queryParts.Any()) { - return "(" + string.Join(") OR (", queryParts) + ")"; + if (isCollection) + { + return String.Format("{0}.Any(({1}))", column.PropertyInfo.Name, string.Join(") OR (", queryParts)); + } + else + { + return "(" + string.Join(") OR (", queryParts) + ")"; + } } return null; } @@ -134,8 +142,8 @@ private static string GetFilterClause(string query, DataTablesPropertyInfo colum public static bool IsNumericType(DataTablesPropertyInfo propertyInfo) { - var type = propertyInfo.Type; - return IsNumericType(type); + return IsNumericType(propertyInfo.Type) || + (propertyInfo.Type.IsGenericType && propertyInfo.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>) && IsNumericType(propertyInfo.Type.GetGenericArguments()[0])); } private static bool IsNumericType(Type type) @@ -171,20 +179,28 @@ private static bool IsNumericType(Type type) public static bool IsEnumType(DataTablesPropertyInfo propertyInfo) { - return propertyInfo.Type.IsEnum; + return propertyInfo.Type.IsEnum || (propertyInfo.Type.IsGenericType && propertyInfo.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>) && propertyInfo.Type.GetGenericArguments()[0].IsEnum); } public static bool IsBoolType(DataTablesPropertyInfo propertyInfo) { - return propertyInfo.Type == typeof(bool) || propertyInfo.Type == typeof(bool?); + return propertyInfo.Type == typeof(bool) || propertyInfo.Type == typeof(bool?) || + propertyInfo.Type == typeof(IEnumerable) || propertyInfo.Type == typeof(IEnumerable); } public static bool IsDateTimeType(DataTablesPropertyInfo propertyInfo) { - return propertyInfo.Type == typeof(DateTime) || propertyInfo.Type == typeof(DateTime?); + return propertyInfo.Type == typeof(DateTime) || propertyInfo.Type == typeof(DateTime?) || + propertyInfo.Type == typeof(IEnumerable) || propertyInfo.Type == typeof(IEnumerable); } public static bool IsDateTimeOffsetType(DataTablesPropertyInfo propertyInfo) { - return propertyInfo.Type == typeof(DateTimeOffset) || propertyInfo.Type == typeof(DateTimeOffset?); + return propertyInfo.Type == typeof(DateTimeOffset) || propertyInfo.Type == typeof(DateTimeOffset?) || + propertyInfo.Type == typeof(IEnumerable) || propertyInfo.Type == typeof(IEnumerable); + } + + public static bool IsStringType(DataTablesPropertyInfo propertyInfo) + { + return propertyInfo.Type == typeof(string) || propertyInfo.Type == typeof(IEnumerable); } } diff --git a/Mvc.JQuery.Datatables/DataTablesResult.cs b/Mvc.JQuery.Datatables/DataTablesResult.cs index 7aef639..497908e 100644 --- a/Mvc.JQuery.Datatables/DataTablesResult.cs +++ b/Mvc.JQuery.Datatables/DataTablesResult.cs @@ -24,8 +24,12 @@ public static DataTablesResult Create(IQueryable() { ArrayOutputType = arrayOutput }); } + public static DataTablesResult Create(IQueryable q, DataTablesParam dataTableParam) + { + return Create(q, dataTableParam, new ResponseOptions()); + } public static DataTablesResult Create(IQueryable q, DataTablesParam dataTableParam, - ArrayOutputType? arrayOutput = null) + ArrayOutputType? arrayOutput) { return Create(q, dataTableParam, new ResponseOptions() { ArrayOutputType = arrayOutput }); } @@ -37,7 +41,7 @@ public static DataTablesResult Create(IQueryable q, D /// //a transform for custom column rendering e.g. to do a custom date row => new { CreatedDate = row.CreatedDate.ToString("dd MM yy") } /// public static DataTablesResult Create(IQueryable q, DataTablesParam dataTableParam, - Func transform, ResponseOptions responseOptions = null) + Func transform, ResponseOptions responseOptions) { var result = new DataTablesResult(q, dataTableParam); diff --git a/Mvc.JQuery.Datatables/Processing/TypeFilters.cs b/Mvc.JQuery.Datatables/Processing/TypeFilters.cs index 2908fc9..0f48081 100644 --- a/Mvc.JQuery.Datatables/Processing/TypeFilters.cs +++ b/Mvc.JQuery.Datatables/Processing/TypeFilters.cs @@ -128,6 +128,13 @@ public static string DateTimeOffsetFilter(string query, string columnname, DataT } } + internal static string StringListFilter(string query, string columnName, DataTablesPropertyInfo columnType, List parametersForLinqQuery) + { + var filterString = $"@{parametersForLinqQuery.Count}"; + parametersForLinqQuery.Add(query); + return filterString; + } + public static string DateTimeFilter(string query, string columnname, DataTablesPropertyInfo propertyInfo, List parametersForLinqQuery) { if (query == "~") return string.Empty;