|
4 | 4 | using System.Text.RegularExpressions;
|
5 | 5 | using System.Web;
|
6 | 6 | using System.Web.Helpers;
|
| 7 | +using System.Web.Mvc; |
| 8 | +using System.Web.Routing; |
7 | 9 | using System.Web.WebPages;
|
8 | 10 |
|
9 | 11 | namespace Griddly.Mvc
|
10 | 12 | {
|
11 | 13 | public abstract class GriddlyColumn
|
12 | 14 | {
|
| 15 | + public GriddlyColumn() |
| 16 | + { |
| 17 | + HeaderHtmlAttributes = new RouteValueDictionary(); |
| 18 | + |
| 19 | + RenderMode = ColumnRenderMode.Both; |
| 20 | + } |
| 21 | + |
13 | 22 | public string Caption { get; set; }
|
14 | 23 | public string ExpressionString { get; set; }
|
15 | 24 | public string Format { get; set; }
|
16 | 25 | public SortDirection? DefaultSort { get; set; }
|
17 | 26 | public int DefaultSortOrder { get; set; }
|
18 | 27 | public string ClassName { get; set; }
|
19 | 28 | public string Width { get; set; }
|
20 |
| - public bool IsExportOnly { get; set; } |
| 29 | + public ColumnRenderMode RenderMode { get; set; } |
21 | 30 | public SummaryAggregateFunction? SummaryFunction { get; set; }
|
22 | 31 | public object SummaryValue { get; set; }
|
| 32 | + public IDictionary<string, object> HeaderHtmlAttributes { get; set; } |
23 | 33 |
|
24 | 34 | public GriddlyFilter Filter { get; set; }
|
25 | 35 |
|
| 36 | + public abstract HtmlString RenderUnderlyingValue(object row); |
26 | 37 | public abstract HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true);
|
27 | 38 | public abstract object RenderCellValue(object row, bool stripHtml = false);
|
28 |
| - public abstract string RenderClassName(object row, GriddlyResultPage page); |
| 39 | + |
| 40 | + public virtual string RenderClassName(object row, GriddlyResultPage page) |
| 41 | + { |
| 42 | + return ClassName; |
| 43 | + } |
| 44 | + |
| 45 | + public virtual IDictionary<string, object> GenerateHtmlAttributes(object row, GriddlyResultPage page) |
| 46 | + { |
| 47 | + return null; |
| 48 | + } |
29 | 49 |
|
30 | 50 | public virtual HtmlString RenderValue(object value, bool encode = true)
|
31 | 51 | {
|
@@ -59,7 +79,9 @@ public virtual HtmlString RenderValue(object value, bool encode = true)
|
59 | 79 | public class GriddlyColumn<TRow> : GriddlyColumn
|
60 | 80 | {
|
61 | 81 | public Func<TRow, object> Template { get; set; }
|
| 82 | + public Func<TRow, object> UnderlyingValueTemplate { get; set; } |
62 | 83 | public Func<TRow, string> ClassNameTemplate { get; set; }
|
| 84 | + public Func<TRow, object> HtmlAttributesTemplate { get; set; } |
63 | 85 |
|
64 | 86 | static readonly Regex _htmlMatch = new Regex(@"<[^>]*>", RegexOptions.Compiled);
|
65 | 87 |
|
@@ -89,6 +111,27 @@ public override string RenderClassName(object row, GriddlyResultPage page)
|
89 | 111 | return null;
|
90 | 112 | }
|
91 | 113 |
|
| 114 | + public override IDictionary<string, object> GenerateHtmlAttributes(object row, GriddlyResultPage page) |
| 115 | + { |
| 116 | + if (HtmlAttributesTemplate == null) |
| 117 | + return null; |
| 118 | + |
| 119 | + RouteValueDictionary attributes = new RouteValueDictionary(); |
| 120 | + |
| 121 | + object value = HtmlAttributesTemplate((TRow)row); |
| 122 | + |
| 123 | + if (value != null) |
| 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 | + |
| 132 | + return attributes; |
| 133 | + } |
| 134 | + |
92 | 135 | public override HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true)
|
93 | 136 | {
|
94 | 137 | object value = null;
|
@@ -116,6 +159,33 @@ public override HtmlString RenderCell(object row, GriddlySettings settings, bool
|
116 | 159 | return RenderValue(value, encode);
|
117 | 160 | }
|
118 | 161 |
|
| 162 | + public override HtmlString RenderUnderlyingValue(object row) |
| 163 | + { |
| 164 | + if (UnderlyingValueTemplate == null) return null; |
| 165 | + |
| 166 | + object value = null; |
| 167 | + |
| 168 | + try |
| 169 | + { |
| 170 | + value = UnderlyingValueTemplate((TRow)row); |
| 171 | + } |
| 172 | + catch (NullReferenceException) |
| 173 | + { |
| 174 | + // Eat |
| 175 | + } |
| 176 | + catch (Exception ex) |
| 177 | + { |
| 178 | + throw new InvalidOperationException("Error rendering underlying value or column \"" + Caption + "\"", ex); |
| 179 | + } |
| 180 | + |
| 181 | + if (value == null) |
| 182 | + return null; |
| 183 | + else if (value is HtmlString) |
| 184 | + return (HtmlString)value; |
| 185 | + else |
| 186 | + return new HtmlString(value.ToString()); |
| 187 | + } |
| 188 | + |
119 | 189 | public override object RenderCellValue(object row, bool stripHtml = false)
|
120 | 190 | {
|
121 | 191 | object value = null;
|
@@ -160,4 +230,11 @@ public enum SummaryAggregateFunction
|
160 | 230 | Min = 3,
|
161 | 231 | Max = 4
|
162 | 232 | }
|
| 233 | + |
| 234 | + public enum ColumnRenderMode |
| 235 | + { |
| 236 | + View = 1 << 0, |
| 237 | + Export = 1 << 1, |
| 238 | + Both = View | Export |
| 239 | + } |
163 | 240 | }
|
0 commit comments