Skip to content

Commit e5b0b91

Browse files
committed
Fixed default parameter issues
1 parent c1b583c commit e5b0b91

File tree

7 files changed

+159
-49
lines changed

7 files changed

+159
-49
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("1.0.12.0")]
19-
[assembly: AssemblyFileVersion("1.0.12.0")]
18+
[assembly: AssemblyVersion("1.0.18.0")]
19+
[assembly: AssemblyFileVersion("1.0.18.0")]

Griddly.Mvc/GriddlyDefaultParametersAttribute.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Web.Mvc;
45

@@ -28,12 +29,32 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
2829
foreach (GriddlyFilter filter in settings.Filters.Union(settings.Columns.Where(x => x.Filter != null).Select(x => x.Filter)))
2930
{
3031
if (filter.Default != null)
31-
settings.FilterDefaults[filter.Field] = filter.Default;
32+
{
33+
object value = filter.Default;
3234

33-
GriddlyFilterRange rangeFilter = filter as GriddlyFilterRange;
35+
GriddlyFilterList filterList = filter as GriddlyFilterList;
36+
37+
if (filterList != null && filterList.IsMultiple && !value.GetType().IsArray)
38+
{
39+
Type type = filter.Default.GetType();
3440

35-
if (rangeFilter != null && rangeFilter.DefaultEnd != null)
36-
settings.FilterDefaults[rangeFilter.FieldEnd] = rangeFilter.DefaultEnd;
41+
if (filterList.IsNullable)
42+
type = typeof(Nullable<>).MakeGenericType(type);
43+
44+
Array array = Array.CreateInstance(type, 1);
45+
46+
array.SetValue(filter.Default, 0);
47+
48+
value = array;
49+
}
50+
51+
settings.FilterDefaults[filter.Field] = value;
52+
}
53+
54+
GriddlyFilterRange filterRange = filter as GriddlyFilterRange;
55+
56+
if (filterRange != null && filterRange.DefaultEnd != null)
57+
settings.FilterDefaults[filterRange.FieldEnd] = filterRange.DefaultEnd;
3758
}
3859

3960
foreach (KeyValuePair<string, object> param in settings.FilterDefaults)

Griddly.Mvc/GriddlyFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public GriddlyFilterList()
8383

8484
public bool IsMultiple { get; set; }
8585
public bool IsNoneAll { get; set; }
86+
public bool IsNullable { get; set; }
8687
}
8788

8889
public enum FilterDataType

Griddly.Mvc/GriddlyFilterExtensions.cs

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Web.Mvc;
@@ -12,8 +13,8 @@ public static GriddlyFilterBox FilterBox(this GriddlyColumn column, FilterDataTy
1213
if (name == null)
1314
name = column.Caption;
1415

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);
1718

1819
if (string.IsNullOrWhiteSpace(name))
1920
throw new ArgumentNullException("name", "Name must be specified.");
@@ -34,11 +35,10 @@ public static GriddlyFilterRange FilterRange(this GriddlyColumn column, FilterDa
3435
if (name == null)
3536
name = column.Caption;
3637

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";
4242

4343
if (string.IsNullOrWhiteSpace(name))
4444
throw new ArgumentNullException("name", "Name must be specified.");
@@ -58,13 +58,23 @@ public static GriddlyFilterRange FilterRange(this GriddlyColumn column, FilterDa
5858
};
5959
}
6060

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)
6272
{
6373
if (name == null)
6474
name = column.Caption;
6575

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);
6878

6979
if (string.IsNullOrWhiteSpace(name))
7080
throw new ArgumentNullException("name", "Name must be specified.");
@@ -74,11 +84,11 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
7484
List<SelectListItem> itemsList = new List<SelectListItem>();
7585

7686
if (!string.IsNullOrWhiteSpace(nullItemText))
77-
itemsList.Add(new SelectListItem() { Text = nullItemText });
87+
itemsList.Add(new SelectListItem() { Text = nullItemText, Value = "" });
7888

7989
itemsList.AddRange(items);
8090

