Skip to content

Commit 9ec3a73

Browse files
committed
Support for column visibility
1 parent 423f4e6 commit 9ec3a73

12 files changed

+73
-24
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.2.16")]
19-
[assembly: AssemblyFileVersion("3.2.16")]
18+
[assembly: AssemblyVersion("3.2.17")]
19+
[assembly: AssemblyFileVersion("3.2.17")]
2020
//[assembly: AssemblyInformationalVersion("2.5-filters")]

Griddly.Mvc/GriddlyColumn.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Linq.Expressions;
45
using System.Text.RegularExpressions;
56
using System.Web;
67
using System.Web.Helpers;
@@ -15,10 +16,29 @@ public abstract class GriddlyColumn
1516
public GriddlyColumn()
1617
{
1718
HeaderHtmlAttributes = new RouteValueDictionary();
18-
1919
RenderMode = ColumnRenderMode.Both;
2020
}
2121

22+
public GriddlyColumn(LambdaExpression expression, string caption, string columnId) : this()
23+
{
24+
Expression = expression;
25+
Caption = caption;
26+
ColumnId = columnId != null ? columnId : expression != null ? GetIdFromExpression(expression) : Caption;
27+
}
28+
29+
string GetIdFromExpression(LambdaExpression expression)
30+
{
31+
string expressionString = expression.Body.ToString();
32+
33+
//trim the parameter for a cleaner expression
34+
string parameter = expression.Parameters.Single().Name;
35+
if (expressionString.StartsWith(parameter + "."))
36+
return expressionString.Substring(parameter.Length + 1);
37+
else
38+
return expressionString;
39+
}
40+
41+
public LambdaExpression Expression { get; protected set; }
2242
public string Caption { get; set; }
2343
public string ExpressionString { get; set; }
2444
public string Format { get; set; }
@@ -28,6 +48,8 @@ public GriddlyColumn()
2848
public string Width { get; set; }
2949
public double? ExportWidth { get; set; }
3050
public ColumnRenderMode RenderMode { get; set; }
51+
public bool Visible { get; set; } = true;
52+
public string ColumnId { get; set; }
3153
public SummaryAggregateFunction? SummaryFunction { get; set; }
3254
public object SummaryValue { get; set; }
3355
public IDictionary<string, object> HeaderHtmlAttributes { get; set; }
@@ -68,6 +90,7 @@ public virtual HtmlString RenderValue(object value, bool encode = true)
6890

6991
if (value == null)
7092
return null;
93+
7194
if (value is HtmlString)
7295
return (HtmlString)value;
7396
else if (encode)
@@ -79,6 +102,8 @@ public virtual HtmlString RenderValue(object value, bool encode = true)
79102

