Skip to content

Commit 54720ac

Browse files
committed
Added inline filters
1 parent 47330db commit 54720ac

23 files changed

+1214
-149
lines changed

Griddly.Mvc/DapperGriddlyResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public DapperGriddlyResult(Func<IDbConnection> getConnection, string sql, object
3434
_fixedSort = fixedSort;
3535
}
3636

37-
protected override long GetCount()
37+
public override long GetCount()
3838
{
3939
if (_overallCount == null)
4040
{
@@ -48,14 +48,14 @@ protected override long GetCount()
4848
return _overallCount.Value;
4949
}
5050

51-
protected override IList<T> GetPage(int pageNumber, int pageSize, SortField[] sortFields)
51+
public override IList<T> GetPage(int pageNumber, int pageSize, SortField[] sortFields)
5252
{
5353
string sql = string.Format("{0} " + (_fixedSort ? "" : "ORDER BY {1}") + " OFFSET {2} ROWS FETCH NEXT {3} ROWS ONLY", _sql, BuildSortClause(sortFields), pageNumber * pageSize, pageSize);
5454

5555
return ExecuteQuery(sql, _param);
5656
}
5757

58-
protected override IEnumerable<T> GetAll(SortField[] sortFields)
58+
public override IEnumerable<T> GetAll(SortField[] sortFields)
5959
{
6060
string sql = _fixedSort ? _sql : string.Format("{0} ORDER BY {1}", _sql, BuildSortClause(sortFields));
6161

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
5151
</Reference>
5252
<Reference Include="System" />
53+
<Reference Include="System.ComponentModel.DataAnnotations" />
5354
<Reference Include="System.Core" />
55+
<Reference Include="System.Data.Entity.Design" />
5456
<Reference Include="System.Web" />
5557
<Reference Include="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5658
<Private>True</Private>
@@ -84,12 +86,15 @@
8486
</ItemGroup>
8587
<ItemGroup>
8688
<Compile Include="DapperGriddlyResult.cs" />
89+
<Compile Include="GriddlyFilterExtensions.cs" />
90+
<Compile Include="InternalExtensions.cs" />
8791
<Compile Include="GriddlyButton.cs" />
8892
<Compile Include="GriddlyColumn.cs" />
8993
<Compile Include="GriddlyCsvResult.cs" />
9094
<Compile Include="GriddlyDefaultParametersAttribute.cs" />
9195
<Compile Include="GriddlyExcelResult.cs" />
9296
<Compile Include="GriddlyExtensions.cs" />
97+
<Compile Include="GriddlyFilter.cs" />
9398
<Compile Include="GriddlyHtmlButton.cs" />
9499
<Compile Include="GriddlyResult.cs" />
95100
<Compile Include="GriddlyResultPage.cs" />

Griddly.Mvc/GriddlyColumn.cs

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.ComponentModel;
3-
using System.Reflection;
4-
using System.Text.RegularExpressions;
52
using System.Web;
63
using System.Web.Helpers;
74
using System.Web.WebPages;
@@ -18,6 +15,8 @@ public abstract class GriddlyColumn
1815
public string Width { get; set; }
1916
public bool IsExportOnly { get; set; }
2017

18+
public GriddlyFilter Filter { get; set; }
19+
2120
public abstract HtmlString RenderCell(object row, bool encode = true);
2221
public abstract object RenderCellValue(object row);
2322

@@ -28,64 +27,24 @@ protected virtual HtmlString RenderValue(object value, bool encode = true)
2827

2928
if (Format == null)
3029
{
31-
if (value is DateTime || value is DateTime? || value.GetType().Name.Contains("M3DateTime"))
30+
if (value is DateTime || value is DateTime? || value.GetType().HasCastOperator<DateTime>())
3231
Format = "d";
3332
}
3433

3534
if (Format != null)
3635
value = string.Format("{0:" + Format + "}", value);
3736
else if (value is Enum)
38-
value = ToStringDescription((Enum)value);
37+
value = Extensions.ToStringDescription((Enum)value);
3938
else
4039
value = value.ToString();
4140

4241
if (value is HtmlString)
4342
return (HtmlString)value;
4443
else if (encode)
45-
return GetHtmlView(value.ToString());
44+
return Extensions.GetHtmlView(value.ToString());
4645
else
4746
return new HtmlString(value.ToString());
4847
}
49-
50-
static readonly Regex _urlRegex = new Regex(@"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?", RegexOptions.IgnoreCase | RegexOptions.Compiled);
51-
52-
static HtmlString GetHtmlView(string value, bool turnUrlsIntoLinks = false)
53-
{
54-
if (value == null)
55-
return null;
56-
57-
value = HttpUtility.HtmlEncode(value).Replace(" ", "&nbsp; ")
58-
.Replace("\r\n", "<br/>").Replace("\r", "<br/>").Replace("\n", "<br/>");
59-
60-
if (turnUrlsIntoLinks)
61-
{
62-
foreach (Match match in _urlRegex.Matches(value))
63-
{
64-
if (match != null && !string.IsNullOrWhiteSpace(match.Value))
65-
value = value.Replace(match.Value, string.Format("<a href=\"{0}\">{1}</a>", HttpUtility.HtmlAttributeEncode(match.Value), match.Value));
66-
}
67-
}
68-
69-
return new HtmlString(value);
70-
}
71-
72-
protected static string ToStringDescription(Enum value)
73-
{
74-
if (value == null)
75-
return null;
76-
77-
FieldInfo fi = value.GetType().GetField(value.ToString());
78-
79-
DescriptionAttribute[] attributes = null;
80-
81-
if (fi != null)
82-
attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
83-
84-
if (attributes != null && attributes.Length > 0)
85-
return attributes[0].Description;
86-
else
87-
return value.ToString();
88-
}
8948
}
9049

