Skip to content

Commit 06cdfe8

Browse files
committed
Merge pull request #12 from ithielnor/master
New RowIds feature and select column storage across data refresh
2 parents b1fcb0d + bfd9497 commit 06cdfe8

File tree

11 files changed

+329
-116
lines changed

11 files changed

+329
-116
lines changed

Griddly.Mvc/GriddlyButton.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class GriddlyButton
2020
public string Target { get; set; }
2121
public string ConfirmMessage { get; set; }
2222
public bool AlignRight { get; set; }
23+
public string[] RowIds { get; set; }
2324

2425
public GriddlyButtonAction Action { get; set; }
2526

Griddly.Mvc/GriddlyColumn.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class GriddlyColumn
2222

2323
public GriddlyFilter Filter { get; set; }
2424

25-
public abstract HtmlString RenderCell(object row, bool encode = true);
25+
public abstract HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true);
2626
public abstract object RenderCellValue(object row, bool stripHtml = false);
2727
public abstract string RenderClassName(object row, GriddlyResultPage page);
2828

@@ -86,7 +86,7 @@ public override string RenderClassName(object row, GriddlyResultPage page)
8686
return null;
8787
}
8888

89-
public override HtmlString RenderCell(object row, bool encode = true)
89+
public override HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true)
9090
{
9191
object value = null;
9292

Griddly.Mvc/GriddlySelectColumn.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
4+
using System.Linq.Expressions;
25
using System.Web;
36
using System.Web.Mvc;
47

@@ -8,19 +11,40 @@ public class GriddlySelectColumn : GriddlyColumn
811
{
912
public GriddlySelectColumn()
1013
{
11-
ClassName = "griddly-select";
14+
ClassName = "griddly-select align-center";
1215
}
13-
14-
public Func<object, object> Id { get; set; }
15-
16-
public override HtmlString RenderCell(object row, bool encode = true)
16+
17+
public override HtmlString RenderCell(object row, GriddlySettings settings, bool encode = true)
1718
{
1819
TagBuilder input = new TagBuilder("input");
1920

2021
input.Attributes["name"] = "_rowselect";
21-
input.Attributes["value"] = Id(row).ToString();
2222
input.Attributes["type"] = "checkbox";
2323

24+
if (settings.RowIds.Any())
25+
{
26+
bool valueSet = false;
27+
string key = "";
28+
foreach (var x in settings.RowIds)
29+
{
30+
string val = "";
31+
object result = x.Value(row);
32+
if (result != null)
33+
val = result.ToString();
34+
35+
input.Attributes["data-" + x.Key] = val;
36+
key += "_" + val;
37+
38+
if (!valueSet)
39+
{
40+
input.Attributes["value"] = val;
41+
valueSet = true;
42+
}
43+
}
44+
45+
input.Attributes["data-rowkey"] = key;
46+
}
47+
2448
return new HtmlString(input.ToString(TagRenderMode.SelfClosing));
2549
}
2650

@@ -31,7 +55,7 @@ public override object RenderCellValue(object row, bool stripHtml = false)
3155

3256
public override string RenderClassName(object row, GriddlyResultPage page)
3357
{
34-
return null;
58+
return ClassName;
3559
}
3660
}
3761
}

Griddly.Mvc/GriddlySettings.cs

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public abstract class GriddlySettings
2020
public static HtmlString BoolFalseHtml = null;
2121
public static int? DefaultPageSize = null;
2222
public static bool DefaultShowFilterInitially = true;
23+
public static bool DefaultShowRowSelectCount = true;
2324

2425
public static Func<GriddlyButton, object> IconTemplate = null;
2526
public static Func<GriddlyResultPage, object> DefaultFooterTemplate = null;
@@ -30,23 +31,29 @@ public abstract class GriddlySettings
3031
public GriddlySettings()
3132
{
3233
IdProperty = "Id";
34+
HasInlineFilter = true;
35+
3336
Columns = new List<GriddlyColumn>();
3437
Filters = new List<GriddlyFilter>();
3538
Buttons = new List<GriddlyButton>();
39+
RowIds = new Dictionary<string, Func<object, object>>();
40+
3641
ClassName = DefaultClassName;
3742
TableClassName = DefaultTableClassName;
3843
FooterTemplate = DefaultFooterTemplate;
3944
PageSize = DefaultPageSize;
4045
ShowFilterInitially = DefaultShowFilterInitially;
41-
HasInlineFilter = true;
46+
ShowRowSelectCount = DefaultShowRowSelectCount;
4247
}
4348

