Skip to content

Commit 5b8dcdd

Browse files
committed
Add HtmlAttributes configuration settings for root, table, column header, row, cell
1 parent 107a0f9 commit 5b8dcdd

File tree

7 files changed

+115
-18
lines changed

7 files changed

+115
-18
lines changed

Build/CommonAssemblyInfo.cs

+2-2
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.2.5")]
19-
[assembly: AssemblyFileVersion("1.2.5")]
18+
[assembly: AssemblyVersion("1.3.0")]
19+
[assembly: AssemblyFileVersion("1.3.0")]

Griddly.Mvc/GriddlyColumn.cs

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
using System.Text.RegularExpressions;
55
using System.Web;
66
using System.Web.Helpers;
7+
using System.Web.Mvc;
8+
using System.Web.Routing;
79
using System.Web.WebPages;
810

911
namespace Griddly.Mvc
1012
{
1113
public abstract class GriddlyColumn
1214
{
15+
public GriddlyColumn()
16+
{
17+
HeaderHtmlAttributes = new RouteValueDictionary();
18+
}
19+
1320
public string Caption { get; set; }
1421
public string ExpressionString { get; set; }
1522
public string Format { get; set; }
@@ -20,12 +27,22 @@ public abstract class GriddlyColumn
2027
public bool IsExportOnly { get; set; }
2128
public SummaryAggregateFunction? SummaryFunction { get; set; }
2229
public object SummaryValue { get; set; }
30+
public IDictionary<string, object> HeaderHtmlAttributes { get; set; }
2331

2432
public GriddlyFilter Filter { get; set; }
2533

2634
public abstract HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true);
2735
public abstract object RenderCellValue(object row, bool stripHtml = false);
28-
public abstract string RenderClassName(object row, GriddlyResultPage page);
36+
37+
public virtual string RenderClassName(object row, GriddlyResultPage page)
38+
{
39+
return ClassName;
40+
}
41+
42+
public virtual IDictionary<string, object> GenerateHtmlAttributes(object row, GriddlyResultPage page)
43+
{
44+
return null;
45+
}
2946

3047
public virtual HtmlString RenderValue(object value, bool encode = true)
3148
{
@@ -60,6 +77,7 @@ public class GriddlyColumn<TRow> : GriddlyColumn
6077
{
6178
public Func<TRow, object> Template { get; set; }
6279
public Func<TRow, string> ClassNameTemplate { get; set; }
80+
public Func<TRow, object> HtmlAttributesTemplate { get; set; }
6381

6482
static readonly Regex _htmlMatch = new Regex(@"<[^>]*>", RegexOptions.Compiled);
6583

@@ -89,6 +107,27 @@ public override string RenderClassName(object row, GriddlyResultPage page)
89107
return null;
90108
}
91109

110+
public override IDictionary<string, object> GenerateHtmlAttributes(object row, GriddlyResultPage page)
111+
{
112+
if (HtmlAttributesTemplate == null)
113+
return null;
114+
115+
RouteValueDictionary attributes = new RouteValueDictionary();
116+
117+
object value = HtmlAttributesTemplate((TRow)row);
118+
119+
if (value != null)
120+
{
121+
if (!(value is IDictionary<string, object>))
122+
value = HtmlHelper.AnonymousObjectToHtmlAttributes(value);
123+
124+
foreach (KeyValuePair<string, object> entry in (IDictionary<string, object>)value)
125+
attributes.Add(entry.Key, entry.Value);
126+
}
127+
128+
return attributes;
129+
}
130+
92131
public override HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true)
93132
{
94133
object value = null;

Griddly.Mvc/GriddlyExtensions.cs

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Text;
67
using System.Web;
78
using System.Web.Mvc;
89
using System.Web.Mvc.Html;
@@ -104,6 +105,22 @@ public static HtmlString AttributeIf(this HtmlHelper helper, string name, bool s
104105
return null;
105106
}
106107

108+
// http://stackoverflow.com/a/18618808/8037
109+
public static IHtmlString ToHtmlAttributes(this IDictionary<string, object> dictionary)
110+
{
111+
if (dictionary == null || dictionary.Count == 0)
112+
return null;
113+
114+
var sb = new StringBuilder();
115+
116+
foreach (var kvp in dictionary)
117+
{
118+
sb.Append(string.Format("{0}=\"{1}\" ", HttpUtility.HtmlEncode(kvp.Key), HttpUtility.HtmlAttributeEncode(kvp.Value != null ? kvp.Value.ToString() : null)));
119+
}
120+
121+
return new HtmlString(sb.ToString());
122+
}
123+
107124
public static void SetGriddlyDefault<T>(this Controller controller, ref T parameter, string field, T value)
108125
{
109126
if (controller.ControllerContext.IsChildAction)

Griddly.Mvc/GriddlySelectColumn.cs

-5
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,5 @@ public override object RenderCellValue(object row, bool stripHtml = false)
5252
{
5353
return null;
5454
}
55-
56-
public override string RenderClassName(object row, GriddlyResultPage page)
57-
{
58-
return ClassName;
59-
}
6055
}
6156
}

Griddly.Mvc/GriddlySettings.cs

+49-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Web;
88
using System.Web.Helpers;
99
using System.Web.Mvc;
10+
using System.Web.Routing;
1011

1112
namespace Griddly.Mvc
1213
{
@@ -45,6 +46,8 @@ public GriddlySettings()
4546
Filters = new List<GriddlyFilter>();
4647
Buttons = new List<GriddlyButton>();
4748
RowIds = new Dictionary<string, Func<object, object>>();
49+
HtmlAttributes = new RouteValueDictionary();
50+
TableHtmlAttributes = new RouteValueDictionary();
4851

4952
ClassName = DefaultClassName;
5053
TableClassName = DefaultTableClassName;
@@ -64,6 +67,8 @@ public GriddlySettings()
6467
public FilterMode? AllowedFilterModes { get; set; }
6568
public FilterMode? InitialFilterMode { get; set; }
6669
public bool ShowRowSelectCount { get; set; }
70+
public IDictionary<string, object> HtmlAttributes { get; set; }
71+
public IDictionary<string, object> TableHtmlAttributes { get; set; }
6772

6873
public int? PageSize { get; set; }
6974
public int? MaxPageSize { get; set; }
@@ -80,6 +85,7 @@ public GriddlySettings()
8085
public Func<object, object> RowClickUrl { get; set; }
8186
public string RowClickModal { get; set; }
8287
public Func<object, object> RowClass { get; set; }
88+
public Func<object, object> RowHtmlAttributes { get; set; }
8389

8490
public Func<GriddlyResultPage, object> FooterTemplate { get; set; }
8591
public Func<GriddlyResultPage, object> HeaderTemplate { get; set; }
@@ -106,6 +112,29 @@ public virtual object RenderRowClass(object o)
106112
return RowClass(o);
107113
}
108114

115+
public virtual IDictionary<string, object> GenerateRowHtmlAttributes(object o)
116+
{
117+
if (RowHtmlAttributes != null)
118+
{
119+
object value = RowHtmlAttributes(o);
120+
121+
if (value != null)
122+
{
123+
RouteValueDictionary attributes = new RouteValueDictionary();
124+
125+
if (!(value is IDictionary<string, object>))
126+
value = HtmlHelper.AnonymousObjectToHtmlAttributes(value);
127+
128+
foreach (KeyValuePair<string, object> entry in (IDictionary<string, object>)value)
129+
attributes.Add(entry.Key, entry.Value);
130+
131+
return attributes;
132+
}
133+
}
134+
135+
return null;
136+
}
137+
109138
public GriddlySettings RowId(Expression<Func<object, object>> expression, string name = null)
110139
{
111140
if (name == null)
@@ -339,6 +368,17 @@ public class GriddlySettings<TRow> : GriddlySettings
339368
}
340369
}
341370

