@@ -10,11 +10,13 @@ public class ColDef
10
10
{
11
11
public string Name { get ; set ; }
12
12
public string DisplayName { get ; set ; }
13
+ public bool Visible { get ; set ; }
14
+ public bool Sortable { get ; set ; }
13
15
public Type Type { get ; set ; }
14
16
15
- public static ColDef Create ( string name , string p1 , Type propertyType )
17
+ public static ColDef Create ( string name , string p1 , Type propertyType , bool visible = true , bool sortable = true )
16
18
{
17
- return new ColDef ( ) { Name = name , DisplayName = p1 , Type = propertyType } ;
19
+ return new ColDef ( ) { Name = name , DisplayName = p1 , Type = propertyType , Visible = visible , Sortable = sortable } ;
18
20
}
19
21
}
20
22
public class DataTableConfigVm
@@ -59,6 +61,14 @@ public string JsOptionsString
59
61
}
60
62
}
61
63
64
+ public string ColumnDefsString
65
+ {
66
+ get
67
+ {
68
+ return convertColumnDefsToJson ( Columns ) ;
69
+ }
70
+ }
71
+
62
72
public bool ColumnFilter { get ; set ; }
63
73
64
74
public bool TableTools { get ; set ; }
@@ -210,6 +220,24 @@ private static string convertDictionaryToJsonBody(IDictionary<string, object> di
210
220
return ( new JavaScriptSerializer ( ) ) . Serialize ( ( object ) dictSystem ) . TrimStart ( '{' ) . TrimEnd ( '}' ) ;
211
221
}
212
222
223
+ private static string convertColumnDefsToJson ( IEnumerable < ColDef > columns )
224
+ {
225
+ var nonSortableColumns = columns . Select ( ( x , idx ) => x . Sortable ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
226
+ var nonVisibleColumns = columns . Select ( ( x , idx ) => x . Visible ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
227
+
228
+ var defs = new List < dynamic > ( ) ;
229
+
230
+ if ( nonSortableColumns . Any ( ) )
231
+ defs . Add ( new { bSortable = false , aTargets = nonSortableColumns } ) ;
232
+ if ( nonVisibleColumns . Any ( ) )
233
+ defs . Add ( new { bVisible = false , aTargets = nonVisibleColumns } ) ;
234
+
235
+ if ( defs . Count > 0 )
236
+ return new JavaScriptSerializer ( ) . Serialize ( defs ) ;
237
+
238
+ return "[]" ;
239
+ }
240
+
213
241
private static IDictionary < string , object > convertObjectToDictionary ( object obj )
214
242
{
215
243
// Doing this way because RouteValueDictionary converts to Json in wrong format
0 commit comments