49+
public string[] DefaultRowIds { get; set; }
4450
public string IdProperty { get; set; }
4551
public string Title { get; set; }
4652
public string ClassName { get; set; }
4753
public string TableClassName { get; set; }
4854
public string OnClientRefresh { get; set; }
4955
public bool ShowFilterInitially { get; set; }
56+
public bool ShowRowSelectCount { get; set; }
5057

5158
public int? PageSize { get; set; }
5259
public int? MaxPageSize { get; set; }
@@ -68,6 +75,8 @@ public GriddlySettings()
6875

6976
public bool HasInlineFilter { get; set; }
7077

78+
public Dictionary<string, Func<object, object>> RowIds { get; protected set; }
79+
7180
public virtual bool HasRowClickUrl
7281
{
7382
get { return RowClickUrl != null; }
@@ -88,6 +97,19 @@ public virtual object RenderRowClass(object o)
8897
return RowClass(o);
8998
}
9099

100+
public GriddlySettings RowId(Expression<Func<object, object>> expression, string name = null)
101+
{
102+
if (name == null)
103+
{
104+
var meta = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary<object>());
105+
name = ExpressionHelper.GetExpressionText(expression);
106+
}
107+
108+
RowIds[name ?? "id"] = expression.Compile();
109+
110+
return this;
111+
}
112+
91113
public GriddlySettings Add(GriddlyColumn column, Func<GriddlyColumn, GriddlyFilter> filter = null)
92114
{
93115
if (filter != null)
@@ -125,7 +147,7 @@ public GriddlySettings Add(GriddlyButton button)
125147
});
126148
}*/
127149

128-
public GriddlySettings Button(Func<object, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null)
150+
public GriddlySettings Button(Func<object, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null, string[] rowIds = null)
129151
{
130152
if (enableOnSelection == null)
131153
enableOnSelection = (action == GriddlyButtonAction.Ajax || action == GriddlyButtonAction.AjaxBulk || action == GriddlyButtonAction.Post);
@@ -137,13 +159,14 @@ public GriddlySettings Button(Func<object, object> argumentTemplate, string capt
137159
Icon = icon,
138160
Action = action,
139161
EnableOnSelection = enableOnSelection.Value,
140-
Target = target
162+
Target = target,
163+
RowIds = rowIds
141164
};
142165

143166
return Add(button);
144167
}
145168

146-
public GriddlySettings Button(string argument, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null)
169+
public GriddlySettings Button(string argument, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, bool? enableOnSelection = null, string className = null, string target = null, string[] rowIds = null)
147170
{
148171
if (enableOnSelection == null)
149172
enableOnSelection = (action == GriddlyButtonAction.Ajax || action == GriddlyButtonAction.AjaxBulk || action == GriddlyButtonAction.Post);
@@ -155,7 +178,8 @@ public GriddlySettings Button(string argument, string caption, string icon = nul
155178
Icon = icon,
156179
Action = action,
157180
EnableOnSelection = enableOnSelection.Value,
158-
Target = target
181+
Target = target,
182+
RowIds = rowIds
159183
};
160184

161185
return Add(button);
@@ -169,11 +193,26 @@ public GriddlySettings ButtonSeparator()
169193
});
170194
}
171195

