Skip to content

Commit 6467111

Browse files
committed
=== Response Options ===
Added ResponseOptions parameters to DataTablesResult method calls Changed arrayOutput method signatures to wrap arrayOutput into ResponseOptions === DT_RowID === Added DT_RowID to ResponseOptions, allows selection of DT_RowID column in each DataTablesResult.Create call - implicitly sets ResponseOptions.ArrayOutputType to ArrayOfObjects. Added DataTablesRowIdAttribute to set DT_RowID at the class level; also allows exclusion of the attributed property from being emitted under its normal column name ResponseOptions.DT_RowID -overrides- DataTablesRowIdAttribute If a class property has DataTablesRowIdAttribute and DataTablesExcludeAttribute, then DataTablesRowIdAttribute will have no effect DataTablesRowIdAttribute and DT_RowID only affect conversion of a row to a Dictionary (via DataTablesTypeInfo.ToDictionary)
1 parent 3b8ae35 commit 6467111

File tree

6 files changed

+139
-7
lines changed

6 files changed

+139
-7
lines changed

Mvc.JQuery.Datatables/DataTablesResult.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,56 @@ public abstract class DataTablesResult : ActionResult
2020
/// <returns></returns>
2121
public static DataTablesResult<TSource> Create<TSource, TTransform>(IQueryable<TSource> q, DataTablesParam dataTableParam,
2222
Func<TSource, TTransform> transform, ArrayOutputType? arrayOutput = null)
23+
{
24+
return Create(q, dataTableParam, transform, new ResponseOptions<TSource>() { ArrayOutputType = arrayOutput });
25+
}
26+
27+
public static DataTablesResult<TSource> Create<TSource>(IQueryable<TSource> q, DataTablesParam dataTableParam,
28+
ArrayOutputType? arrayOutput = null)
29+
{
30+
return Create(q, dataTableParam, new ResponseOptions<TSource>() { ArrayOutputType = arrayOutput });
31+
}
32+
33+
/// <typeparam name="TSource"></typeparam>
34+
/// <typeparam name="TTransform"></typeparam>
35+
/// <param name="q">A queryable for the data. The properties of this can be marked up with [DataTablesAttribute] to control sorting/searchability/visibility</param>
36+
/// <param name="dataTableParam"></param>
37+
/// <param name="transform">//a transform for custom column rendering e.g. to do a custom date row => new { CreatedDate = row.CreatedDate.ToString("dd MM yy") } </param>
38+
/// <returns></returns>
39+
public static DataTablesResult<TSource> Create<TSource, TTransform>(IQueryable<TSource> q, DataTablesParam dataTableParam,
40+
Func<TSource, TTransform> transform, ResponseOptions<TSource> responseOptions = null)
2341
{
2442
var result = new DataTablesResult<TSource>(q, dataTableParam);
2543

2644
result.Data = result.Data
2745
.Transform<TSource, Dictionary<string, object>>(row => TransformTypeInfo.MergeTransformValuesIntoDictionary(transform, row))
2846
.Transform<Dictionary<string, object>, Dictionary<string, object>>(StringTransformers.StringifyValues);
2947

30-
result.Data = ApplyOutputRules(result.Data, arrayOutput);
48+
result.Data = ApplyOutputRules(result.Data, responseOptions);
3149

3250
return result;
3351
}
3452

3553
public static DataTablesResult<TSource> Create<TSource>(IQueryable<TSource> q, DataTablesParam dataTableParam,
36-
ArrayOutputType? arrayOutput = null)
54+
ResponseOptions<TSource> responseOptions = null)
3755
{
3856
var result = new DataTablesResult<TSource>(q, dataTableParam);
3957

58+
var dictionaryTransform = DataTablesTypeInfo<TSource>.ToDictionary(responseOptions);
4059
result.Data = result.Data
41-
.Transform<TSource, Dictionary<string, object>>(DataTablesTypeInfo<TSource>.ToDictionary)
60+
.Transform<TSource, Dictionary<string, object>>(dictionaryTransform)
4261
.Transform<Dictionary<string, object>, Dictionary<string, object>>(StringTransformers.StringifyValues);
4362

44-
result.Data = ApplyOutputRules(result.Data, arrayOutput);
63+
result.Data = ApplyOutputRules(result.Data, responseOptions);
4564

4665
return result;
4766
}
4867