9150
public class GriddlyColumn<TRow> : GriddlyColumn
@@ -141,9 +100,9 @@ public override object RenderCellValue(object row)
141100
else if (value is HelperResult)
142101
value = new HtmlString(((HelperResult)value).ToString());
143102
else if (value is Enum)
144-
value = ToStringDescription((Enum)value);
145-
else if (value != null && value.GetType().Name.Contains("M3DateTime"))
146-
value = (DateTime?)value;
103+
value = Extensions.ToStringDescription((Enum)value);
104+
else if (value != null && value.GetType().HasCastOperator<DateTime>())
105+
value = (DateTime)value;
147106

148107
return value;
149108
}
Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Reflection;
53
using System.Web.Mvc;
64

75
namespace Griddly.Mvc
@@ -17,53 +15,33 @@ public GriddlyDefaultParametersAttribute(string viewName)
1715
{
1816
ViewName = viewName;
1917
}
20-
18+
2119
public override void OnActionExecuting(ActionExecutingContext filterContext)
2220
{
2321
if (filterContext.IsChildAction)
2422
{
2523
if (filterContext.ActionDescriptor.GetFilterAttributes(true).Any(x => x.GetType() == typeof(GriddlyDefaultParametersAttribute)) ||
26-
typeof(GriddlyResult).IsAssignableFrom(GetExpectedReturnType(filterContext)))
24+
typeof(GriddlyResult).IsAssignableFrom(filterContext.GetExpectedReturnType()))
2725
{
2826
GriddlySettings settings = GriddlySettingsResult.GetSettings(filterContext.Controller.ControllerContext, ViewName);
2927

28+
foreach (GriddlyFilter filter in settings.Filters.Union(settings.Columns.Where(x => x.Filter != null).Select(x => x.Filter)))
29+
{
30+
if (filter.Default != null)
31+
settings.FilterDefaults[filter.Field] = filter.Default;
32+
33+
GriddlyFilterRange rangeFilter = filter as GriddlyFilterRange;
34+
35+
if (rangeFilter != null && rangeFilter.DefaultEnd != null)
36+
settings.FilterDefaults[rangeFilter.FieldEnd] = rangeFilter.DefaultEnd;
37+
}
38+
3039
foreach (KeyValuePair<string, object> param in settings.FilterDefaults)
3140
filterContext.ActionParameters[param.Key] = param.Value;
3241
}
3342
}
3443