172-
public GriddlySettings SelectColumn(Func<object, object> id)
196+
public GriddlySettings SelectColumn(Expression<Func<object, object>> id, object summaryValue = null)
173197
{
198+
RowId(id, "id");
199+
174200
return Add(new GriddlySelectColumn()
175201
{
176-
Id = id
202+
SummaryValue = summaryValue
203+
});
204+
}
205+
206+
public GriddlySettings SelectColumn(Dictionary<string, Func<object, object>> ids, object summaryValue = null)
207+
{
208+
foreach (var x in ids)
209+
{
210+
RowIds[x.Key] = x.Value;
211+
}
212+
213+
return Add(new GriddlySelectColumn()
214+
{
215+
SummaryValue = summaryValue
177216
});
178217
}
179218

@@ -242,6 +281,19 @@ public class GriddlySettings<TRow> : GriddlySettings
242281
base.RowClass = null;
243282
}
244283
}
284+
285+
public GriddlySettings<TRow> RowId(Expression<Func<TRow, object>> expression, string name = null)
286+
{
287+
if (name == null)
288+
{
289+
var meta = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary<TRow>());
290+
name = ExpressionHelper.GetExpressionText(expression);
291+
}
292+
293+
RowIds[name ?? "id"] = (x) => expression.Compile()((TRow)x);
294+
295+
return this;
296+
}
245297

246298
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)
247299
{
@@ -330,26 +382,42 @@ public GriddlySettings<TRow> Column(string caption = null, string format = null,
330382
// return this;
331383
//}
332384

333-
public GriddlySettings<TRow> SelectColumn(Func<TRow, object> id, object summaryValue = null)
385+
public GriddlySettings<TRow> SelectColumn(Expression<Func<TRow, object>> id, object summaryValue = null)
334386
{
387+
RowId(id, "id");
388+
335389
Add(new GriddlySelectColumn()
336390
{
337-
Id = (x) => id((TRow)x),
338-
ClassName = "align-center",
339391
SummaryValue = summaryValue
340392
});
341393

342394
return this;
343395
}
344396

345-
public GriddlySettings<TRow> Button<TModel>(Func<TModel, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate)
397+
public GriddlySettings<TRow> SelectColumn(Dictionary<string, Func<TRow, object>> ids, object summaryValue = null)
398+
{
399+
foreach (var x in ids)
400+
{
401+
RowIds[x.Key] = (z) => x.Value((TRow)z);
402+
}
403+
404+
Add(new GriddlySelectColumn()
405+
{
406+
SummaryValue = summaryValue
407+
});
408+
409+
return this;
410+
}
411+
412+
public GriddlySettings<TRow> Button<TModel>(Func<TModel, object> argumentTemplate, string caption, string icon = null, GriddlyButtonAction action = GriddlyButtonAction.Navigate, string[] rowIds = null)
346413
{
347414
Add(new GriddlyButton()
348415
{
349416
ArgumentTemplate = (x) => argumentTemplate((TModel)x),
350417
Text = caption,
351418
Icon = icon,
352-
Action = action
419+
Action = action,
420+
RowIds = rowIds
353421
});
354422

355423
return this;

Griddly/Controllers/HomeController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ static List<TestGridItem> BuildTestData()
115115
{
116116
items.Add(new TestGridItem()
117117
{
118+
Id = (long)i,
118119
FirstName = Name.GetFirstName(),
119120
LastName = Name.GetLastName(),
120121
Company = Company.GetName(),
121122
Address = r.Next(short.MaxValue) + " " + Address.GetStreetName(),
122123
City = Address.GetCity(),
123124
State = Address.GetUSState(),
124125
PostalCode = Address.GetZipCode(),
125-
Test = (decimal)(r.NextDouble() * 10000)
126+
Test = (decimal)(r.NextDouble() * 10000),
127+
NullThing = (string)null,
126128
});
127129
}
128130

Griddly/Models/TestGridItem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Griddly.Models
44
{
55
public class TestGridItem
66
{
7+
public long? Id { get; set; }
78
public decimal? Test { get; set; }
89
public string FirstName { get; set; }
910
public string LastName { get; set; }
@@ -12,6 +13,7 @@ public class TestGridItem
1213
public string City { get; set; }
1314
public string State { get; set; }
1415
public string PostalCode { get; set; }
16+
public string NullThing { get; set; }
1517

1618
public int PostalCodePrefix
1719
{

0 commit comments

Comments
 (0)