1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
4
5
using System . Web . Mvc ;
@@ -12,8 +13,8 @@ public static GriddlyFilterBox FilterBox(this GriddlyColumn column, FilterDataTy
12
13
if ( name == null )
13
14
name = column . Caption ;
14
15
15
- if ( field == null && column . SortField != null && ! column . SortField . Contains ( "." ) )
16
- field = column . SortField . Substring ( 0 , 1 ) . ToLower ( ) + column . SortField . Substring ( 1 ) ;
16
+ if ( field == null )
17
+ field = GetField ( column ) ;
17
18
18
19
if ( string . IsNullOrWhiteSpace ( name ) )
19
20
throw new ArgumentNullException ( "name" , "Name must be specified." ) ;
@@ -34,11 +35,10 @@ public static GriddlyFilterRange FilterRange(this GriddlyColumn column, FilterDa
34
35
if ( name == null )
35
36
name = column . Caption ;
36
37
37
- if ( field == null && column . SortField != null && ! column . SortField . Contains ( "." ) )
38
- {
39
- field = column . SortField . Substring ( 0 , 1 ) . ToLower ( ) + column . SortField . Substring ( 1 ) + "Start" ;
40
- fieldEnd = column . SortField . Substring ( 0 , 1 ) . ToLower ( ) + column . SortField . Substring ( 1 ) + "End" ;
41
- }
38
+ if ( field == null )
39
+ field = GetField ( column ) + "Start" ;
40
+ if ( fieldEnd == null )
41
+ fieldEnd = GetField ( column ) + "End" ;
42
42
43
43
if ( string . IsNullOrWhiteSpace ( name ) )
44
44
throw new ArgumentNullException ( "name" , "Name must be specified." ) ;
@@ -58,13 +58,23 @@ public static GriddlyFilterRange FilterRange(this GriddlyColumn column, FilterDa
58
58
} ;
59
59
}
60
60
61
- public static GriddlyFilterList FilterList ( this GriddlyColumn column , IEnumerable < SelectListItem > items , object defaultValue = null , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
61
+ public static GriddlyFilterList FilterList ( this GriddlyColumn column , IEnumerable < SelectListItem > items , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
62
+ {
63
+ return column . FilterList ( items , ( object ) null , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
64
+ }
65
+
66
+ public static GriddlyFilterList FilterList ( this GriddlyColumn column , IEnumerable < SelectListItem > items , Array defaultValue , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
67
+ {
68
+ return column . FilterList ( items , ( object ) defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
69
+ }
70
+
71
+ public static GriddlyFilterList FilterList ( this GriddlyColumn column , IEnumerable < SelectListItem > items , object defaultValue , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
62
72
{
63
73
if ( name == null )
64
74
name = column . Caption ;
65
75
66
- if ( field == null && column . SortField != null && ! column . SortField . Contains ( "." ) )
67
- field = column . SortField . Substring ( 0 , 1 ) . ToLower ( ) + column . SortField . Substring ( 1 ) ;
76
+ if ( field == null )
77
+ field = GetField ( column ) ;
68
78
69
79
if ( string . IsNullOrWhiteSpace ( name ) )
70
80
throw new ArgumentNullException ( "name" , "Name must be specified." ) ;
@@ -74,11 +84,11 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
74
84
List < SelectListItem > itemsList = new List < SelectListItem > ( ) ;
75
85
76
86
if ( ! string . IsNullOrWhiteSpace ( nullItemText ) )
77
- itemsList . Add ( new SelectListItem ( ) { Text = nullItemText } ) ;
87
+ itemsList . Add ( new SelectListItem ( ) { Text = nullItemText , Value = "" } ) ;
78
88
79
89
itemsList . AddRange ( items ) ;
80
90
81
- if ( isMultiple && defaultSelectAll )
91
+ if ( isMultiple && defaultSelectAll && defaultValue == null )
82
92
{
83
93
foreach ( SelectListItem item in itemsList )
84
94
item . Selected = true ;
@@ -87,10 +97,25 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
87
97
{
88
98
if ( defaultValue != null )
89
99
{
90
- string value = defaultValue . ToString ( ) ;
91
-
92
- foreach ( SelectListItem item in itemsList )
93
- item . Selected = item . Value == value ;
100
+ IEnumerable defaultValues = defaultValue as IEnumerable ;
101
+
102
+ if ( defaultValues == null )
103
+ {
104
+ string value = GetDefaultValueString ( defaultValue ) ;
105
+
106
+ foreach ( SelectListItem item in itemsList )
107
+ item . Selected = item . Value == value ;
108
+ }
109
+ else
110
+ {
111
+ foreach ( object value in defaultValues )
112
+ {
113
+ string valueString = GetDefaultValueString ( value ) ;
114
+
115
+ foreach ( SelectListItem item in itemsList . Where ( x => x . Value == valueString ) )
116
+ item . Selected = true ;
117
+ }
118
+ }
94
119
}
95
120
else
96
121
{
@@ -102,26 +127,83 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
102
127
return new GriddlyFilterList ( )
103
128
{
104
129
Field = field ,
130
+ Default = defaultValue ,
105
131
Items = itemsList ,
106
132
Name = name ,
107
133
IsMultiple = isMultiple ,
108
- IsNoneAll = isNoneAll
134
+ IsNoneAll = isNoneAll ,
135
+ IsNullable = ! string . IsNullOrWhiteSpace ( nullItemText )
109
136
} ;
110
137
}
111
138
112
- public static GriddlyFilterList FilterEnum < T > ( this GriddlyColumn column , T ? defaultValue = null , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
139
+ static string GetDefaultValueString ( object defaultValue )
140
+ {
141
+ if ( defaultValue . GetType ( ) . IsEnum )
142
+ return Convert . ToInt32 ( defaultValue ) . ToString ( ) ;
143
+ else
144
+ return defaultValue . ToString ( ) ;
145
+ }
146
+
147
+ static string GetField ( GriddlyColumn column )
148
+ {
149
+ string value = null ;
150
+
151
+ if ( column . SortField != null )
152
+ {
153
+ value = column . SortField . Split ( '.' ) . Last ( ) ;
154
+
155
+ if ( value . Length > 1 )
156
+ value = char . ToLower ( value [ 0 ] ) + value . Substring ( 1 ) ;
157
+ }
158
+
159
+ return value ;
160
+ }
161
+
162
+ public static GriddlyFilterList FilterEnum < T > ( this GriddlyColumn column , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
113
163
where T : struct
114
164
{
115
- if ( ! typeof ( T ) . IsEnum )
116
- throw new InvalidOperationException ( "Type must be an Enum." ) ;
165
+ return column . FilterList ( Extensions . ToSelectListItems < T > ( ) , null , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
166
+ }
117
167
118
- IEnumerable < SelectListItem > items = Extensions . ToSelectListItems < T > ( ) ;
119
- string value = defaultValue != null ? Convert . ToInt32 ( defaultValue . Value ) . ToString ( ) : null ;
168
+ public static GriddlyFilterList FilterEnum < T > ( this GriddlyColumn column , T ? defaultValue , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
169
+ where T : struct
170
+ {
171
+ return column . FilterList ( Extensions . ToSelectListItems < T > ( ) , defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
172
+ }
173
+
174
+ public static GriddlyFilterList FilterEnum < T > ( this GriddlyColumn column , T [ ] defaultValue , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
175
+ where T : struct
176
+ {
177
+ return column . FilterList ( Extensions . ToSelectListItems < T > ( ) , defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
178
+ }
179
+
180
+ public static GriddlyFilterList FilterEnum < T > ( this GriddlyColumn column , T ? [ ] defaultValue , bool isMultiple = true , bool defaultSelectAll = true , string nullItemText = null , bool isNoneAll = true , string field = null , string name = null )
181
+ where T : struct
182
+ {
183
+ return column . FilterList ( Extensions . ToSelectListItems < T > ( ) , defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
184
+ }
120
185
121
- return column . FilterList ( items , value , isMultiple , defaultSelectAll , nullItemText , isNoneAll , field , name ) ;
186
+ public static GriddlyFilterList FilterBool ( this GriddlyColumn column , bool isMultiple = false , bool defaultSelectAll = false , string nullItemText = null , bool isNoneAll = false , string trueLabel = "Yes" , string falseLabel = "No" , string field = null , string name = null )
187
+ {
188
+ return column . FilterBool ( ( object ) null , isMultiple , defaultSelectAll , nullItemText , isNoneAll , trueLabel , falseLabel , field , name ) ;
189
+ }
190
+
191
+ public static GriddlyFilterList FilterBool ( this GriddlyColumn column , bool ? defaultValue , bool isMultiple = false , bool defaultSelectAll = false , string nullItemText = null , bool isNoneAll = false , string trueLabel = "Yes" , string falseLabel = "No" , string field = null , string name = null )
192
+ {
193
+ return column . FilterBool ( defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , trueLabel , falseLabel , field , name ) ;
194
+ }
195
+
196
+ public static GriddlyFilterList FilterBool ( this GriddlyColumn column , bool [ ] defaultValue , bool isMultiple = false , bool defaultSelectAll = false , string nullItemText = null , bool isNoneAll = false , string trueLabel = "Yes" , string falseLabel = "No" , string field = null , string name = null )
197
+ {
198
+ return column . FilterBool ( defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , trueLabel , falseLabel , field , name ) ;
199
+ }
200
+
201
+ public static GriddlyFilterList FilterBool ( this GriddlyColumn column , bool ? [ ] defaultValue , bool isMultiple = false , bool defaultSelectAll = false , string nullItemText = null , bool isNoneAll = false , string trueLabel = "Yes" , string falseLabel = "No" , string field = null , string name = null )
202
+ {
203
+ return column . FilterBool ( defaultValue , isMultiple , defaultSelectAll , nullItemText , isNoneAll , trueLabel , falseLabel , field , name ) ;
122
204
}
123
205
124
- public static GriddlyFilterList FilterBool ( this GriddlyColumn column , bool ? defaultValue = null , bool isMultiple = false , bool defaultSelectAll = false , string nullItemText = null , bool isNoneAll = false , string trueLabel = "Yes" , string falseLabel = "No" , string field = null , string name = null )
206
+ static GriddlyFilterList FilterBool ( this GriddlyColumn column , object defaultValue , bool isMultiple = false , bool defaultSelectAll = false , string nullItemText = null , bool isNoneAll = false , string trueLabel = "Yes" , string falseLabel = "No" , string field = null , string name = null )
125
207
{
126
208
List < SelectListItem > items = new List < SelectListItem > ( )
127
209
{
0 commit comments