81-
if (isMultiple && defaultSelectAll)
91+
if (isMultiple && defaultSelectAll && defaultValue == null)
8292
{
8393
foreach (SelectListItem item in itemsList)
8494
item.Selected = true;
@@ -87,10 +97,25 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
8797
{
8898
if (defaultValue != null)
8999
{
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+
}
94119
}
95120
else
96121
{
@@ -102,26 +127,83 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
102127
return new GriddlyFilterList()
103128
{
104129
Field = field,
130+
Default = defaultValue,
105131
Items = itemsList,
106132
Name = name,
107133
IsMultiple = isMultiple,
108-
IsNoneAll = isNoneAll
134+
IsNoneAll = isNoneAll,
135+
IsNullable = !string.IsNullOrWhiteSpace(nullItemText)
109136
};
110137
}
111138

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)
113163
where T : struct
114164
{
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+
}
117167

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+
}
120185

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);
122204
}
123205

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)
125207
{
126208
List<SelectListItem> items = new List<SelectListItem>()
127209
{

Griddly.Mvc/GriddlySettings.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public abstract class GriddlySettings
1515
public static string DefaultTableClassName = "table table-bordered table-hover";
1616
public static string ButtonTemplate = "~/Views/Shared/Griddly/BootstrapButton.cshtml";
1717
public static string ButtonListTemplate = "~/Views/Shared/Griddly/ButtonStrip.cshtml";
18-
public static string BoolTrueHtml = "<span class=\"icon20 check_gray\"></span>";
19-
public static string BoolFalseHtml = null;
18+
public static HtmlString BoolTrueHtml = null;
19+
public static HtmlString BoolFalseHtml = null;
2020
public static int? DefaultPageSize = null;
2121
public static bool DefaultShowFilterInitially = true;
2222

@@ -190,10 +190,11 @@ public class GriddlySettings<TRow> : GriddlySettings
190190
public new Func<TRow, object> RowClickUrl { set { base.RowClickUrl = (x) => value((TRow)x); } }
191191
public new Func<TRow, object> RowClass { set { base.RowClass = (x) => value((TRow)x); } }
192192

193-
public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> template, string caption, string format = null, string sortField = null, SortDirection? defaultSort = null, string className = null, bool isExportOnly = false, string width = null, Func<GriddlyColumn, GriddlyFilter> filter = null)
193+
public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> template, string caption = null, string format = null, string sortField = null, SortDirection? defaultSort = null, string className = null, bool isExportOnly = false, string width = null, Func<GriddlyColumn, GriddlyFilter> filter = null)
194194
{
195195
var compiledTemplate = template.Compile();
196196
ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TRow, TProperty>(template, new ViewDataDictionary<TRow>());
197+
string htmlFieldName = ExpressionHelper.GetExpressionText(template);
197198

198199
if (className == null)
199200
{
@@ -210,28 +211,26 @@ public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>>
210211
className = "align-right";
211212
}
212213

213-
if (metadata.ModelType == typeof(bool) || metadata.ModelType == typeof(bool?))
214+
if (caption == null)
215+
caption = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
216+
217+
if (metadata.ModelType == typeof(bool) || metadata.ModelType == typeof(bool?) && (BoolTrueHtml != null || BoolFalseHtml != null))
214218
{
215219
return TemplateColumn(
216220
(row) =>
217221
{
218-
string value = (compiledTemplate(row) as bool? == true) ? BoolTrueHtml : BoolFalseHtml;
219-
220-
if (!string.IsNullOrWhiteSpace(value))
221-
return new HtmlString(value);
222-
else
223-
return null;
222+
return (compiledTemplate(row) as bool? == true) ? BoolTrueHtml : BoolFalseHtml;
224223
},
225-
caption, format, sortField, defaultSort, className, isExportOnly
226-
);
224+
caption, format, sortField, defaultSort, className, isExportOnly, width, filter
225+
);
227226
}
228227

229228
Add(new GriddlyColumn<TRow>()
230229
{
231230
Template = (row) => compiledTemplate(row),
232231
Caption = caption,
233232
Format = format,
234-
SortField = sortField ?? ExpressionHelper.GetExpressionText(template),
233+
SortField = sortField ?? htmlFieldName,
235234
DefaultSort = defaultSort,
236235
ClassName = className,
237236
IsExportOnly = isExportOnly,

0 commit comments

Comments
 (0)