Skip to content

Commit 40a8466

Browse files
committed
Made real coldefs class
1 parent ad4473a commit 40a8466

File tree

3 files changed

+73
-63
lines changed

3 files changed

+73
-63
lines changed

Mvc.JQuery.Datatables.Templates/Views/Shared/DataTable.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
<tr>
99
@foreach (var column in Model.Columns)
1010
{
11-
<th>@column.Item2</th>
11+
<th>@column.DisplayName</th>
1212
}
1313
</tr>
1414
@if (Model.ColumnFilter)
1515
{
1616
<tr>
1717
@foreach (var column in Model.Columns)
1818
{
19-
<th>@column.Item2</th>
19+
<th>@column.DisplayName</th>
2020
}
2121
</tr>
2222
}

Mvc.JQuery.Datatables/DataTableVm.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
namespace Mvc.JQuery.Datatables
66
{
7+
public class ColDef
8+
{
9+
public string Name { get; set; }
10+
public string DisplayName { get; set; }
11+
public Type Type { get; set; }
12+
13+
public static ColDef Create(string name, string p1, Type propertyType)
14+
{
15+
return new ColDef() {Name = name, DisplayName = p1, Type = propertyType};
16+
}
17+
}
718
public class DataTableVm
819
{
920
static DataTableVm()
@@ -14,7 +25,7 @@ static DataTableVm()
1425
public static string DefaultTableClass { get; set; }
1526
public string TableClass { get; set; }
1627

17-
public DataTableVm(string id, string ajaxUrl, IEnumerable<Tuple<string, string, Type>> columns)
28+
public DataTableVm(string id, string ajaxUrl, IEnumerable<ColDef> columns)
1829
{
1930
AjaxUrl = ajaxUrl;
2031
this.Id = id;
@@ -32,7 +43,7 @@ public DataTableVm(string id, string ajaxUrl, IEnumerable<Tuple<string, string,
3243

3344
public string AjaxUrl { get; private set; }
3445

35-
public IEnumerable<Tuple<string, string, Type>> Columns { get; private set; }
46+
public IEnumerable<ColDef> Columns { get; private set; }
3647

3748
public bool ColumnFilter { get; set; }
3849

@@ -42,7 +53,7 @@ public DataTableVm(string id, string ajaxUrl, IEnumerable<Tuple<string, string,
4253

4354
public string ColumnFiltersString
4455
{
45-
get { return string.Join(",", Columns.Select(c => GetFilterType(c.Item1, c.Item3))); }
56+
get { return string.Join(",", Columns.Select(c => GetFilterType(c.Name, c.Type))); }
4657
}
4758

4859
public string Dom
Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,69 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4-
using System.Linq;
4+
using System.Linq;
55
using System.Linq.Expressions;
66
using System.Reflection;
7-
using System.Text;
8-
using System.Web;
9-
using System.Web.Mvc;
10-
using System.Web.Mvc.Html;
11-
12-
namespace Mvc.JQuery.Datatables
13-
{
14-
public static class DataTablesHelper
15-
{
16-
17-
public static IHtmlString DataTableIncludes(this HtmlHelper helper, bool jqueryUi = false, bool filters = true, bool tableTools = true)
18-
{
19-
StringBuilder output = new StringBuilder();
20-
Action<string> addJs = s => output.AppendLine(@"<script src=""" + s + @""" type=""text/javascript""></script>");
21-
Action<string> addCss = s => output.AppendLine(@"<link type=""text/css"" href=""" + s + @""" rel=""stylesheet""/>");
22-
23-
addCss("/Content/DataTables/media/css/" + (jqueryUi ? ("jquery.dataTables_themeroller.css") : "jquery.dataTables.css"));
24-
addJs("/Content/DataTables/media/js/jquery.dataTables.js");
25-
if (filters) addJs("/Content/jquery.dataTables.columnFilter.js");
26-
if (tableTools)
27-
{
28-
addJs("/Content/DataTables/extras/TableTools/media/js/ZeroClipboard.js");
29-
addJs("/Content/DataTables/extras/TableTools/media/js/TableTools.js");
30-
addCss("/Content/DataTables/extras/TableTools/media/css/TableTools.css");
31-
}
32-
return helper.Raw(output.ToString());
33-
34-
}
35-
36-
public static DataTableVm DataTableVm<TController, TResult>(this HtmlHelper html, string id, Expression<Func<TController, DataTablesResult<TResult>>> exp, params Tuple<string, string, Type>[] columns)
37-
{
38-
if (columns == null || columns.Length == 0)
7+
using System.Text;
8+
using System.Web;
9+
using System.Web.Mvc;
10+
using System.Web.Mvc.Html;
11+
12+
namespace Mvc.JQuery.Datatables
13+
{
14+
public static class DataTablesHelper
15+
{
16+
17+
public static IHtmlString DataTableIncludes(this HtmlHelper helper, bool jqueryUi = false, bool filters = true, bool tableTools = true)
18+
{
19+
StringBuilder output = new StringBuilder();
20+
Action<string> addJs = s => output.AppendLine(@"<script src=""" + s + @""" type=""text/javascript""></script>");
21+
Action<string> addCss = s => output.AppendLine(@"<link type=""text/css"" href=""" + s + @""" rel=""stylesheet""/>");
22+
23+
addCss("/Content/DataTables/media/css/" + (jqueryUi ? ("jquery.dataTables_themeroller.css") : "jquery.dataTables.css"));
24+
addJs("/Content/DataTables/media/js/jquery.dataTables.js");
25+
if (filters) addJs("/Content/jquery.dataTables.columnFilter.js");
26+
if (tableTools)
27+
{
28+
addJs("/Content/DataTables/extras/TableTools/media/js/ZeroClipboard.js");
29+
addJs("/Content/DataTables/extras/TableTools/media/js/TableTools.js");
30+
addCss("/Content/DataTables/extras/TableTools/media/css/TableTools.css");
31+
}
32+
return helper.Raw(output.ToString());
33+
34+
}
35+
36+
public static DataTableVm DataTableVm<TController, TResult>(this HtmlHelper html, string id, Expression<Func<TController, DataTablesResult<TResult>>> exp, IEnumerable<ColDef> columns = null)
37+
{
38+
if (columns == null || !columns.Any())
3939
{
4040
var propInfos = typeof (TResult).GetProperties().Where(p => p.GetGetMethod() != null).ToList();
41-
var columnList = new List<Tuple<string, string, Type>>();
41+
var columnList = new List<ColDef>();
4242
foreach (var propertyInfo in propInfos)
4343
{
44-
var displayNamettribute = (DisplayNameAttribute)propertyInfo.GetCustomAttributes(typeof (DisplayNameAttribute), false).FirstOrDefault();
45-
if(displayNamettribute != null)
46-
{
47-
columnList.Add(Tuple.Create(propertyInfo.Name, displayNamettribute.DisplayName, propertyInfo.PropertyType));
48-
}
49-
else
44+
var displayNameAttribute = (DisplayNameAttribute) propertyInfo.GetCustomAttributes(typeof (DisplayNameAttribute), false).FirstOrDefault();
45+
var displayName = displayNameAttribute == null ? propertyInfo.Name : displayNameAttribute.DisplayName;
46+
columnList.Add(new ColDef()
5047
{
51-
columnList.Add(Tuple.Create(propertyInfo.Name, propertyInfo.Name, propertyInfo.PropertyType));
52-
}
48+
Name = propertyInfo.Name,
49+
DisplayName = displayName,
50+
Type = propertyInfo.PropertyType
51+
});
5352
}
5453
columns = columnList.ToArray();
55-
}
56-
57-
var mi = exp.MethodInfo();
58-
var controllerName = typeof(TController).Name;
59-
controllerName = controllerName.Substring(0, controllerName.LastIndexOf("Controller"));
60-
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
61-
var ajaxUrl = urlHelper.Action(mi.Name, controllerName);
62-
return new DataTableVm(id, ajaxUrl, columns);
63-
}
64-
65-
public static DataTableVm DataTableVm(this HtmlHelper html, string id, string ajaxUrl, params string[] columns)
66-
{
67-
return new DataTableVm(id, ajaxUrl, columns.Select(c => Tuple.Create(c, (string)null, typeof(string))));
68-
}
69-
}
54+
}
55+
56+
var mi = exp.MethodInfo();
57+
var controllerName = typeof(TController).Name;
58+
controllerName = controllerName.Substring(0, controllerName.LastIndexOf("Controller"));
59+
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
60+
var ajaxUrl = urlHelper.Action(mi.Name, controllerName);
61+
return new DataTableVm(id, ajaxUrl, columns);
62+
}
63+
64+
public static DataTableVm DataTableVm(this HtmlHelper html, string id, string ajaxUrl, params string[] columns)
65+
{
66+
return new DataTableVm(id, ajaxUrl, columns.Select(c => ColDef.Create(c, (string)null, typeof(string))));
67+
}
68+
}
7069
}

0 commit comments

Comments
 (0)