Skip to content

Commit 175f58f

Browse files
committed
Merge pull request #62 from jehhynes/master
Pass old value and cell to save function
2 parents 01023e5 + e0ade64 commit 175f58f

File tree

10 files changed

+173
-31
lines changed

10 files changed

+173
-31
lines changed

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="Exceptions\DapperGriddlyException.cs" />
9393
<Compile Include="GriddlyFilterExtensions.cs" />
9494
<Compile Include="GriddlyHtmlFilter.cs" />
95+
<Compile Include="GriddlyExport.cs" />
9596
<Compile Include="InternalExtensions.cs" />
9697
<Compile Include="GriddlyButton.cs" />
9798
<Compile Include="GriddlyColumn.cs" />

Griddly.Mvc/GriddlyCsvResult.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CsvHelper.Configuration;
33
using System.Collections.Generic;
44
using System.Web.Mvc;
5+
using System.Linq;
56

67
namespace Griddly.Mvc
78
{
@@ -11,13 +12,15 @@ public class GriddlyCsvResult<T> : ActionResult
1112
GriddlySettings _settings;
1213
string _name;
1314
GriddlyExportFormat _format;
15+
string _exportName;
1416

15-
public GriddlyCsvResult(IEnumerable<T> data, GriddlySettings settings, string name, GriddlyExportFormat format = GriddlyExportFormat.Csv)
17+
public GriddlyCsvResult(IEnumerable<T> data, GriddlySettings settings, string name, GriddlyExportFormat format = GriddlyExportFormat.Csv, string exportName = null)
1618
{
1719
_data = data;
1820
_settings = settings;
1921
_name = name;
2022
_format = format;
23+
_exportName = exportName;
2124
}
2225

2326
public override void ExecuteResult(ControllerContext context)
@@ -29,18 +32,23 @@ public override void ExecuteResult(ControllerContext context)
2932

3033
using (CsvWriter w = new CsvWriter(context.HttpContext.Response.Output, new CsvConfiguration() { HasHeaderRecord = true, Delimiter = _format == GriddlyExportFormat.Tsv ? "\t" : "," }))
3134
{
32-
for (int i = 0; i < _settings.Columns.Count; i++)
33-
w.WriteField(_settings.Columns[i].Caption);
35+
var export = _settings.Exports.FirstOrDefault(x => x.Name == _exportName);
36+
var columns = export == null ? _settings.Columns : export.Columns;
37+
if (export != null && export.UseGridColumns) columns.InsertRange(0, _settings.Columns);
38+
39+
40+
for (int i = 0; i < columns.Count; i++)
41+
w.WriteField(columns[i].Caption);
3442

3543
w.NextRecord();
3644

3745
int y = 0;
3846

3947
foreach (T row in _data)
4048
{
41-
for (int x = 0; x < _settings.Columns.Count; x++)
49+
for (int x = 0; x < columns.Count; x++)
4250
{
43-
object renderedValue = _settings.Columns[x].RenderCellValue(row, true);
51+
object renderedValue = columns[x].RenderCellValue(row, true);
4452

4553
w.WriteField(renderedValue);
4654
}

Griddly.Mvc/GriddlyExcelResult.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Web.Mvc;
6+
using System.Linq;
67

78
namespace Griddly.Mvc
89
{
@@ -11,12 +12,14 @@ public class GriddlyExcelResult<T> : ActionResult
1112
IEnumerable<T> _data;
1213
GriddlySettings _settings;
1314
string _name;
15+
string _exportName;
1416

15-
public GriddlyExcelResult(IEnumerable<T> data, GriddlySettings settings, string name)
17+
public GriddlyExcelResult(IEnumerable<T> data, GriddlySettings settings, string name, string exportName = null)
1618
{
1719
_data = data;
1820
_settings = settings;
1921
_name = name;
22+
_exportName = exportName;
2023
}
2124

2225
// static readonly Regex _aMatch = new Regex(@"<a\s[^>]*\s?href=""(.*?)"">(.*?)</a>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
@@ -27,19 +30,23 @@ public override void ExecuteResult(ControllerContext context)
2730
{
2831
ExcelWorksheet ws = p.Workbook.Worksheets.Add(_name);
2932

30-
for (int i = 0; i < _settings.Columns.Count; i++)
33+
var export = _settings.Exports.FirstOrDefault(x => x.Name == _exportName);
34+
var columns = export == null ? _settings.Columns : export.Columns;
35+
if (export != null && export.UseGridColumns) columns.InsertRange(0, _settings.Columns);
36+
37+
for (int i = 0; i < columns.Count; i++)
3138
{
32-
ws.Cells[1, i + 1].Value = _settings.Columns[i].Caption;
39+
ws.Cells[1, i + 1].Value = columns[i].Caption;
3340
ws.Cells[1, i + 1].Style.Font.Bold = true;
3441
}
3542

3643
int y = 0;
3744

3845
foreach (T row in _data)
3946
{
40-
for (int x = 0; x < _settings.Columns.Count; x++)
47+
for (int x = 0; x < columns.Count; x++)
4148
{
42-
object renderedValue = _settings.Columns[x].RenderCellValue(row, true);
49+
object renderedValue = columns[x].RenderCellValue(row, true);
4350

4451
ExcelRange cell = ws.Cells[y + 2, x + 1];
4552

@@ -50,7 +57,7 @@ public override void ExecuteResult(ControllerContext context)
5057
cell.Style.Numberformat.Format = "mm/dd/yyyy";
5158
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
5259
}
53-
else if (_settings.Columns[x].Format == "c")
60+
else if (columns[x].Format == "c")
5461
{
5562
cell.Style.Numberformat.Format = "\"$\"#,##0.00_);(\"$\"#,##0.00)";
5663
cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
@@ -60,7 +67,7 @@ public override void ExecuteResult(ControllerContext context)
6067
y++;
6168
}
6269

63-
for (int i = 0; i < _settings.Columns.Count; i++)
70+
for (int i = 0; i < columns.Count; i++)
6471
ws.Column(i + 1).AutoFit();
6572

6673
context.HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

Griddly.Mvc/GriddlyExport.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Collections.Specialized;
5+
using System.Linq;
6+
using System.Linq.Expressions;
7+
using System.Web;
8+
using System.Web.Helpers;
9+
using System.Web.Mvc;
10+
using System.Web.Routing;
11+
12+
namespace Griddly.Mvc
13+
{
14+
public class GriddlyExport
15+
{
16+
public GriddlyExport(string name, bool useGridColumns = false)
17+
{
18+
this.UseGridColumns = useGridColumns;
19+
this.Name = name;
20+
this.Columns = new List<GriddlyColumn>();
21+
}
22+
public string Name { get; set; }
23+
public bool UseGridColumns { get; set; }
24+
public List<GriddlyColumn> Columns { get; set; }
25+
}
26+
public class GriddlyExport<TRow> : GriddlyExport
27+
{
28+
public GriddlyExport(string name, bool useGridColumns = false)
29+
: base(name, useGridColumns)
30+
{
31+
}
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, bool isExportOnly = false, 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)
33+
{
34+
ModelMetadata metadata = null;
35+
36+
if (expression != null)
37+
{
38+
metadata = ModelMetadata.FromLambdaExpression<TRow, TProperty>(expression, new ViewDataDictionary<TRow>());
39+
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
40+
41+
Type type = metadata.ModelType;
42+
43+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
44+
type = Nullable.GetUnderlyingType(type);
45+
46+
if (className == null)
47+
{
48+
if (type == typeof(byte) || type == typeof(sbyte) ||
49+
type == typeof(short) || type == typeof(ushort) ||
50+
type == typeof(int) || type == typeof(uint) ||
51+
type == typeof(long) || type == typeof(ulong) ||
52+
type == typeof(float) ||
53+
type == typeof(double) ||
54+
type == typeof(decimal))
55+
className = "align-right";
56+
else if (type == typeof(bool) ||
57+
type == typeof(DateTime) || type.HasCastOperator<DateTime>())
58+
className = "align-center";
59+
}
60+
61+
if (caption == null)
62+
caption = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
63+
64+
if (expressionString == null)
65+
expressionString = htmlFieldName;
66+
67+
if (template == null)
68+
{
69+
var compiledTemplate = expression.Compile();
70+
71+
//if (type == typeof(bool) && (BoolTrueHtml != null || BoolFalseHtml != null))
72+
// template = (row) => (compiledTemplate(row) as bool? == true) ? BoolTrueHtml : BoolFalseHtml;
73+
//else
74+
template = (row) => compiledTemplate(row);
75+
}
76+
}
77+
78+
if (string.IsNullOrWhiteSpace(expressionString) && summaryFunction != null)
79+
throw new InvalidOperationException("Must specify an expression to use a summary function.");
80+
81+
if (headerHtmlAttributes != null && !(headerHtmlAttributes is IDictionary<string, object>))
82+
headerHtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(headerHtmlAttributes);
83+
84+
var valueTemplate = value == null ? null : value.Compile();
85+
Columns.Add(new GriddlyColumn<TRow>()
86+
{
87+
Template = template,
88+
Caption = caption,
89+
Format = format,
90+
ExpressionString = expressionString,
91+
SummaryFunction = summaryFunction,
92+
SummaryValue = summaryValue,
93+
DefaultSort = defaultSort,
94+
DefaultSortOrder = defaultSortOrder,
95+
ClassName = className,
96+
IsExportOnly = isExportOnly,
97+
Width = width,
98+
HtmlAttributesTemplate = htmlAttributes,
99+
HeaderHtmlAttributes = (IDictionary<string, object>)headerHtmlAttributes,
100+
UnderlyingValueTemplate = valueTemplate
101+
});
102+
103+
return this;
104+
}
105+
106+
public GriddlyExport<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<TRow, object> htmlAttributes = null, object headerHtmlAttributes = null, int defaultSortOrder = 0, Expression<Func<TRow, object>> value = null)
107+
{
108+
return Column<object>(null, caption, format, expressionString, defaultSort, className, isExportOnly, width, summaryFunction, summaryValue, template, htmlAttributes, headerHtmlAttributes, defaultSortOrder, value);
109+
}
110+
}
111+
}

Griddly.Mvc/GriddlyResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ public override void ExecuteResult(ControllerContext context)
150150
}
151151
else if (exportFormat == GriddlyExportFormat.Xlsx)
152152
{
153-
result = new GriddlyExcelResult<T>(records, settings, fileName);
153+
result = new GriddlyExcelResult<T>(records, settings, fileName, items["exportName"]);
154154
}
155155
else // if (exportFormat == GriddlyExportFormat.Csv || exportFormat == GriddlyExportFormat.Tsv)
156156
{
157-
result = new GriddlyCsvResult<T>(records, settings, fileName, exportFormat.Value);
157+
result = new GriddlyCsvResult<T>(records, settings, fileName, exportFormat.Value, items["exportName"]);
158158
}
159159

160160
result.ExecuteResult(context);

Griddly.Mvc/GriddlySettings.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public GriddlySettings()
4545
Columns = new List<GriddlyColumn>();
4646
Filters = new List<GriddlyFilter>();
4747
Buttons = new List<GriddlyButton>();
48+
Exports = new List<GriddlyExport>();
4849
RowIds = new Dictionary<string, Func<object, object>>();
4950
HtmlAttributes = new RouteValueDictionary();
5051
TableHtmlAttributes = new RouteValueDictionary();
@@ -76,6 +77,7 @@ public GriddlySettings()
7677
public List<GriddlyColumn> Columns { get; set; }
7778
public List<GriddlyFilter> Filters { get; set; }
7879
public List<GriddlyButton> Buttons { get; set; }
80+
public List<GriddlyExport> Exports { get; set; }
7981

8082
public Func<object, object> BeforeTemplate { get; set; }
8183
public Func<object, object> AfterTemplate { get; set; }
@@ -174,6 +176,13 @@ public GriddlySettings Add(GriddlyButton button)
174176
return this;
175177
}
176178

179+
public GriddlySettings Add(GriddlyExport export)
180+
{
181+
Exports.Add(export);
182+
183+
return this;
184+
}
185+
177186
public GriddlySettings FilterBox(string field, string caption, FilterDataType dataType = FilterDataType.Decimal, string htmlClass = null, string captionPlural = null)
178187
{
179188
return Add(GriddlyFilterExtensions.FilterBox(null, dataType, field, caption, htmlClass, captionPlural));

Griddly/Scripts/editly.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
var saveActive = function () {
153153
var oldValue = active.data("value");
154154
if (oldValue === undefined) oldValue = self.options.parseText(active.text(), template);
155+
var oldText = active.text();
155156
var newValue = editor.val();
156157

157158
if (oldValue != newValue) {
@@ -162,7 +163,10 @@
162163
{
163164
id: active.parents("tr:first").data("id"),
164165
field: $(active.parents("table:first").find("col")[active[0].cellIndex]).data("field"),
165-
value: newValue
166+
value: newValue,
167+
oldValue: oldValue,
168+
oldText: oldText,
169+
cell: active
166170
});
167171
}
168172
};
@@ -395,7 +399,7 @@
395399

396400
$.fn.editly.defaults =
397401
{
398-
save: function (id, field, value) { },
402+
save: function () { },
399403
parseText: function (val, editor) { return val; },
400404
formatText: function (val, editor) { return val; },
401405
cloneProperties: ['padding', 'padding-top', 'padding-bottom', 'padding-left', 'padding-right',

Griddly/Scripts/griddly.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,15 @@
449449
}, this));
450450

451451
$("a.export-xlsx", this.$element).on("click", $.proxy(function (e) {
452-
this.exportFile("xlsx");
452+
this.exportFile("xlsx", null, { exportName: $(e.target).data("export-name") });
453453
e.preventDefault();
454454
}, this));
455455
$("a.export-csv", this.$element).on("click", $.proxy(function (e) {
456-
this.exportFile("csv");
456+
this.exportFile("csv", null, { exportName: $(e.target).data("export-name") });
457457
e.preventDefault();
458458
}, this));
459459
$("a.export-tsv", this.$element).on("click", $.proxy(function (e) {
460-
this.exportFile("tsv");
460+
this.exportFile("tsv", null, { exportName: $(e.target).data("export-name") });
461461
e.preventDefault();
462462
}, this));
463463

@@ -767,15 +767,15 @@
767767
var params = this.buildRequest();
768768

769769
params.exportFormat = type;
770-
770+
$.extend(params, data);
771771
if (exec)
772772
{
773-
$.extend(params, data);
774773
exec(this.options.url, params)
775774
}
776775
else
777776
{
778777
var url = this.options.url + (this.options.url.indexOf("?") == -1 ? "?" : "&") + $.param(params, true);
778+
alert(url);
779779
window.location = url;
780780
}
781781
},

0 commit comments

Comments
 (0)