Skip to content

Commit 0f1e529

Browse files
committed
Add GetAllForProperty to GriddlyResult
Breaking change
1 parent 712fe0c commit 0f1e529

File tree

7 files changed

+96
-53
lines changed

7 files changed

+96
-53
lines changed

Griddly.Mvc/GriddlyResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public SortField[] GetSortFields(NameValueCollection items)
3737
})
3838
.ToArray();
3939
}
40+
41+
public abstract IEnumerable<P> GetAllForProperty<P>(string propertyName);
4042
}
4143

4244
public abstract class GriddlyResult<T> : GriddlyResult

Griddly.Mvc/Results/DapperResult.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ public override void PopulateSummaryValues(GriddlySettings<T> settings)
7979
}
8080
}
8181

82+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName)
83+
{
84+
string sql = string.Format("SELECT {0} as _val FROM ({1}) [_proj]", propertyName, _sql);
85+
86+
try
87+
{
88+
IDbConnection cn = _getConnection();
89+
IDbTransaction tx = _getTransaction != null ? _getTransaction() : null;
90+
91+
return cn.Query<P>(sql, _param, tx);
92+
}
93+
catch (Exception ex)
94+
{
95+
throw new DapperGriddlyException($"Error selecting property: {propertyName}.", sql, _param, ex: ex);
96+
}
97+
}
98+
8299
public override long GetCount()
83100
{
84101
if (_overallCount == null)
@@ -109,12 +126,10 @@ protected string BuildSortClause(SortField[] sortFields)
109126

110127
protected virtual X ExecuteSingle<X>(string sql)
111128
{
112-
113-
IDbConnection cn = _getConnection();
114-
IDbTransaction tx = _getTransaction != null ? _getTransaction() : null;
129+
IDbConnection cn = _getConnection();
130+
IDbTransaction tx = _getTransaction != null ? _getTransaction() : null;
115131

116-
return cn.Query<X>(sql, _param, tx).Single();
117-
132+
return cn.Query<X>(sql, _param, tx).Single();
118133
}
119134

120135
// TODO: return IEnumerable so we don't have to .ToList()

Griddly.Mvc/Results/MapQueryableResult.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public override void PopulateSummaryValues(GriddlySettings<TOut> settings)
4040
_result.PopulateSummaryValue(c);
4141
}
4242

43+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName)
44+
{
45+
return _result.GetAllForProperty<P>(propertyName);
46+
}
47+
4348
public override long GetCount()
4449
{
4550
return _result.GetCount();

Griddly.Mvc/Results/QueryableResult.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ internal void PopulateSummaryValue(GriddlyColumn c)
9393
}
9494
}
9595

