Skip to content

Commit 5bcb958

Browse files
committed
Fixed enum default value binding
Fixed setting templates to null on GriddlySettings<T> Fixed datetime has cast to not get everything Added column caption back for non sortable columns Removed href="#"
1 parent 251953f commit 5bcb958

File tree

8 files changed

+77
-32
lines changed

8 files changed

+77
-32
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.28.0")]
19-
[assembly: AssemblyFileVersion("1.0.28.0")]
18+
[assembly: AssemblyVersion("1.0.32.0")]
19+
[assembly: AssemblyFileVersion("1.0.32.0")]

Griddly.Mvc/GriddlyFilterExtensions.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
107107

108108
if (defaultValues == null || defaultValue is string)
109109
{
110-
string value = GetDefaultValueString(defaultValue);
110+
string value = defaultValue.ToString();
111111

112112
foreach (SelectListItem item in selectableItemsList)
113113
item.Selected = item.Value == value;
@@ -116,7 +116,7 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
116116
{
117117
foreach (object value in defaultValues)
118118
{
119-
string valueString = GetDefaultValueString(value);
119+
string valueString = value.ToString();
120120

121121
foreach (SelectListItem item in selectableItemsList.Where(x => x.Value == valueString))
122122
item.Selected = true;
@@ -144,14 +144,6 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
144144
};
145145
}
146146

147-
static string GetDefaultValueString(object defaultValue)
148-
{
149-
if (defaultValue.GetType().IsEnum)
150-
return Convert.ToInt32(defaultValue).ToString();
151-
else
152-
return defaultValue.ToString();
153-
}
154-
155147
static string GetField(GriddlyColumn column)
156148
{
157149
string value = null;

Griddly.Mvc/GriddlySettings.cs

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,36 +185,80 @@ public SortField[] GetDefaultSort()
185185

186186
public class GriddlySettings<TRow> : GriddlySettings
187187
{
188-
public new Func<GriddlySettings<TRow>, object> FilterTemplate { set { base.FilterTemplate = (x) => value((GriddlySettings<TRow>)x); } }
189-
public new Func<GriddlySettings<TRow>, object> InlineFilterTemplate { set { base.InlineFilterTemplate = (x) => value((GriddlySettings<TRow>)x); } }
190-
public new Func<TRow, object> RowClickUrl { set { base.RowClickUrl = (x) => value((TRow)x); } }
191-
public new Func<TRow, object> RowClass { set { base.RowClass = (x) => value((TRow)x); } }
188+
public new Func<GriddlySettings<TRow>, object> FilterTemplate
189+
{
190+
set
191+
{
192+
if (value != null)
193+
base.FilterTemplate = (x) => value((GriddlySettings<TRow>)x);
194+
else
195+
base.FilterTemplate = null;
196+
}
197+
}
198+
199+
public new Func<GriddlySettings<TRow>, object> InlineFilterTemplate
200+
{
201+
set
202+
{
203+
if (value != null)
204+
base.InlineFilterTemplate = (x) => value((GriddlySettings<TRow>)x);
205+
else
206+
base.InlineFilterTemplate = null;
207+
}
208+
}
209+
210+
public new Func<TRow, object> RowClickUrl
211+
{
212+
set
213+
{
214+
if (value != null)
215+
base.RowClickUrl = (x) => value((TRow)x);
216+
else
217+
base.RowClickUrl = null;
218+
}
219+
}
192220

221+
public new Func<TRow, object> RowClass
222+
{
223+
set
224+
{
225+
if (value != null)
226+
base.RowClass = (x) => value((TRow)x);
227+
else
228+
base.RowClass = null;
229+
}
230+
}
231+
193232
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)
194233
{
195234
var compiledTemplate = template.Compile();
196235
ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TRow, TProperty>(template, new ViewDataDictionary<TRow>());
197236
string htmlFieldName = ExpressionHelper.GetExpressionText(template);
198237

238+
Type type = metadata.ModelType;
239+
240+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
241+
type = Nullable.GetUnderlyingType(type);
242+
199243
if (className == null)
200244
{
201-
if (metadata.ModelType == typeof(bool) || metadata.ModelType == typeof(bool?) ||
202-
metadata.ModelType == typeof(DateTime) || metadata.ModelType == typeof(DateTime?) || metadata.ModelType.HasCastOperator<DateTime>())
203-
className = "align-center";
204-
else if (metadata.ModelType == typeof(byte) || metadata.ModelType == typeof(sbyte) ||
205-
metadata.ModelType == typeof(short) || metadata.ModelType == typeof(ushort) ||
206-
metadata.ModelType == typeof(int) || metadata.ModelType == typeof(uint) ||
207-
metadata.ModelType == typeof(long) || metadata.ModelType == typeof(ulong) ||
208-
metadata.ModelType == typeof(float) ||
209-
metadata.ModelType == typeof(double) ||
210-
metadata.ModelType == typeof(decimal))
245+
if (type == typeof(byte) || type == typeof(sbyte) ||
246+
type == typeof(short) || type == typeof(ushort) ||
247+
type == typeof(int) || type == typeof(uint) ||
248+
type == typeof(long) || type == typeof(ulong) ||
249+
type == typeof(float) ||
250+
type == typeof(double) ||
251+
type == typeof(decimal))
211252
className = "align-right";
253+
else if (type == typeof(bool) ||
254+
type == typeof(DateTime) || type.HasCastOperator<DateTime>())
255+
className = "align-center";
212256
}
213257

214258
if (caption == null)
215259
caption = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
216260

217-
if (metadata.ModelType == typeof(bool) || metadata.ModelType == typeof(bool?) && (BoolTrueHtml != null || BoolFalseHtml != null))
261+
if (type == typeof(bool) && (BoolTrueHtml != null || BoolFalseHtml != null))
218262
{
219263
return TemplateColumn(
220264
(row) =>

Griddly.Mvc/InternalExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Text.RegularExpressions;
88
using System.Web;
99
using System.Web.Mvc;
10-
using System.Web.Routing;
1110

1211
namespace Griddly.Mvc
1312
{
@@ -20,8 +19,8 @@ internal static bool HasCastOperator<T>(this Type type)
2019
bool castable = type.GetMethods(BindingFlags.Public | BindingFlags.Static)
2120
.Any(
2221
m => m.ReturnType == typeof(T) &&
23-
m.Name == "op_Implicit" ||
24-
m.Name == "op_Explicit"
22+
(m.Name == "op_Implicit" ||
23+
m.Name == "op_Explicit")
2524
);
2625
return castable;
2726
}

Griddly/Models/TestGridItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Griddly.Models
44
{
55
public class TestGridItem
66
{
7+
public decimal? Test { get; set; }
78
public string FirstName { get; set; }
89
public string LastName { get; set; }
910
public string Company { get; set; }

Griddly/Scripts/griddly.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,10 @@
504504
var dataType = filter.data("filter-datatype");
505505
var display = null;
506506

507+
508+
// TODO: shove formatted values back into boxes to ensure post is correct?
509+
// TODO: for numbers, do correctly shredded numeric (no symbols, but numbers and decimals etc.)
510+
// TODO: for dates, push actual formatted date
507511
if (filter.hasClass("griddly-filter-box"))
508512
{
509513
if (!dontHide)

Griddly/Views/Home/FilterBoxGrid.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{
88
PageSize = 5
99
}
10+
.Column(x => x.Test)
1011
.Column(x => x.FirstName, "First Name")
1112
.Column(x => x.LastName, "Last Name", defaultSort: SortDirection.Ascending, filter: x => x.FilterBox(FilterDataType.String, "ba"))
1213
.Column(x => x.Company, "Company", filter: x => x.FilterBox(FilterDataType.Decimal, 53.5))

Griddly/Views/Shared/Griddly/Griddly.cshtml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@
105105
{
106106
<i class="glyphicon glyphicon-check"></i>
107107
}
108+
else
109+
{
110+
@column.Caption
111+
}
108112
</th>
109113
}
110114
</tr>
@@ -186,7 +190,7 @@
186190
@Html.AttributeIf("data-griddly-filter-ismultiple", filterList != null, x => filterList.IsMultiple.ToString().ToLower())
187191
@Html.AttributeIf("data-griddly-filter-isnoneall", filterList != null, x => filterList.IsNoneAll.ToString().ToLower())
188192
@Html.AttributeIf("data-griddly-filter-displayitemcount", filterList != null, x => filterList.DisplayItemCount)>
189-
<a class="btn btn-link btn-xs filter-trigger" href="#">
193+
<a class="btn btn-link btn-xs filter-trigger">
190194
<span class="griddly-filter-display">
191195
@if (filterBox != null)
192196
{
@@ -357,7 +361,7 @@
357361
@helper RenderListItem(GriddlyFilterList filter, SelectListItem item, bool isGrouped = false)
358362
{
359363
<li class="@(item.Selected ? "griddly-filter-selected" : null) @(isGrouped ? "griddly-list-group" : null)">
360-
<a href="#">
364+
<a>
361365
<input name="@filter.Field" type="@(filter.IsMultiple ? "checkbox" : "radio")" checked="@(item.Selected)" value="@item.Value" />
362366
<i class="glyphicon glyphicon-@(filter.IsMultiple ? "ok" : "record") griddly-filter-selected-indicator"></i>
363367
@item.Text

0 commit comments

Comments
 (0)