49-
private static DataTablesData ApplyOutputRules(DataTablesData sourceData, ArrayOutputType? arrayOutput = null)
68+
private static DataTablesData ApplyOutputRules<TSource>(DataTablesData sourceData, ResponseOptions<TSource> responseOptions)
5069
{
5170
DataTablesData outputData = sourceData;
5271

53-
switch (arrayOutput)
72+
switch (responseOptions.ArrayOutputType)
5473
{
5574
case ArrayOutputType.ArrayOfObjects:
5675
// Nothing is needed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Mvc.JQuery.Datatables.Models;
8+
9+
namespace Mvc.JQuery.Datatables
10+
{
11+
public class DataTablesRowIdAttribute : DataTablesAttributeBase
12+
{
13+
public bool EmitAsColumnName { get; set; }
14+
15+
public override void ApplyTo(ColDef colDef, PropertyInfo pi)
16+
{
17+
// This attribute does not affect rendering
18+
}
19+
20+
public DataTablesRowIdAttribute()
21+
{
22+
EmitAsColumnName = true;
23+
}
24+
}
25+
}

Mvc.JQuery.Datatables/Models/DataTablesData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class DataTablesData
1111
public object[] aaData { get; set; }
1212

1313

14-
public DataTablesData Transform<TData, TTransform>(Func<TData, TTransform> transformRow)
14+
public DataTablesData Transform<TData, TTransform>(Func<TData, TTransform> transformRow, ResponseOptions responseOptions = null)
1515
{
1616
var data = new DataTablesData
1717
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Mvc.JQuery.Datatables.Models
8+
{
9+
public class ResponseOptions
10+
{
11+
public virtual ArrayOutputType? ArrayOutputType { get; set; }
12+
13+
public static ResponseOptions<TSource> For<TSource>(IQueryable<TSource> data,
14+
Action<ResponseOptions<TSource>> setOptions) where TSource : class
15+
{
16+
var responseOptions = new ResponseOptions<TSource>();
17+
setOptions(responseOptions);
18+
return responseOptions;
19+
}
20+
}
21+
22+
public class ResponseOptions<TSource> : ResponseOptions
23+
{
24+
public Func<TSource, object> DT_RowID
25+
{
26+
get
27+
{
28+
return dt_rowid;
29+
}
30+
set
31+
{
32+
dt_rowid = value;
33+
if (value != null)
34+
{
35+
ArrayOutputType = Models.ArrayOutputType.ArrayOfObjects;
36+
}
37+
}
38+
}
39+
private Func<TSource, object> dt_rowid;
40+
41+
public override ArrayOutputType? ArrayOutputType
42+
{
43+
get { return base.ArrayOutputType; }
44+
set
45+
{
46+
if (DT_RowID != null && value != Models.ArrayOutputType.ArrayOfObjects)
47+
{
48+
throw new ArgumentOutOfRangeException("ArrayOutputType", "ArrayOutputType must be ArrayOfObjects when DT_RowID is set");
49+
}
50+
base.ArrayOutputType = value;
51+
}
52+
}
53+
}
54+
}

Mvc.JQuery.Datatables/Mvc.JQuery.Datatables.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="DataTablesExcludeAttribute.cs" />
8383
<Compile Include="DataTablesFiltering.cs" />
8484
<Compile Include="DataTablesFilterAttribute.cs" />
85+
<Compile Include="DataTablesRowIdAttribute.cs" />
8586
<Compile Include="LengthMenuVm.cs" />
8687
<Compile Include="Models\ArrayOutputType.cs" />
8788
<Compile Include="Models\ColDef.cs" />
@@ -105,6 +106,7 @@
105106
<Compile Include="DynamicLinq\Signature.cs" />
106107
<Compile Include="FilterDef.cs" />
107108
<Compile Include="Models\Language.cs" />
109+
<Compile Include="Models\ResponseOptions.cs" />
108110
<Compile Include="Reflection\DataTablesPropertyInfo.cs" />
109111
<Compile Include="Reflection\DataTablesTypeInfo.cs" />
110112
<Compile Include="Reflection\DataTablesTypeInfoHelper.cs" />

Mvc.JQuery.Datatables/Reflection/DataTablesTypeInfo.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Specialized;
55
using System.Linq;
66
using System.Reflection;
7+
using Mvc.JQuery.Datatables.Models;
78

89
namespace Mvc.JQuery.Datatables.Reflection
910
{
@@ -27,10 +28,12 @@ orderby attributes.OfType<DataTablesAttribute>().Select(a => a.Order as int?).Si
2728
static class DataTablesTypeInfo<T>
2829
{
2930
internal static DataTablesPropertyInfo[] Properties { get; private set; }
31+
internal static DataTablesPropertyInfo RowID { get; private set; }
3032

3133
static DataTablesTypeInfo()
3234
{
3335
Properties = DataTablesTypeInfo.Properties(typeof (T));
36+
RowID = Properties.SingleOrDefault(x => x.Attributes.Any(y => y is DataTablesRowIdAttribute));
3437
}
3538

3639
public static Dictionary<string, object> ToDictionary(T row)
@@ -40,9 +43,38 @@ public static Dictionary<string, object> ToDictionary(T row)
4043
{
4144
dictionary[pi.PropertyInfo.Name] = pi.PropertyInfo.GetValue(row, null);
4245
}
46+
if (RowID != null)
47+
{
48+
dictionary["DT_RowID"] = RowID.PropertyInfo.GetValue(row, null);
49+
if (!RowID.Attributes.OfType<DataTablesRowIdAttribute>().First().EmitAsColumnName)
50+
{
51+
dictionary.Remove(RowID.PropertyInfo.Name);
52+
}
53+
}
4354
return dictionary;
4455
}
4556

57+
public static Func<T, Dictionary<string, object>> ToDictionary(ResponseOptions<T> options = null)
58+
{
59+
if (options == null || options.DT_RowID == null)
60+
{
61+
return ToDictionary;
62+
}
63+
else
64+
{
65+
return row =>
66+
{
67+
var dictionary = new Dictionary<string, object>();
68+
dictionary["DT_RowID"] = options.DT_RowID(row);
69+
foreach (var pi in Properties)
70+
{
71+
dictionary[pi.PropertyInfo.Name] = pi.PropertyInfo.GetValue(row, null);
72+
}
73+
return dictionary;
74+
};
75+
}
76+
}
77+
4678
public static OrderedDictionary ToOrderedDictionary(T row)
4779
{
4880
var dictionary = new OrderedDictionary();

0 commit comments

Comments
 (0)