96+
public override IEnumerable<P> GetAllForProperty<P>(string propertyName)
97+
{
98+
return _result.Select<P>(propertyName, null);
99+
}
100+
96101
public override long GetCount()
97102
{
98103
return _result.Count();

Griddly/Controllers/HomeController.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
using Griddly.Mvc.Results;
55
using System;
66
using System.Collections.Generic;
7+
using System.Collections.Specialized;
78
using System.Linq;
89
using System.Web.Mvc;
910

1011
namespace Griddly.Controllers
1112
{
1213
public class HomeController : Controller
1314
{
15+
public static ActionResult HandleCustomExport(GriddlyResult result, NameValueCollection form)
16+
{
17+
return new JsonResult()
18+
{
19+
Data = result.GetAllForProperty<long?>("Id"),
20+
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
21+
};
22+
}
23+
1424
public ActionResult Index()
1525
{
1626
return View();

Griddly/Views/Home/TestGrid.cshtml

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11

22
@{
33
ViewBag.Title = "TestGrid";
4-
}
54

6-
@Html.Griddly(new GriddlySettings<TestGridItem>()
7-
{
8-
PageSize = 5,
9-
ShowRowSelectCount = true,
10-
Title = "Row Select Test",
11-
ClassName = "test-grid"
12-
}
13-
.SelectColumn(x => x.Id, inputHtmlAttributesTemplate: x => new { data_testattr = x.Id })
14-
.RowId(x => x.FirstName)
15-
.RowId(x => x.LastName)
16-
.RowId(x => x.NullThing)
17-
.Column(x => x.FirstName, "First Name", filter: x => x.FilterList(new List<SelectListItem>() { new SelectListItem() { Text = "Blah", Value = "0" } }, defaultSelectAll: true))
18-
.Column(x => x.LastName, "Last Name", defaultSort: SortDirection.Ascending)
19-
.Column(x => x.Company, "Company")
20-
.Column(x => x.Address, "Address")
21-
.Column(x => x.City, "City")
22-
.Column(x => x.State, "State")
23-
.Column(x => x.PostalCode, "Zip", template: x => Html.ActionLink("&" + x.PostalCode, "Profile"))
24-
.Button("", "export", action: GriddlyButtonAction.Javascript, className: "export-xlsx")
25-
.Button("", "named export", action: GriddlyButtonAction.Javascript, className: "export-xlsx", htmlAttributes: new { data_export_name="test-export" })
26-
.Add(new GriddlyButton() { Text = "Confirm", Argument = "http://google.com", Action = GriddlyButtonAction.PostCriteria, ConfirmMessage = "Please confirm" })
27-
.Add(new GriddlyButton() { IsSeparator = true })
28-
.Add(new GriddlyButton() { IsSeparator = true })
29-
.Button("AlertSelectedIds", "Alert Selected", action: GriddlyButtonAction.Javascript, enableOnSelection: true, htmlAttributes: new { data_test = "hello world" })
30-
.Add(new GriddlyButton() { IsSeparator = true })
31-
.Button("TestPost", "Post Selected", action: GriddlyButtonAction.AjaxBulk, enableOnSelection: true)
32-
.Add(new GriddlyButton()
5+
var grid = new GriddlySettings<TestGridItem>()
336
{
34-
Text = "A dropdown in a group",
35-
Buttons = new List<GriddlyButton>()
7+
PageSize = 5,
8+
ShowRowSelectCount = true,
9+
Title = "Row Select Test",
10+
ClassName = "test-grid",
11+
};
12+
13+
grid.SelectColumn(x => x.Id, inputHtmlAttributesTemplate: x => new { data_testattr = x.Id })
14+
.RowId(x => x.FirstName)
15+
.RowId(x => x.LastName)
16+
.RowId(x => x.NullThing)
17+
.Column(x => x.FirstName, "First Name", filter: x => x.FilterList(new List<SelectListItem>() { new SelectListItem() { Text = "Blah", Value = "0" } }, defaultSelectAll: true))
18+
.Column(x => x.LastName, "Last Name", defaultSort: SortDirection.Ascending)
19+
.Column(x => x.Company, "Company")
20+
.Column(x => x.Address, "Address")
21+
.Column(x => x.City, "City")
22+
.Column(x => x.State, "State")
23+
.Column(x => x.PostalCode, "Zip", template: x => Html.ActionLink("&" + x.PostalCode, "Profile"))
24+
.Column("City/State/Zip", template: x => x.City + " " + x.State + " " + x.PostalCode);
25+
26+
grid.Button("", "export", action: GriddlyButtonAction.Javascript, className: "export-xlsx")
27+
.Button("", "named export", action: GriddlyButtonAction.Javascript, className: "export-xlsx", htmlAttributes: new { data_export_name = "test-export" })
28+
.Button("", "Custom Export", action: GriddlyButtonAction.Javascript, className: "export-custom")
29+
.Add(new GriddlyButton() { Text = "Confirm", Argument = "http://google.com", Action = GriddlyButtonAction.PostCriteria, ConfirmMessage = "Please confirm" })
30+
.Add(new GriddlyButton() { IsSeparator = true })
31+
.Add(new GriddlyButton() { IsSeparator = true })
32+
.Button("AlertSelectedIds", "Alert Selected", action: GriddlyButtonAction.Javascript, enableOnSelection: true, htmlAttributes: new { data_test = "hello world" })
33+
.Add(new GriddlyButton() { IsSeparator = true })
34+
.Button("TestPost", "Post Selected", action: GriddlyButtonAction.AjaxBulk, enableOnSelection: true)
35+
.Add(new GriddlyButton()
3636
{
37-
new GriddlyButton() { Text = "1" },
38-
new GriddlyButton() { Text = "2" },
39-
new GriddlyButton() { Text = "3" }
40-
}
41-
})
42-
.Add(new GriddlyButton() { IsSeparator = true })
43-
.Add(new GriddlyButton()
44-
{
45-
Text = "A dropdown alone",
46-
Buttons = new List<GriddlyButton>()
37+
Text = "A dropdown in a group",
38+
Buttons = new List<GriddlyButton>()
39+
{
40+
new GriddlyButton() { Text = "1" },
41+
new GriddlyButton() { Text = "2" },
42+
new GriddlyButton() { Text = "3" }
43+
}
44+
})
45+
.Add(new GriddlyButton() { IsSeparator = true })
46+
.Add(new GriddlyButton()
4747
{
48-
new GriddlyButton() { Text = "1" },
49-
new GriddlyButton() { Text = "2" },
50-
new GriddlyButton() { Text = "3" }
51-
}
52-
})
53-
.Add(new GriddlyExport<TestGridItem>("test-export", true)
54-
.Column("City/State/Zip", template:x=>x.City+" " +x.State+" " + x.PostalCode))
55-
)
48+
Text = "A dropdown alone",
49+
Buttons = new List<GriddlyButton>()
50+
{
51+
new GriddlyButton() { Text = "1" },
52+
new GriddlyButton() { Text = "2" },
53+
new GriddlyButton() { Text = "3" }
54+
}
55+
})
56+
.Add(new GriddlyExport<TestGridItem>("test-export", true));
57+
}
58+
59+
@Html.Griddly(grid)

Griddly/_AppStart.cshtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22

33
@{
44
//GriddlySettings.DefaultClassName = "table table-bordered table-hover";
5+
6+
GriddlySettings.HandleCustomExport = Griddly.Controllers.HomeController.HandleCustomExport;
57
}
68

0 commit comments

Comments
 (0)