Skip to content

Commit 2a4251d

Browse files
committed
Filter defaults example
1 parent b01baf6 commit 2a4251d

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

Griddly/Controllers/ExampleController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public ActionResult Filters()
3333
});
3434
}
3535

36+
public ActionResult FilterDefaults()
37+
{
38+
return View("Example", new ExampleModel()
39+
{
40+
Title = "Filter Defaults Example",
41+
GridAction = "FilterDefaultsGrid",
42+
ParentView = "FilterDefaults.cshtml",
43+
GridView = "FilterDefaultsGrid.cshtml",
44+
Description = "Filter default values may be set using the ControllerBase.SetGriddlyDefault() extension method."
45+
});
46+
}
47+
3648
public ActionResult Parameters()
3749
{
3850
return View("Example", new ExampleModel()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Griddly.Models;
2+
using Griddly.Mvc;
3+
using Griddly.Mvc.Results;
4+
using System;
5+
using System.Linq;
6+
using System.Web.Mvc;
7+
8+
namespace Griddly.Controllers
9+
{
10+
public partial class ExampleController : Controller
11+
{
12+
public GriddlyResult FilterDefaultsGrid(string[] item, string lastName, bool? isApproved)
13+
{
14+
this.SetGriddlyDefault(ref isApproved, nameof(isApproved), true);
15+
16+
this.SetGriddlyDefault(ref item, nameof(item), new[] { "Item1", "Item2" });
17+
18+
IQueryable<TestGridItem> query = _testData.AsQueryable();
19+
20+
if (item != null && item.Any())
21+
query = query.Where(x => item.Contains(x.Item));
22+
23+
if (!string.IsNullOrWhiteSpace(lastName))
24+
query = query.Where(x => x.LastName.IndexOf(lastName, StringComparison.InvariantCultureIgnoreCase) > -1);
25+
26+
if (isApproved != null)
27+
query = query.Where(x => x.IsApproved == isApproved.Value);
28+
29+
return new QueryableResult<TestGridItem>(query);
30+
}
31+
}
32+
}

Griddly/Griddly.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<Compile Include="Controllers\Examples\ColumnsGrid.cs">
130130
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
131131
</Compile>
132+
<Compile Include="Controllers\Examples\FilterDefaultsGrid.cs">
133+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
134+
</Compile>
132135
<Compile Include="Controllers\Examples\ParametersGrid.cs">
133136
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
134137
</Compile>
@@ -289,6 +292,8 @@
289292
<Content Include="Views\Example\Parameters.cshtml" />
290293
<Content Include="Views\Example\Columns.cshtml" />
291294
<Content Include="Views\Example\Filters.cshtml" />
295+
<Content Include="Views\Example\FilterDefaultsGrid.cshtml" />
296+
<Content Include="Views\Example\FilterDefaults.cshtml" />
292297
</ItemGroup>
293298
<ItemGroup>
294299
<ProjectReference Include="..\Griddly.Mvc\Griddly.Mvc.csproj">
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@Html.Griddly("FilterDefaultsGrid")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@{
2+
List<SelectListItem> groupedList = new List<SelectListItem>()
3+
{
4+
new SelectListItemGroup()
5+
{
6+
Text = "Group1",
7+
Items = new List<SelectListItem>()
8+
{
9+
new SelectListItem() { Text = "Item1", Value = "Item1" },
10+
new SelectListItem() { Text = "Item2", Value = "Item2" },
11+
new SelectListItem() { Text = "Item3", Value = "Item3" },
12+
new SelectListItem() { Text = "Item4", Value = "Item4" },
13+
new SelectListItem() { Text = "Item5", Value = "Item5" },
14+
}
15+
},
16+
new SelectListItemGroup()
17+
{
18+
Text = "Group2",
19+
Items = new List<SelectListItem>()
20+
{
21+
new SelectListItem() { Text = "Item6", Value = "Item6" },
22+
new SelectListItem() { Text = "Item7", Value = "Item7" },
23+
new SelectListItem() { Text = "Item8", Value = "Item8" },
24+
new SelectListItem() { Text = "Item9", Value = "Item9" },
25+
new SelectListItem() { Text = "Item10", Value = "Item10" },
26+
}
27+
}
28+
};
29+
30+
var gridSettings = new GriddlySettings<TestGridItem>()
31+
{
32+
PageSize = 20,
33+
RowClickUrl = x => "http://microsoft.com",
34+
EmptyGridMessageTemplate =@<text><div class="alert alert-warning">@item.Settings.EmptyGridMessage</div></text>,
35+
EmptyGridMessage = "Sorry, no records were found"
36+
};
37+
38+
gridSettings
39+
.Column(x => x.Id)
40+
.Column(x => x.Item, filter: x => x.FilterList(groupedList, field: "item"))
41+
.Column(x => x.FirstName, "First Name")
42+
.Column(x => x.LastName, "Last Name", filter: x => x.FilterBox(FilterDataType.String, field: "lastName"))
43+
.Column(x => x.Address)
44+
.Column(x => x.City)
45+
.Column(x => x.State)
46+
.Column(x => x.PostalCode, "Zip")
47+
.Column(x => x.IsApproved, "Approved", filter: x => x.FilterBool(nullItemText: "Both", field: "isApproved"));
48+
}
49+
50+
@Html.Griddly(gridSettings)

Griddly/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ul class="dropdown-menu">
3030
<li>@Html.ActionLink("Columns", "Columns", "Example")</li>
3131
<li>@Html.ActionLink("Filters", "Filters", "Example")</li>
32+
<li>@Html.ActionLink("Filter Defaults", "FilterDefaults", "Example")</li>
3233
<li>@Html.ActionLink("Additional Parameters", "Parameters", "Example")</li>
3334
</ul>
3435
</li>

0 commit comments

Comments
 (0)