371+
public new Func<TRow, object> RowHtmlAttributes
372+
{
373+
set
374+
{
375+
if (value != null)
376+
base.RowHtmlAttributes = (x) => value((TRow)x);
377+
else
378+
base.RowHtmlAttributes = null;
379+
}
380+
}
381+
342382
public GriddlySettings<TRow> RowId(Expression<Func<TRow, object>> expression, string name = null)
343383
{
344384
if (name == null)
@@ -352,7 +392,7 @@ public GriddlySettings<TRow> RowId(Expression<Func<TRow, object>> expression, st
352392
return this;
353393
}
354394

355-
public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> expression, string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, bool isExportOnly = false, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<GriddlyColumn, GriddlyFilter> filter = null, int defaultSortOrder = 0)
395+
public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> expression, string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, bool isExportOnly = false, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<GriddlyColumn, GriddlyFilter> filter = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0)
356396
{
357397
ModelMetadata metadata = null;
358398

@@ -401,6 +441,9 @@ public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>>
401441
if (string.IsNullOrWhiteSpace(expressionString) && summaryFunction != null)
402442
throw new InvalidOperationException("Must specify an expression to use a summary function.");
403443

444+
if (headerHtmlAttributes != null && !(headerHtmlAttributes is IDictionary<string, object>))
445+
headerHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(headerHtmlAttributes);
446+
404447
Add(new GriddlyColumn<TRow>()
405448
{
406449
Template = template,
@@ -413,15 +456,17 @@ public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>>
413456
DefaultSortOrder = defaultSortOrder,
414457
ClassName = className,
415458
IsExportOnly = isExportOnly,
416-
Width = width
459+
Width = width,
460+
HtmlAttributesTemplate = htmlAttributes,
461+
HeaderHtmlAttributes = (IDictionary<string, object>)headerHtmlAttributes
417462
}, filter);
418463

419464
return this;
420465
}
421466

