Skip to content

Commit f8df900

Browse files
committed
Some rearrangement of things
1 parent 0e226fd commit f8df900

32 files changed

+1040
-918
lines changed

Mvc.JQuery.Datatables.Example/Controllers/HomeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Web;
77
using System.Web.Mvc;
8+
using Mvc.JQuery.Datatables.Models;
89
using Resources;
910

1011
namespace Mvc.JQuery.Datatables.Example.Controllers

Mvc.JQuery.Datatables/ColInfo.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Mvc.JQuery.Datatables
4+
{
5+
public class ColInfo
6+
{
7+
public string Name { get; set; }
8+
public Type Type { get; set; }
9+
10+
public ColInfo(string name, Type propertyType)
11+
{
12+
Name = name;
13+
Type = propertyType;
14+
15+
}
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections;
2+
using System.Linq;
3+
using System.Web.Script.Serialization;
4+
5+
namespace Mvc.JQuery.Datatables
6+
{
7+
public class ColumnFilterSettingsVm : Hashtable
8+
{
9+
private readonly DataTableConfigVm _vm;
10+
11+
public ColumnFilterSettingsVm(DataTableConfigVm vm)
12+
{
13+
_vm = vm;
14+
this["bUseColVis"] = true;
15+
this["sPlaceHolder"] = "head:after";
16+
}
17+
18+
public override string ToString()
19+
{
20+
var noColumnFilter = new FilterDef(null);
21+
this["aoColumns"] = _vm.Columns
22+
//.Where(c => c.Visible || c.Filter["sSelector"] != null)
23+
.Select(c => c.Searchable?c.Filter:noColumnFilter).ToArray();
24+
return new JavaScriptSerializer().Serialize(this);
25+
}
26+
}
27+
}

Mvc.JQuery.Datatables/DataTableConfigVm.cs

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
1+
using System.Collections.Generic;
42
using System.Linq;
53
using System.Net.Mime;
64
using System.Web.Routing;
@@ -11,52 +9,6 @@
119

1210
namespace Mvc.JQuery.Datatables
1311
{
14-
public class FilterDef : Hashtable
15-
{
16-
internal object[] values { set { this["values"] = value; } }
17-
internal string type { set { this["type"] = value; } }
18-
19-
20-
public FilterDef(Type t)
21-
{
22-
SetDefaultValuesAccordingToColumnType(t);
23-
}
24-
25-
private static List<Type> DateTypes = new List<Type> { typeof(DateTime), typeof(DateTime?), typeof(DateTimeOffset), typeof(DateTimeOffset?) };
26-
27-
28-
private void SetDefaultValuesAccordingToColumnType(Type t)
29-
{
30-
if (t==null)
31-
{
32-
type = "null";
33-
}
34-
else if (DateTypes.Contains(t))
35-
{
36-
type = "date-range";
37-
}
38-
else if (t == typeof (bool))
39-
{
40-
type = "select";
41-
values = new object[] {"True", "False"};
42-
}
43-
else if (t == typeof (bool?))
44-
{
45-
type = "select";
46-
values = new object[] {"True", "False", "null"};
47-
}
48-
else if (t.IsEnum)
49-
{
50-
type = "checkbox";
51-
values = Enum.GetNames(t).Cast<object>().ToArray();
52-
}
53-
else
54-
{
55-
type = "text";
56-
}
57-
}
58-
}
59-
6012
public class DataTableConfigVm
6113
{
6214
public bool HideHeaders { get; set; }
@@ -327,26 +279,4 @@ private static IDictionary<string, object> convertObjectToDictionary(object obj)
327279
return new Dictionary<string, object>(new RouteValueDictionary(obj));
328280
}
329281
}
330-
331-
public class ColumnFilterSettingsVm : Hashtable
332-
{
333-
private readonly DataTableConfigVm _vm;
334-
335-
public ColumnFilterSettingsVm(DataTableConfigVm vm)
336-
{
337-
_vm = vm;
338-
this["bUseColVis"] = true;
339-
this["sPlaceHolder"] = "head:after";
340-
}
341-
342-
public override string ToString()
343-
{
344-
var noColumnFilter = new FilterDef(null);
345-
this["aoColumns"] = _vm.Columns
346-
//.Where(c => c.Visible || c.Filter["sSelector"] != null)
347-
.Select(c => c.Searchable?c.Filter:noColumnFilter).ToArray();
348-
return new JavaScriptSerializer().Serialize(this);
349-
}
350-
}
351-
352282
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using Mvc.JQuery.Datatables.Models;
5+
using Mvc.JQuery.Datatables.Reflection;
6+
7+
namespace Mvc.JQuery.Datatables
8+
{
9+
public class DataTablesAttribute : DataTablesAttributeBase
10+
{
11+
public DataTablesAttribute()
12+
{
13+
this.Sortable = true;
14+
this.Searchable = true;
15+
this.Visible = true;
16+
this.Order = int.MaxValue;
17+
}
18+
19+
public bool Searchable { get; set; }
20+
public bool Sortable { get; set; }
21+
public int Order { get; set; }
22+
public string DisplayName { get; set; }
23+
public Type DisplayNameResourceType { get; set; }
24+
public SortDirection SortDirection { get; set; }
25+
public string MRenderFunction { get; set; }
26+
public String CssClass { get; set; }
27+
public String CssClassHeader { get; set; }
28+
29+
public bool Visible { get; set; }
30+
31+
public override void ApplyTo(ColDef colDef, PropertyInfo pi)
32+
{
33+
34+
colDef.DisplayName = this.ToDisplayName() ?? colDef.Name;
35+
colDef.Sortable = this.Sortable;
36+
colDef.Visible = this.Visible;
37+
colDef.Searchable = this.Searchable;
38+
colDef.SortDirection = this.SortDirection;
39+
colDef.MRenderFunction = this.MRenderFunction;
40+
colDef.CssClass = this.CssClass;
41+
colDef.CssClassHeader = this.CssClassHeader;
42+
colDef.CustomAttributes = pi.GetCustomAttributes().ToArray();
43+
}
44+
}
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Reflection;
3+
using Mvc.JQuery.Datatables.Models;
4+
5+
namespace Mvc.JQuery.Datatables
6+
{
7+
public abstract class DataTablesAttributeBase : Attribute
8+
{
9+
public abstract void ApplyTo(ColDef colDef, PropertyInfo pi);
10+
}
11+
}

Mvc.JQuery.Datatables/DataTablesHelper.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Web;
99
using System.Web.Mvc;
1010
using System.Web.Mvc.Html;
11+
using Mvc.JQuery.Datatables.Models;
12+
using Mvc.JQuery.Datatables.Reflection;
1113

1214
namespace Mvc.JQuery.Datatables
1315
{
@@ -40,24 +42,21 @@ public static DataTableConfigVm DataTableVm<TResult>(this HtmlHelper html, strin
4042
return DataTableVm(html, typeof (TResult), id, uri);
4143
}
4244

43-
public static ColDef[] ColDefs (Type t)
45+
public static ColDef[] ColDefs (this Type t)
4446
{
4547
var propInfos = DataTablesTypeInfo.Properties(t);
4648
var columnList = new List<ColDef>();
47-
foreach (var pi in propInfos)
49+
50+
foreach (var dtpi in propInfos)
4851
{
49-
columnList.Add(new ColDef(pi.Item1.PropertyType)
52+
53+
var colDef = new ColDef(dtpi.PropertyInfo.Name, dtpi.PropertyInfo.PropertyType);
54+
foreach (var att in dtpi.Attributes)
5055
{
51-
Name = pi.Item1.Name,
52-
DisplayName = pi.Item2.ToDisplayName() ?? pi.Item1.Name,
53-
Sortable = pi.Item2.Sortable,
54-
Visible = pi.Item2.Visible,
55-
Searchable = pi.Item2.Searchable,
56-
SortDirection = pi.Item2.SortDirection,
57-
MRenderFunction = pi.Item2.MRenderFunction,
58-
CssClass = pi.Item2.CssClass,
59-
CssClassHeader = pi.Item2.CssClassHeader
60-
});
56+
att.ApplyTo(colDef, dtpi.PropertyInfo);
57+
}
58+
59+
columnList.Add(colDef);
6160
}
6261
return columnList.ToArray();
6362
}

Mvc.JQuery.Datatables/DataTablesResult.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using System.Linq;
99
using System.Reflection;
1010
using System.Web.Mvc;
11+
using Mvc.JQuery.Datatables.Models;
12+
using Mvc.JQuery.Datatables.Reflection;
13+
using Mvc.JQuery.Datatables.Util;
1114

1215
namespace Mvc.JQuery.Datatables
1316
{
@@ -69,26 +72,6 @@ public static DataTablesResult<T> CreateResultUsingEnumerable<T>(IEnumerable<T>
6972

7073
}
7174

72-
public class TransformTypeInfo
73-
{
74-
public static Dictionary<string, object> MergeTransformValuesIntoDictionary<TInput, TTransform>(Func<TInput, TTransform> transformInput, TInput tInput)
75-
{
76-
//get the the properties from the input as a dictionary
77-
var dict = DataTablesTypeInfo<TInput>.ToDictionary(tInput);
78-
79-
//get the transform object
80-
var transform = transformInput(tInput);
81-
if (transform != null)
82-
{
83-
foreach (var propertyInfo in transform.GetType().GetProperties())
84-
{
85-
dict[propertyInfo.Name] = propertyInfo.GetValue(transform, null);
86-
}
87-
}
88-
return dict;
89-
}
90-
}
91-
9275

9376
public class DataTablesResult<TSource> : DataTablesResult
9477
{
@@ -124,7 +107,7 @@ DataTablesData GetResults(IQueryable<TSource> data, DataTablesParam param)
124107
var filters = new DataTablesFilter();
125108

126109
var outputProperties = DataTablesTypeInfo<TSource>.Properties;
127-
var searchColumns = outputProperties.Select(p => new ColInfo(p.Item1.Name, p.Item1.PropertyType)).ToArray();
110+
var searchColumns = outputProperties.Select(p => new ColInfo(p.PropertyInfo.Name, p.PropertyInfo.PropertyType)).ToArray();
128111

129112
var filteredData = filters.FilterPagingSortingSearch(param, data, searchColumns);
130113
var totalDisplayRecords = filteredData.Count();
@@ -146,17 +129,4 @@ DataTablesData GetResults(IQueryable<TSource> data, DataTablesParam param)
146129

147130

148131
}
149-
150-
public class ColInfo
151-
{
152-
public string Name { get; set; }
153-
public Type Type { get; set; }
154-
155-
public ColInfo(string name, Type propertyType)
156-
{
157-
Name = name;
158-
Type = propertyType;
159-
160-
}
161-
}
162132
}

0 commit comments

Comments
 (0)