3544
base.OnActionExecuting(filterContext);
3645
}
37-
38-
Type GetExpectedReturnType(ActionExecutingContext filterContext)
39-
{
40-
if (filterContext.ActionDescriptor is ReflectedActionDescriptor)
41-
return ((ReflectedActionDescriptor)filterContext.ActionDescriptor).MethodInfo.ReturnType;
42-
43-
// Find out what type is expected to be returned
44-
string actionName = filterContext.ActionDescriptor.ActionName;
45-
Type controllerType = filterContext.Controller.GetType();
46-
MethodInfo actionMethodInfo = default(MethodInfo);
47-
try
48-
{
49-
actionMethodInfo = controllerType.GetMethod(actionName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
50-
}
51-
catch (AmbiguousMatchException)
52-
{
53-
// Try to find a match using the parameters passed through
54-
var actionParams = filterContext.ActionParameters;
55-
List<Type> paramTypes = new List<Type>();
56-
57-
foreach (var p in actionParams)
58-
paramTypes.Add(p.Value.GetType());
59-
60-
actionMethodInfo = controllerType.GetMethod(actionName, paramTypes.ToArray());
61-
}
62-
63-
if (actionMethodInfo != null)
64-
return actionMethodInfo.ReturnType;
65-
else
66-
return null;
67-
}
6846
}
6947
}

Griddly.Mvc/GriddlyExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public static HtmlString AttributeNullable(this HtmlHelper helper, string name,
8686

8787
public static HtmlString AttributeIf(this HtmlHelper helper, string name, bool shouldShow, Func<object, object> value)
8888
{
89-
return helper.AttributeIf(name, shouldShow, value(null));
89+
if (shouldShow)
90+
return helper.AttributeIf(name, shouldShow, value(null));
91+
else
92+
return null;
9093
}
9194

9295
public static HtmlString AttributeIf(this HtmlHelper helper, string name, bool shouldShow, object value)

Griddly.Mvc/GriddlyFilter.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity.Design.PluralizationServices;
4+
using System.Globalization;
5+
using System.Web.Mvc;
6+
7+
namespace Griddly.Mvc
8+
{
9+
public abstract class GriddlyFilter
10+
{
11+
public string Name { get; set; }
12+
13+
public string NamePlural
14+
{
15+
get
16+
{
17+
return PluralizationService.CreateService(CultureInfo.CurrentUICulture).Pluralize(Name);
18+
}
19+
}
20+
21+
public string Field { get; set; }
22+
public object Default { get; set; }
23+
public virtual FilterDataType DataType { get; set; }
24+
25+
public string GetFormattedValue(object value)
26+
{
27+
return Extensions.GetFormattedValue(value, DataType);
28+
}
29+
30+
public string GetEditValue(object value)
31+
{
32+
FilterDataType dataType = DataType;
33+
34+
if (dataType == FilterDataType.Currency)
35+
dataType = FilterDataType.Decimal;
36+
37+
return Extensions.GetFormattedValue(value, dataType);
38+
}
39+
}
40+
41+
public class GriddlyFilterBox : GriddlyFilter
42+
{
43+
public GriddlyFilterBox()
44+
{
45+
DataType = FilterDataType.String;
46+
}
47+
}
48+
49+
public class GriddlyFilterRange : GriddlyFilter
50+
{
51+
public GriddlyFilterRange()
52+
{
53+
DataType = FilterDataType.Decimal;
54+
}
55+
56+
public override FilterDataType DataType
57+
{
58+
get
59+
{
60+
return base.DataType;
61+
}
62+
set
63+
{
64+
if (value == FilterDataType.String)
65+
throw new ArgumentOutOfRangeException("String is not a valid DataType for Range filter.");
66+
67+
base.DataType = value;
68+
}
69+
}
70+
public string FieldEnd { get; set; }
71+
public object DefaultEnd { get; set; }
72+
}
73+
74+
public class GriddlyFilterList : GriddlyFilter
75+
{
76+
public GriddlyFilterList()
77+
{
78+
IsMultiple = true;
79+
IsNoneAll = true;
80+
}
81+
82+
public List<SelectListItem> Items { get; set; }
83+
84+
public bool IsMultiple { get; set; }
85+
public bool IsNoneAll { get; set; }
86+
}
87+
88+
public enum FilterDataType
89+
{
90+
Other = 0,
91+
String = 1,
92+
Integer = 2,
93+
Decimal = 3,
94+
Currency = 4,
95+
Date = 5,
96+
//Percent = 6,
97+
}
98+
}

0 commit comments

Comments
 (0)