Skip to content

Commit 9ec3a73

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

File tree

12 files changed

+73
-24
lines changed

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")

0 commit comments

Comments
 (0)