11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Web . Routing ;
45using System . Web . Script . Serialization ;
56
67namespace Mvc . JQuery . Datatables
@@ -54,7 +55,7 @@ public string JsOptionsString
5455 {
5556 get
5657 {
57- return ( new JavaScriptSerializer ( ) ) . Serialize ( ( object ) JsOptions ) . TrimStart ( '{' ) . TrimEnd ( '}' ) ;
58+ return convertDictionaryToJsonBody ( JsOptions ) ;
5859 }
5960 }
6061
@@ -111,12 +112,14 @@ public class _FilterOn<TTarget>
111112 private readonly TTarget _target ;
112113 private readonly FilterRuleList _list ;
113114 private readonly Func < string , Type , bool > _predicate ;
115+ private readonly IDictionary < string , object > _jsOptions ;
114116
115- public _FilterOn ( TTarget target , FilterRuleList list , Func < string , Type , bool > predicate )
117+ public _FilterOn ( TTarget target , FilterRuleList list , Func < string , Type , bool > predicate , IDictionary < string , object > jsOptions )
116118 {
117119 _target = target ;
118120 _list = list ;
119121 _predicate = predicate ;
122+ _jsOptions = jsOptions ;
120123 }
121124
122125 public TTarget Select ( params string [ ] options )
@@ -150,6 +153,12 @@ public TTarget CheckBoxes(params string[] options)
150153 return _target ;
151154 }
152155
156+ public TTarget Text ( )
157+ {
158+ AddRule ( "{type: 'text'}" ) ;
159+ return _target ;
160+ }
161+
153162 public TTarget None ( )
154163 {
155164 AddRule ( "null" ) ;
@@ -158,18 +167,53 @@ public TTarget None()
158167
159168 private void AddRule ( string result )
160169 {
170+ if ( result != "null" && this . _jsOptions != null && this . _jsOptions . Count > 0 )
171+ {
172+ var _jsOptionsAsJson = DataTableVm . convertDictionaryToJsonBody ( this . _jsOptions ) ;
173+ result = result . TrimEnd ( '}' ) + ", " + _jsOptionsAsJson + "}" ;
174+ }
161175 _list . Insert ( 0 , ( c , t ) => _predicate ( c , t ) ? result : null ) ;
162176 }
163177 }
164178 public _FilterOn < DataTableVm > FilterOn < T > ( )
165179 {
166- return new _FilterOn < DataTableVm > ( this , this . FilterTypeRules , ( c , t ) => t == typeof ( T ) ) ;
180+ return FilterOn < T > ( null ) ;
181+ }
182+ public _FilterOn < DataTableVm > FilterOn < T > ( object jsOptions )
183+ {
184+ IDictionary < string , object > optionsDict = DataTableVm . convertObjectToDictionary ( jsOptions ) ;
185+ return FilterOn < T > ( optionsDict ) ;
186+ }
187+ public _FilterOn < DataTableVm > FilterOn < T > ( IDictionary < string , object > jsOptions )
188+ {
189+ return new _FilterOn < DataTableVm > ( this , this . FilterTypeRules , ( c , t ) => t == typeof ( T ) , jsOptions ) ;
167190 }
168191 public _FilterOn < DataTableVm > FilterOn ( string columnName )
169192 {
170- return new _FilterOn < DataTableVm > ( this , this . FilterTypeRules , ( c , t ) => c == columnName ) ;
193+ return FilterOn ( columnName , null ) ;
194+ }
195+ public _FilterOn < DataTableVm > FilterOn ( string columnName , object jsOptions )
196+ {
197+ IDictionary < string , object > optionsDict = DataTableVm . convertObjectToDictionary ( jsOptions ) ;
198+ return FilterOn ( columnName , optionsDict ) ;
199+ }
200+ public _FilterOn < DataTableVm > FilterOn ( string columnName , IDictionary < string , object > jsOptions )
201+ {
202+ return new _FilterOn < DataTableVm > ( this , this . FilterTypeRules , ( c , t ) => c == columnName , jsOptions ) ;
203+ }
204+
205+ private static string convertDictionaryToJsonBody ( IDictionary < string , object > dict )
206+ {
207+ // Converting to System.Collections.Generic.Dictionary<> to ensure Dictionary will be converted to Json in correct format
208+ var dictSystem = new Dictionary < string , object > ( dict ) ;
209+ return ( new JavaScriptSerializer ( ) ) . Serialize ( ( object ) dictSystem ) . TrimStart ( '{' ) . TrimEnd ( '}' ) ;
171210 }
172211
212+ private static IDictionary < string , object > convertObjectToDictionary ( object obj )
213+ {
214+ // Doing this way because RouteValueDictionary converts to Json in wrong format
215+ return new Dictionary < string , object > ( new RouteValueDictionary ( obj ) ) ;
216+ }
173217 }
174218
175219 public class FilterRuleList : List < Func < string , Type , string > >
0 commit comments