1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Net . Mime ;
45using System . Web . Routing ;
@@ -255,44 +256,45 @@ private static string ConvertDictionaryToJsonBody(IDictionary<string, object> di
255256
256257 private static string ConvertColumnDefsToJson ( ColDef [ ] columns )
257258 {
258- var nonSortableColumns = columns . Select ( ( x , idx ) => x . Sortable ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
259- var nonVisibleColumns = columns . Select ( ( x , idx ) => x . Visible ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
260- var nonSearchableColumns = columns . Select ( ( x , idx ) => x . Searchable ? - 1 : idx ) . Where ( x => x > - 1 ) . ToArray ( ) ;
261- 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 ( ) ;
262- var CssClassColumns = columns . Select ( ( x , idx ) => string . IsNullOrEmpty ( x . CssClass ) ? new { x . CssClass , Index = - 1 } : new { x . CssClass , Index = idx } ) . Where ( x => x . Index > - 1 ) . ToArray ( ) ;
263-
264-
259+ Func < bool , bool > isFalse = x => x == false ;
260+ Func < string , bool > isNonEmptyString = x => ! string . IsNullOrEmpty ( x ) ;
265261
266262 var defs = new List < dynamic > ( ) ;
267263
268- if ( nonSortableColumns . Any ( ) )
269- defs . Add ( new { bSortable = false , aTargets = nonSortableColumns } ) ;
270- if ( nonVisibleColumns . Any ( ) )
271- defs . Add ( new { bVisible = false , aTargets = nonVisibleColumns } ) ;
272- if ( nonSearchableColumns . Any ( ) )
273- defs . Add ( new { bSearchable = false , aTargets = nonSearchableColumns } ) ;
274- if ( mRenderColumns . Any ( ) )
275- foreach ( var mRenderColumn in mRenderColumns )
276- {
277- defs . Add ( new { mRender = "%" + mRenderColumn . MRenderFunction + "%" , aTargets = new [ ] { mRenderColumn . Index } } ) ;
278- }
279- if ( CssClassColumns . Any ( ) )
280- foreach ( var CssClassColumn in CssClassColumns )
281- {
282- defs . Add ( new { className = CssClassColumn . CssClass , aTargets = new [ ] { CssClassColumn . Index } } ) ;
283- }
284-
285- for ( var i = 0 ; i < columns . Length ; i ++ )
286- {
287- if ( columns [ i ] . Width != null )
288- {
289- defs . Add ( new { width = columns [ i ] . Width , aTargets = new [ ] { i } } ) ;
290- }
291- }
292-
264+ defs . AddRange ( ConvertColumnDefsToTargetedProperty (
265+ jsonPropertyName : "bSortable" ,
266+ propertySelector : column => column . Sortable ,
267+ propertyPredicate : isFalse ,
268+ columns : columns ) ) ;
269+ defs . AddRange ( ConvertColumnDefsToTargetedProperty (
270+ jsonPropertyName : "bVisible" ,
271+ propertySelector : column => column . Visible ,
272+ propertyPredicate : isFalse ,
273+ columns : columns ) ) ;
274+ defs . AddRange ( ConvertColumnDefsToTargetedProperty (
275+ jsonPropertyName : "bSearchable" ,
276+ propertySelector : column => column . Searchable ,
277+ propertyPredicate : isFalse ,
278+ columns : columns ) ) ;
279+ defs . AddRange ( ConvertColumnDefsToTargetedProperty (
280+ jsonPropertyName : "mRender" ,
281+ propertySelector : column => column . MRenderFunction ,
282+ propertyConverter : x => new JRaw ( x ) ,
283+ propertyPredicate : isNonEmptyString ,
284+ columns : columns ) ) ;
285+ defs . AddRange ( ConvertColumnDefsToTargetedProperty (
286+ jsonPropertyName : "className" ,
287+ propertySelector : column => column . CssClass ,
288+ propertyPredicate : isNonEmptyString ,
289+ columns : columns ) ) ;
290+ defs . AddRange ( ConvertColumnDefsToTargetedProperty (
291+ jsonPropertyName : "width" ,
292+ propertySelector : column => column . Width ,
293+ propertyPredicate : isNonEmptyString ,
294+ columns : columns ) ) ;
293295
294296 if ( defs . Count > 0 )
295- return new JavaScriptSerializer ( ) . Serialize ( defs ) . Replace ( " \" %" , "" ) . Replace ( "% \" " , "" ) ;
297+ return JsonConvert . SerializeObject ( defs ) ;
296298
297299 return "[]" ;
298300 }
@@ -312,5 +314,42 @@ private static IDictionary<string, object> ConvertObjectToDictionary(object obj)
312314 // Doing this way because RouteValueDictionary converts to Json in wrong format
313315 return new Dictionary < string , object > ( new RouteValueDictionary ( obj ) ) ;
314316 }
317+
318+ private static IEnumerable < JObject > ConvertColumnDefsToTargetedProperty < TProperty > (
319+ string jsonPropertyName ,
320+ Func < ColDef , TProperty > propertySelector ,
321+ Func < TProperty , bool > propertyPredicate ,
322+ IEnumerable < ColDef > columns )
323+ {
324+ return ConvertColumnDefsToTargetedProperty (
325+ jsonPropertyName ,
326+ propertySelector ,
327+ propertyPredicate ,
328+ x => x ,
329+ columns ) ;
330+ }
331+
332+ private static IEnumerable < JObject > ConvertColumnDefsToTargetedProperty < TProperty , TResult > (
333+ string jsonPropertyName ,
334+ Func < ColDef , TProperty > propertySelector ,
335+ Func < TProperty , bool > propertyPredicate ,
336+ Func < TProperty , TResult > propertyConverter ,
337+ IEnumerable < ColDef > columns )
338+ {
339+ return columns
340+ . Select ( ( x , idx ) => new { rawPropertyValue = propertySelector ( x ) , idx } )
341+ . Where ( x => propertyPredicate ( x . rawPropertyValue ) )
342+ . GroupBy (
343+ x => x . rawPropertyValue ,
344+ ( rawPropertyValue , groupedItems ) => new
345+ {
346+ rawPropertyValue ,
347+ indices = groupedItems . Select ( x => x . idx )
348+ } )
349+ . Select ( x => new JObject (
350+ new JProperty ( jsonPropertyName , propertyConverter ( x . rawPropertyValue ) ) ,
351+ new JProperty ( "aTargets" , new JArray ( x . indices ) )
352+ ) ) ;
353+ }
315354 }
316355}
0 commit comments