80103
public class GriddlyColumn<TRow> : GriddlyColumn
81104
{
105+
public GriddlyColumn(LambdaExpression expression, string caption, string columnId) : base(expression, caption, columnId) { }
106+
82107
public Func<TRow, object> Template { get; set; }
83108
public Func<TRow, object> UnderlyingValueTemplate { get; set; }
84109
public Func<TRow, string> ClassNameTemplate { get; set; }

Griddly.Mvc/GriddlyCsvResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override void ExecuteResult(ControllerContext context)
3838
if (export != null && export.UseGridColumns)
3939
columns.InsertRange(0, _settings.Columns);
4040

41-
columns.RemoveAll(x => !x.RenderMode.HasFlag(ColumnRenderMode.Export));
41+
columns.RemoveAll(x => !x.Visible || !x.RenderMode.HasFlag(ColumnRenderMode.Export));
4242

4343
for (int i = 0; i < columns.Count; i++)
4444
w.WriteField(columns[i].Caption);

Griddly.Mvc/GriddlyExcelResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override void ExecuteResult(ControllerContext context)
4141
if (export != null && export.UseGridColumns)
4242
columns.InsertRange(0, _settings.Columns);
4343

44-
columns.RemoveAll(x => !x.RenderMode.HasFlag(ColumnRenderMode.Export));
44+
columns.RemoveAll(x => !x.Visible || !x.RenderMode.HasFlag(ColumnRenderMode.Export));
4545

4646
for (int i = 0; i < columns.Count; i++)
4747
{

Griddly.Mvc/GriddlyExport.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public GriddlyExport(string name, bool useGridColumns = false)
2929
: base(name, useGridColumns)
3030
{
3131
}
32-
public GriddlyExport<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> expression, string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, ColumnRenderMode renderMode = ColumnRenderMode.Both, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null, double? exportWidth = null)
32+
public GriddlyExport<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> expression, string caption = null, string format = null, string expressionString = null, SortDirection? defaultSort = null, string className = null, ColumnRenderMode renderMode = ColumnRenderMode.Both, string width = null, SummaryAggregateFunction? summaryFunction = null, object summaryValue = null, Func<TRow, object> template = null, Func<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null, double? exportWidth = null, bool visible = true, string columnId = null)
3333
{
3434
ModelMetadata metadata = null;
3535

@@ -64,10 +64,10 @@ public GriddlyExport<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>> e
6464
headerHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(headerHtmlAttributes);
6565

6666
var valueTemplate = value == null ? null : value.Compile();
67-
Columns.Add(new GriddlyColumn<TRow>()
67+
Columns.Add(new GriddlyColumn<TRow>(expression, caption, columnId)
6868
{
69+
Visible = visible,
6970
Template = template,
70-
Caption = caption,
7171
Format = format,
7272
ExpressionString = expressionString,
7373
SummaryFunction = summaryFunction,

Griddly.Mvc/GriddlyExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,10 @@ public static GriddlyColumn<TRow> GriddlyColumnFor<TRow>(this HtmlHelper<IEnumer
125125
return htmlHelper.GriddlyColumnFor<TRow>(template, null);
126126
}
127127

128-
public static GriddlyColumn<TRow> GriddlyColumnFor<TRow>(this HtmlHelper<IEnumerable<TRow>> htmlHelper, Func<TRow, object> template, string caption)
128+
public static GriddlyColumn<TRow> GriddlyColumnFor<TRow>(this HtmlHelper<IEnumerable<TRow>> htmlHelper, Func<TRow, object> template, string caption, string columnId = null)
129129
{
130-
return new GriddlyColumn<TRow>()
130+
return new GriddlyColumn<TRow>(null, caption, columnId)
131131
{
132-
Caption = caption,
133132
Template = template
134133
};
135134
}

Griddly.Mvc/GriddlySelectColumn.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Griddly.Mvc
1010
{
1111
public class GriddlySelectColumn : GriddlyColumn
1212
{
13+
public GriddlySelectColumn() : base() { }
14+
1315
public Func<object, bool> IsRowSelectable { get; set; }
1416

1517
public GriddlySelectColumn(GriddlySettings settings)

Griddly.Mvc/GriddlySettings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public GriddlySettings<TRow> RowId(Expression<Func<TRow, object>> expression, st
448448
return this;
449449
}
450450

451-
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, ColumnRenderMode renderMode = ColumnRenderMode.Both, 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, Expression<Func<TRow, object>> value = null, double? exportWidth = null, Func<TRow, string> linkUrl = null)
451+
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, ColumnRenderMode renderMode = ColumnRenderMode.Both, 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, Expression<Func<TRow, object>> value = null, double? exportWidth = null, Func<TRow, string> linkUrl = null, bool visible = true, string columnId = null)
452452
{
453453
ModelMetadata metadata = null;
454454

@@ -501,10 +501,10 @@ public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>>
501501
headerHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(headerHtmlAttributes);
502502

503503
var valueTemplate = value == null ? null : value.Compile();
504-
Add(new GriddlyColumn<TRow>()
504+
Add(new GriddlyColumn<TRow>(expression, caption, columnId)
505505
{
506+
Visible = visible,
506507
Template = template,
507-
Caption = caption,
508508
Format = format,
509509
ExpressionString = expressionString,
510510
SummaryFunction = summaryFunction,

Griddly/Content/griddly.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,8 @@
248248
.griddly-selection-count
249249
{
250250
font-weight:bold;
251-
}
251+
}
252+
253+
.griddly .column-hidden{
254+
display:none;
255+
}

Griddly/Scripts/griddly.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,25 @@
17881788
}, this));
17891789
},
17901790

1791+
toggleColumnVisible: function (columnId, visible) {
1792+
var colElement = $("colgroup col[data-columnid='" + columnId + "']", this.$element);
1793+
var colIdx = colElement.index();
1794+
1795+
if (typeof visible === 'undefined')
1796+
visible = colElement.hasClass("column-hidden");
1797+
1798+
var prevColspan = $("colgroup>col:not(.column-hidden)", this.$element).length;
1799+
colElement.toggleClass("column-hidden", !visible)
1800+
var colspan = $("colgroup>col:not(.column-hidden)", this.$element).length;
1801+
1802+
$(">table>>tr", this.$element.find(".griddly-scrollable-container")).each(function () {
1803+
$(">:eq(" + colIdx + "):not([colspan])", $(this)).toggleClass("column-hidden", !visible);
1804+
$(">[colspan='" + prevColspan + "']", $(this)).attr("colspan", colspan);
1805+
});
1806+
1807+
return visible;
1808+
},
1809+
17911810
getSelected: function (arrayIdNames)
17921811
{
17931812
if (arrayIdNames === "all")

Griddly/Views/Shared/Griddly/Griddly.cshtml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@
207207
<colgroup>
208208
@foreach (GriddlyColumn column in settings.Columns)
209209
{
210-
<col @Html.AttributeIf("style", column.Width != null, "width:" + column.Width) data-field="@Griddly.Mvc.GriddlyFilterExtensions.GetField(column)" />
210+
<col @Html.AttributeIf("style", column.Width != null, "width:" + column.Width) data-field="@Griddly.Mvc.GriddlyFilterExtensions.GetField(column)" @Html.AttributeNullable("data-columnid", column.ColumnId) @Html.AttributeIf("class", !column.Visible, "column-hidden") />
211211
}
212212
</colgroup>
213213
<thead>
214214
@if (!string.IsNullOrWhiteSpace(settings.Title))
215215
{
216216
<tr class="header">
217-
<td colspan="@settings.Columns.Count">@settings.Title
217+
<td colspan="@settings.Columns.Where(x => x.Visible).Count()">@settings.Title
218218

219219
@if (settings.ShowRowSelectCount)
220220
{
@@ -229,7 +229,7 @@
229229
@if (settings.AllowedFilterModes != FilterMode.None && settings.AllowedFilterModes.Value.HasFlag(FilterMode.Form) && settings.IsFilterFormInline)
230230
{
231231
<tr class="griddly-filters griddly-filters-form" style="@(settings.InitialFilterMode?.HasFlag(FilterMode.Form) == true ? null : "display:none")">
232-
<td colspan="@settings.Columns.Count">
232+
<td colspan="@settings.Columns.Where(x => x.Visible).Count()">
233233
@if (settings.FilterTemplate != null)
234234
{
235235
@settings.FilterTemplate(settings)
@@ -243,7 +243,7 @@
243243
SortField sort = Model.SortFields==null?null:Model.SortFields.FirstOrDefault(x => x.Field == column.ExpressionString);
244244

245245
bool isSortable = !simple && !string.IsNullOrWhiteSpace(column.ExpressionString);
246-
<th class="@column.ClassName @(isSortable ? "sortable" : null) @(column is GriddlySelectColumn ? "select" : null) @(sort != null ? (sort.Direction == SortDirection.Descending ? "sorted_d" : "sorted_a") : null)" @Html.AttributeNullable("data-griddly-sortfield", simple ? null : column.ExpressionString) @column.HeaderHtmlAttributes.ToHtmlAttributes()>
246+
<th class="@column.ClassName @(isSortable ? "sortable" : null) @(!column.Visible ? "column-hidden" : null) @(column is GriddlySelectColumn ? "select" : null) @(sort != null ? (sort.Direction == SortDirection.Descending ? "sorted_d" : "sorted_a") : null)" @Html.AttributeNullable("data-griddly-sortfield", simple ? null : column.ExpressionString) @column.HeaderHtmlAttributes.ToHtmlAttributes()>
247247
@if (!string.IsNullOrWhiteSpace(column.ExpressionString))
248248
{
249249
<text>@Html.Raw(column.Caption)<span class="icon">&nbsp;</span></text>
@@ -270,7 +270,7 @@
270270
if (template != null)
271271
{
272272
<tr class="griddly-header">
273-
<td colspan="@settings.Columns.Count">
273+
<td colspan="@settings.Columns.Where(x => x.Visible).Count()">
274274
@template
275275
</td>
276276
</tr>
@@ -294,7 +294,7 @@
294294
@foreach (GriddlyColumn column in settings.Columns)
295295
{
296296
var underlyingValue = column.RenderUnderlyingValue(row);
297-
<td class="@column.RenderClassName(row, Model)" @column.GenerateHtmlAttributes(row, Model).ToHtmlAttributes() @if (underlyingValue != null) { <text>data-value="@underlyingValue"</text> }>@column.RenderCell(row, settings)</td>
297+
<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>
298298
}
299299
</tr>
300300
}
@@ -305,7 +305,7 @@
305305
<tr>
306306
@foreach (GriddlyColumn column in settings.Columns)
307307
{
308-
<td class="@column.ClassName">@column.RenderValue(column.SummaryValue)</td>
308+
<td class="@column.ClassName @(!column.Visible ? "column-hidden" : null)">@column.RenderValue(column.SummaryValue)</td>
309309
}
310310
</tr>
311311
</tfoot>
@@ -314,7 +314,7 @@
314314
@if (settings.EmptyGridMessage != null) {
315315
<tfoot class="empty-grid-message" style="@(Model.Total > 0 ? "display:none;" : null)">
316316
<tr>
317-
<td colspan="@settings.Columns.Count">
317+
<td colspan="@settings.Columns.Where(x => x.Visible).Count()">
318318
@if (settings.EmptyGridMessageTemplate != null)
319319
{
320320
@settings.EmptyGridMessageTemplate(new EmptyGridMessageTemplateParams() { ResultPage = Model, Settings = settings })

Griddly/Views/Shared/Griddly/GriddlyFilterInline.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{
1818
GriddlyColumn column = Model.Columns[i];
1919

20-
<td class="@column.ClassName @(column.DefaultSort != null ? (column.DefaultSort == SortDirection.Descending ? "sorted_d" : "sorted_a") : null)">
20+
<td class="@column.ClassName @(!column.Visible ? "column-hidden" : null) @(column.DefaultSort != null ? (column.DefaultSort == SortDirection.Descending ? "sorted_d" : "sorted_a") : null)">
2121
@if (column.Filter != null)
2222
{
2323
GriddlyFilter filter = column.Filter;

0 commit comments

Comments
 (0)