422-
public GriddlySettings<TRow> Column(string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, bool isExportOnly = false, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<GriddlyColumn, GriddlyFilter> filter = null, int defaultSortOrder = 0)
467+
public GriddlySettings<TRow> Column(string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, bool isExportOnly = false, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<GriddlyColumn, GriddlyFilter> filter = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0)
423468
{
424-
return Column<object>(null, caption, format, expressionString, defaultSort, className, isExportOnly, width, summaryFunction, summaryValue, template, filter, defaultSortOrder);
469+
return Column<object>(null, caption, format, expressionString, defaultSort, className, isExportOnly, width, summaryFunction, summaryValue, template, filter, htmlAttributes, headerHtmlAttributes, defaultSortOrder);
425470
}
426471

427472
public GriddlySettings<TRow> SelectColumn(Expression<Func<TRow, object>> id, object summaryValue = null)

Griddly.Mvc/InternalExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,5 @@ internal static string GetFormattedValue(object value, FilterDataType dataType)
191191

192192
return output;
193193
}
194-
195194
}
196195
}

Griddly/Views/Shared/Griddly/Griddly.cshtml

+7-5
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
@: @Html.AttributeNullable("data-griddly-rowclickmodal", settings.RowClickModal)
9898
@: data-griddly-defaultrowids="@Json.Encode(settings.DefaultRowIds != null ? settings.DefaultRowIds.Select(x => x.ToLower()).ToArray() : new[] { "value" })"
9999
@: data-griddly-filter-defaults="@Newtonsoft.Json.JsonConvert.SerializeObject(this.GetGriddlyDefaults())"
100-
@: @Html.AttributeIf("data-griddly-defaultsort", defaultSort != null && defaultSort.Any(), Html.AttributeEncode(Json.Encode(defaultSort.Select(x => new { Field = x.Field, Direction = x.Direction.ToString() }))))>
100+
@: @Html.AttributeIf("data-griddly-defaultsort", defaultSort != null && defaultSort.Any(), Html.AttributeEncode(Json.Encode(defaultSort.Select(x => new { Field = x.Field, Direction = x.Direction.ToString() }))))
101+
@: @settings.HtmlAttributes.ToHtmlAttributes()>
101102
if (settings.Buttons.Any())
102103
{
103104
<div class="buttons">
@@ -112,7 +113,7 @@
112113
@settings.BeforeTemplate(null)
113114
}
114115
@:<div class="griddly-scrollable-container">
115-
@:<table class="@settings.TableClassName">
116+
@:<table class="@settings.TableClassName" @settings.TableHtmlAttributes.ToHtmlAttributes()>
116117
<colgroup>
117118
@foreach (GriddlyColumn column in settings.Columns)
118119
{
@@ -150,7 +151,7 @@
150151
@foreach (GriddlyColumn column in settings.Columns)
151152
{
152153
bool isSortable = !simple && !string.IsNullOrWhiteSpace(column.ExpressionString);
153-
<th class="@column.ClassName @(isSortable ? "sortable" : null) @(column is GriddlySelectColumn ? "select" : null) @(column.DefaultSort != null ? (column.DefaultSort == SortDirection.Descending ? "sorted_d" : "sorted_a") : null)" @Html.AttributeNullable("data-griddly-sortfield", simple ? null : column.ExpressionString)>
154+
<th class="@column.ClassName @(isSortable ? "sortable" : null) @(column is GriddlySelectColumn ? "select" : null) @(column.DefaultSort != null ? (column.DefaultSort == SortDirection.Descending ? "sorted_d" : "sorted_a") : null)" @Html.AttributeNullable("data-griddly-sortfield", simple ? null : column.ExpressionString) @column.HeaderHtmlAttributes.ToHtmlAttributes()>
154155
@if (isSortable)
155156
{
156157
<text>@column.Caption<span class="icon">&nbsp;</span></text>
@@ -190,10 +191,11 @@
190191
{
191192
@: data-rowid-@(x.Key)="@(x.Value(row))"
192193
}
193-
data-rowkey="@("_" + string.Join("_", settings.RowIds.Select(x => { var val = x.Value(row); return val == null ? "" : val.ToString(); }).ToArray()))">
194+
data-rowkey="@("_" + string.Join("_", settings.RowIds.Select(x => { var val = x.Value(row); return val == null ? "" : val.ToString(); }).ToArray()))"
195+
@settings.GenerateRowHtmlAttributes(row).ToHtmlAttributes()>
194196
@foreach (GriddlyColumn column in settings.Columns)
195197
{
196-
<td class="@column.RenderClassName(row, Model)">@column.RenderCell(row, settings)</td>
198+
<td class="@column.RenderClassName(row, Model)" @column.GenerateHtmlAttributes(row, Model).ToHtmlAttributes()>@column.RenderCell(row, settings)</td>
197199
}
198200
</tr>
199201
}

0 commit comments

Comments
 (0)