-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathDataTablesHelper.cs
55 lines (46 loc) · 1.92 KB
/
DataTablesHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using Mvc.JQuery.DataTables;
using Mvc.JQuery.DataTables.Models;
using Mvc.JQuery.DataTables.Reflection;
namespace Mvc.JQuery.DataTables
{
public static class DataTablesHelper
{
public static DataTableConfigVm DataTableVm<TController, TResult>(this HtmlHelper html, string id,
Expression<Func<TController, DataTablesResult<TResult>>> exp, IEnumerable<ColDef> columns = null)
{
if (columns == null || !columns.Any())
{
columns = typeof(TResult).ColDefs();
}
var mi = exp.MethodInfo();
var controllerName = typeof (TController).Name;
controllerName = controllerName.Substring(0, controllerName.LastIndexOf("Controller"));
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
var ajaxUrl = urlHelper.Action(mi.Name, controllerName);
return new DataTableConfigVm(id, ajaxUrl, columns);
}
public static DataTableConfigVm DataTableVm(this HtmlHelper html, Type t, string id, string uri)
{
return new DataTableConfigVm(id, uri.ToString(), t.ColDefs());
}
public static DataTableConfigVm DataTableVm<T>(string id, string uri)
{
return new DataTableConfigVm(id, uri.ToString(), typeof(T).ColDefs());
}
public static DataTableConfigVm DataTableVm<TResult>(this HtmlHelper html, string id, string uri)
{
return DataTableVm(html, typeof (TResult), id, uri);
}
public static DataTableConfigVm DataTableVm(this HtmlHelper html, string id, string ajaxUrl, params string[] columns)
{
return new DataTableConfigVm(id, ajaxUrl, columns.Select(c => new ColDef(c, typeof(string))
{
}));
}
}
}