Skip to content

Commit 825bfcb

Browse files
committed
Global column value filter
1 parent 11a6e6a commit 825bfcb

File tree

9 files changed

+122
-23
lines changed

9 files changed

+122
-23
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
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("3.4.9")]
19-
[assembly: AssemblyFileVersion("3.4.9")]
18+
[assembly: AssemblyVersion("3.5.0")]
19+
[assembly: AssemblyFileVersion("3.5.0")]
2020
//[assembly: AssemblyInformationalVersion("2.5-filters")]

Griddly.Mvc/GriddlyColumn.cs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
using System.Web.WebPages;
1212
#else
1313
using Microsoft.AspNetCore.Html;
14+
using Microsoft.AspNetCore.Http;
1415
using Microsoft.AspNetCore.Mvc.Razor;
16+
using Microsoft.AspNetCore.Mvc.Rendering;
1517
using Microsoft.AspNetCore.Mvc.ViewFeatures;
1618
using Microsoft.AspNetCore.Routing;
1719
#endif
@@ -63,9 +65,29 @@ string GetIdFromExpression(LambdaExpression expression)
6365

6466
public GriddlyFilter Filter { get; set; }
6567

66-
public abstract HtmlString RenderUnderlyingValue(object row);
67-
public abstract HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true);
68-
public abstract object RenderCellValue(object row, bool stripHtml = false);
68+
public abstract HtmlString RenderUnderlyingValue(object row,
69+
#if NET45
70+
HtmlHelper html
71+
#else
72+
IHtmlHelper html
73+
#endif
74+
);
75+
76+
public abstract HtmlString RenderCell(object row, GriddlySettings settings,
77+
#if NET45
78+
HtmlHelper html,
79+
#else
80+
IHtmlHelper html,
81+
#endif
82+
bool encode = true);
83+
84+
public abstract object RenderCellValue(object row,
85+
#if NET45
86+
HttpContextBase httpContext,
87+
#else
88+
HttpContext httpContext,
89+
#endif
90+
bool stripHtml = false);
6991

