1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . ComponentModel ;
4- using System . Linq ;
4+ using System . Linq ;
55using System . Linq . Expressions ;
66using 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