@@ -15,10 +15,12 @@ public class ColDef
15
15
public bool Sortable { get ; set ; }
16
16
public Type Type { get ; set ; }
17
17
public bool Searchable { get ; set ; }
18
+ public SortDirection SortDirection { get ; set ; }
19
+ public string MRenderFunction { get ; set ; }
18
20
19
- public static ColDef Create ( string name , string p1 , Type propertyType , bool visible = true , bool sortable = true )
21
+ public static ColDef Create ( string name , string p1 , Type propertyType , bool visible = true , bool sortable = true , SortDirection sortDirection = SortDirection . None , string mRenderFunction = null )
20
22
{
21
- return new ColDef ( ) { Name = name , DisplayName = p1 , Type = propertyType , Visible = visible , Sortable = sortable } ;
23
+ return new ColDef ( ) { Name = name , DisplayName = p1 , Type = propertyType , Visible = visible , Sortable = sortable , SortDirection = sortDirection , MRenderFunction = mRenderFunction } ;
22
24
}
23
25
}
24
26
public class DataTableConfigVm
@@ -107,6 +109,14 @@ public string Dom
107
109
}
108
110
}
109
111
112
+ public string ColumnSortingString
113
+ {
114
+ get
115
+ {
116
+ return convertColumnSortingToJson ( Columns ) ;
117
+ }
118
+ }
119
+
110
120
public bool ShowPageSizes { get ; set ; }
111
121
112
122
public bool StateSave { get ; set ; }
@@ -244,6 +254,7 @@ private static string convertColumnDefsToJson(IEnumerable<ColDef> columns)
244
254
var nonSortableColumns = columns . Select ( ( x , idx ) => x . Sortable ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
245
255
var nonVisibleColumns = columns . Select ( ( x , idx ) => x . Visible ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
246
256
var nonSearchableColumns = columns . Select ( ( x , idx ) => x . Searchable ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
257
+ var mRenderColumns = columns . Select ( ( x , idx ) => string . IsNullOrEmpty ( x . MRenderFunction ) ? new { x . MRenderFunction , Index = - 1 } : new { x . MRenderFunction , Index = idx } ) . Where ( x => x . Index > - 1 ) . ToArray ( ) ;
247
258
248
259
var defs = new List < dynamic > ( ) ;
249
260
@@ -253,9 +264,24 @@ private static string convertColumnDefsToJson(IEnumerable<ColDef> columns)
253
264
defs . Add ( new { bVisible = false , aTargets = nonVisibleColumns } ) ;
254
265
if ( nonSearchableColumns . Any ( ) )
255
266
defs . Add ( new { bSearchable = false , aTargets = nonSearchableColumns } ) ;
267
+ if ( mRenderColumns . Any ( ) )
268
+ foreach ( var mRenderColumn in mRenderColumns )
269
+ {
270
+ defs . Add ( new { mRender = "%" + mRenderColumn . MRenderFunction + "%" , aTargets = new [ ] { mRenderColumn . Index } } ) ;
271
+ }
272
+
273
+ if ( defs . Count > 0 )
274
+ return new JavaScriptSerializer ( ) . Serialize ( defs ) . Replace ( "\" %" , "" ) . Replace ( "%\" " , "" ) ;
275
+
276
+ return "[]" ;
277
+ }
278
+
279
+ private static string convertColumnSortingToJson ( IEnumerable < ColDef > columns )
280
+ {
281
+ var sortList = columns . Select ( ( c , idx ) => c . SortDirection == SortDirection . None ? new dynamic [ ] { - 1 , "" } : ( c . SortDirection == SortDirection . Ascending ? new dynamic [ ] { idx , "asc" } : new dynamic [ ] { idx , "desc" } ) ) . Where ( x => x [ 0 ] > - 1 ) . ToArray ( ) ;
256
282
257
- if ( defs . Count > 0 )
258
- return new JavaScriptSerializer ( ) . Serialize ( defs ) ;
283
+ if ( sortList . Length > 0 )
284
+ return new JavaScriptSerializer ( ) . Serialize ( sortList ) ;
259
285
260
286
return "[]" ;
261
287
}
0 commit comments