7092
public virtual string RenderClassName(object row, GriddlyResultPage page)
7193
{
@@ -77,11 +99,20 @@ public virtual IDictionary<string, object> GenerateHtmlAttributes(object row, Gr
7799
return null;
78100
}
79101

80-
public virtual HtmlString RenderValue(object value, bool encode = true)
102+
public virtual HtmlString RenderValue(object value,
103+
#if NET45
104+
HtmlHelper html,
105+
#else
106+
IHtmlHelper html,
107+
#endif
108+
bool encode = true)
81109
{
82110
if (value == null)
83111
return null;
84112

113+
if (GriddlySettings.ColumnValueFilter != null)
114+
value = GriddlySettings.ColumnValueFilter.Filter(this, value, html.ViewContext.HttpContext);
115+
85116
if (Format == null)
86117
{
87118
if (value is DateTime || value is DateTime? || value.GetType().HasCastOperator<DateTime>())
@@ -169,7 +200,13 @@ public override IDictionary<string, object> GenerateHtmlAttributes(object row, G
169200
return attributes;
170201
}
171202

172-
public override HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true)
203+
public override HtmlString RenderCell(object row, GriddlySettings settings,
204+
#if NET45
205+
HtmlHelper html,
206+
#else
207+
IHtmlHelper html,
208+
#endif
209+
bool encode = true)
173210
{
174211
object value = null;
175212

@@ -199,8 +236,8 @@ public override HtmlString RenderCell(object row, GriddlySettings settings, bool
199236
valueString = ((HelperResult)value).ToString();
200237
else
201238
{
202-
if (LinkUrl == null) return RenderValue(value, encode); //Return directly, to avoid converting to string and back to HtmlString unnecessarily
203-
else valueString = RenderValue(value, encode)?.ToHtmlString();
239+
if (LinkUrl == null) return RenderValue(value, html, encode); //Return directly, to avoid converting to string and back to HtmlString unnecessarily
240+
else valueString = RenderValue(value, html, encode)?.ToHtmlString();
204241
}
205242

206243
if (row != null && LinkUrl != null && !string.IsNullOrWhiteSpace(valueString))
@@ -213,7 +250,13 @@ public override HtmlString RenderCell(object row, GriddlySettings settings, bool
213250
return new HtmlString(valueString);
214251
}
215252

216-
public override HtmlString RenderUnderlyingValue(object row)
253+
public override HtmlString RenderUnderlyingValue(object row,
254+
#if NET45
255+
HtmlHelper html
256+
#else
257+
IHtmlHelper html
258+
#endif
259+
)
217260
{
218261
if (UnderlyingValueTemplate == null) return null;
219262

@@ -232,6 +275,9 @@ public override HtmlString RenderUnderlyingValue(object row)
232275
throw new InvalidOperationException("Error rendering underlying value or column \"" + Caption + "\"", ex);
233276
}
234277

278+
if (GriddlySettings.ColumnValueFilter != null)
279+
value = GriddlySettings.ColumnValueFilter.Filter(this, value, html.ViewContext.HttpContext);
280+
235281
if (value == null)
236282
return null;
237283
else if (value is HtmlString)
@@ -240,7 +286,13 @@ public override HtmlString RenderUnderlyingValue(object row)
240286
return new HtmlString(value.ToString());
241287
}
242288

243-
public override object RenderCellValue(object row, bool stripHtml = false)
289+
public override object RenderCellValue(object row,
290+
#if NET45
291+
HttpContextBase httpContext,
292+
#else
293+
HttpContext httpContext,
294+
#endif
295+
bool stripHtml = false)
244296
{
245297
object value = null;
246298

@@ -257,6 +309,8 @@ public override object RenderCellValue(object row, bool stripHtml = false)
257309
throw new InvalidOperationException("Error rendering column \"" + Caption + "\"", ex);
258310
}
259311

312+
if (GriddlySettings.ColumnValueFilter != null)
313+
value = GriddlySettings.ColumnValueFilter.Filter(this, value, httpContext);
260314

261315
// TODO: test if we need to match separately -- maybe we get a real string here and could strip?
262316
if (value is HelperResult)

Griddly.Mvc/GriddlyCsvResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public override async Task ExecuteResultAsync(ActionContext context)
6767
{
6868
for (int x = 0; x < columns.Count; x++)
6969
{
70-
object renderedValue = columns[x].RenderCellValue(row, true);
70+
object renderedValue = columns[x].RenderCellValue(row, context.HttpContext, true);
7171

7272
w.WriteField(renderedValue);
7373
}

Griddly.Mvc/GriddlyExcelResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public override async Task ExecuteResultAsync(ActionContext context)
6767
{
6868
for (int x = 0; x < columns.Count; x++)
6969
{
70-
object renderedValue = columns[x].RenderCellValue(row, true);
70+
object renderedValue = columns[x].RenderCellValue(row, context.HttpContext, true);
7171

7272
ExcelRange cell = ws.Cells[y + 2, x + 1];
7373

Griddly.Mvc/GriddlySelectColumn.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Web.Mvc;
1010
using System.Web.Routing;
1111
#else
12+
using Microsoft.AspNetCore.Http;
1213
using Microsoft.AspNetCore.Html;
1314
using Microsoft.AspNetCore.Mvc.ViewFeatures;
1415
using Microsoft.AspNetCore.Mvc.Rendering;
@@ -34,7 +35,13 @@ public virtual IDictionary<string, object> GenerateInputHtmlAttributes(object ro
3435
return null;
3536
}
3637

37-
public override HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true)
38+
public override HtmlString RenderCell(object row, GriddlySettings settings,
39+
#if NET45
40+
HtmlHelper html,
41+
#else
42+
IHtmlHelper html,
43+
#endif
44+
bool encode = true)
3845
{
3946
if (IsRowSelectable?.Invoke(row) != false)
4047
{
@@ -89,12 +96,24 @@ public override HtmlString RenderCell(object row, GriddlySettings settings, bool
8996
return null;
9097
}
9198

92-
public override object RenderCellValue(object row, bool stripHtml = false)
99+
public override object RenderCellValue(object row,
100+
#if NET45
101+
HttpContextBase httpContext,
102+
#else
103+
HttpContext httpContext,
104+
#endif
105+
bool stripHtml = false)
93106
{
94107
return null;
95108
}
96109

97-
public override HtmlString RenderUnderlyingValue(object row)
110+
public override HtmlString RenderUnderlyingValue(object row,
111+
#if NET45
112+
HtmlHelper html
113+
#else
114+
IHtmlHelper html
115+
#endif
116+
)
98117
{
99118
return null;
100119
}

Griddly.Mvc/GriddlySettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public abstract class GriddlySettings : IGriddlyFilterSettings
5151
public static Func<GriddlyResultPage, object> DefaultFooterTemplate = null;
5252
public static Func<GriddlyResultPage, object> DefaultHeaderTemplate = null;
5353
public static Func<IEnumerable, GriddlySettings, IEnumerable> OnGriddlyExportExecuting = null;
54-
54+
public static IGriddlyColumnValueFilter ColumnValueFilter = null;
5555

5656
#if NET45
5757
/// <summary>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Web;
5+
6+
#if NET45
7+
using System.Web.Mvc;
8+
#else
9+
using Microsoft.AspNetCore.Html;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Mvc.Rendering;
12+
#endif
13+
14+
namespace Griddly.Mvc
15+
{
16+
public interface IGriddlyColumnValueFilter
17+
{
18+
object Filter(GriddlyColumn column, object value,
19+
#if NET45
20+
HttpContextBase httpContext
21+
#else
22+
HttpContext httpContext
23+
#endif
24+
);
25+
}
26+
}

Griddly.NetCore/Views/Shared/Griddly/Griddly.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@
318318
@settings.GenerateRowHtmlAttributes(row).ToHtmlAttributes()>
319319
@foreach (GriddlyColumn column in settings.Columns)
320320
{
321-
var underlyingValue = column.RenderUnderlyingValue(row);
322-
<td class="@column.RenderClassName(row, Model) @(!column.Visible ? "column-hidden" : null)" @column.GenerateHtmlAttributes(row, Model).ToHtmlAttributes() @if (underlyingValue != null) { <text>data-value="@underlyingValue"</text> }>@column.RenderCell(row, settings)</td>
321+
var underlyingValue = column.RenderUnderlyingValue(row, Html);
322+
<td class="@column.RenderClassName(row, Model) @(!column.Visible ? "column-hidden" : null)" @column.GenerateHtmlAttributes(row, Model).ToHtmlAttributes() @if (underlyingValue != null) { <text>data-value="@underlyingValue"</text> }>@column.RenderCell(row, settings, Html)</td>
323323
}
324324
</tr>
325325
}
@@ -330,7 +330,7 @@
330330
<tr>
331331
@foreach (GriddlyColumn column in settings.Columns)
332332
{
333-
<td class="@column.ClassName @(!column.Visible ? "column-hidden" : null)">@column.RenderValue(column.SummaryValue)</td>
333+
<td class="@column.ClassName @(!column.Visible ? "column-hidden" : null)">@column.RenderValue(column.SummaryValue, Html)</td>
334334
}
335335
</tr>
336336
</tfoot>

Griddly/Views/Shared/Griddly/Griddly.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@
303303
@settings.GenerateRowHtmlAttributes(row).ToHtmlAttributes()>
304304
@foreach (GriddlyColumn column in settings.Columns)
305305
{
306-
var underlyingValue = column.RenderUnderlyingValue(row);
307-
<td class="@column.RenderClassName(row, Model) @(!column.Visible ? "column-hidden" : null)" @column.GenerateHtmlAttributes(row, Model).ToHtmlAttributes() @if (underlyingValue != null) { <text>data-value="@underlyingValue"</text> }>@column.RenderCell(row, settings)</td>
306+
var underlyingValue = column.RenderUnderlyingValue(row, Html);
307+
<td class="@column.RenderClassName(row, Model) @(!column.Visible ? "column-hidden" : null)" @column.GenerateHtmlAttributes(row, Model).ToHtmlAttributes() @if (underlyingValue != null) { <text>data-value="@underlyingValue"</text> }>@column.RenderCell(row, settings, Html)</td>
308308
}
309309
</tr>
310310
}
@@ -315,7 +315,7 @@
315315
<tr>
316316
@foreach (GriddlyColumn column in settings.Columns)
317317
{
318-
<td class="@column.ClassName @(!column.Visible ? "column-hidden" : null)">@column.RenderValue(column.SummaryValue)</td>
318+
<td class="@column.ClassName @(!column.Visible ? "column-hidden" : null)">@column.RenderValue(column.SummaryValue, Html)</td>
319319
}
320320
</tr>
321321
</tfoot>

0 commit comments

Comments
 (0)