Skip to content

Commit cf03058

Browse files
committed
Add hidden input flag to griddly template so we know whether page was reached via back button
If reached via back button, initially display:none grid and only show after refresh If reached normally or via page refresh, leave default filters
1 parent 879d40e commit cf03058

File tree

7 files changed

+74
-19
lines changed

7 files changed

+74
-19
lines changed

Griddly/Content/griddly.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
white-space:nowrap;
2727
}
2828

29+
.griddly-init-flag[value=loaded] + .griddly.griddly-init
30+
{
31+
display:none
32+
}
33+
2934
.griddly th
3035
{
3136
background-color: #c8c8c8;

Griddly/Controllers/HomeController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public ActionResult Index()
1515
return View();
1616
}
1717

18+
public ActionResult HistoryTest()
19+
{
20+
return View();
21+
}
22+
1823
public GriddlyResult TestGrid(string firstName, int? zipStart, int? zipEnd)
1924
{
2025
IQueryable<TestGridItem> query = _testData;

Griddly/Griddly.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
<Content Include="Views\Home\IndexGrid.cshtml" />
250250
<Content Include="Views\Shared\Griddly\GriddlyFilterInline.cshtml" />
251251
<Content Include="Views\Shared\Griddly\GriddlyFilterForm.cshtml" />
252+
<Content Include="Views\Home\HistoryTest.cshtml" />
252253
</ItemGroup>
253254
<ItemGroup>
254255
<ProjectReference Include="..\Griddly.Mvc\Griddly.Mvc.csproj">

Griddly/Scripts/griddly.js

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,65 @@
3030
count: this.options.count
3131
});
3232

33+
var isLoadingHistory = false;
34+
3335
if (history.state && history.state.griddly)
3436
{
3537
var state = history.state.griddly[this.options.url];
3638

3739
if (state && state.filterValues)
3840
{
39-
this.options.pageNumber = state.pageNumber;
40-
this.options.pageSize = state.pageSize;
41-
this.options.sortFields = state.sortFields;
42-
this.setFilterMode(state.filterMode, true);
43-
this.setFilterValues(state.filterValues, false, true);
44-
45-
$("[data-griddly-sortfield], .griddly-filters-inline td", this.$element).removeClass("sorted_a sorted_d");
46-
47-
if (this.options.sortFields)
41+
if (this.$element.prev(".griddly-init-flag").val() == "loaded")
4842
{
49-
for (var i = 0; i < this.options.sortFields.length; i++)
43+
try
5044
{
51-
var sort = this.options.sortFields[i];
45+
isLoadingHistory = true;
46+
47+
this.options.pageNumber = state.pageNumber;
48+
this.options.pageSize = state.pageSize;
49+
this.options.sortFields = state.sortFields;
50+
this.setFilterMode(state.filterMode, true);
51+
this.setFilterValues(state.filterValues, false, true);
52+
53+
$("[data-griddly-sortfield], .griddly-filters-inline td", this.$element).removeClass("sorted_a sorted_d");
5254

53-
var header = $("th[data-griddly-sortfield='" + sort.Field + "']", this.$element);
54-
var inlineFilter = $(".griddly-filters-inline")[0].cells[header[0].cellIndex];
55+
if (this.options.sortFields)
56+
{
57+
for (var i = 0; i < this.options.sortFields.length; i++)
58+
{
59+
var sort = this.options.sortFields[i];
60+
61+
var header = $("th[data-griddly-sortfield='" + sort.Field + "']", this.$element);
62+
var inlineFilter = $(".griddly-filters-inline")[0].cells[header[0].cellIndex];
63+
64+
header.addClass(sort.Direction == "Ascending" ? "sorted_a" : "sorted_d");
65+
$(inlineFilter).addClass(sort.Direction == "Ascending" ? "sorted_a" : "sorted_d");
66+
}
67+
}
5568

56-
header.addClass(sort.Direction == "Ascending" ? "sorted_a" : "sorted_d");
57-
$(inlineFilter).addClass(sort.Direction == "Ascending" ? "sorted_a" : "sorted_d");
69+
this.refresh();
70+
}
71+
catch (e)
72+
{
73+
isLoadingHistory = false;
5874
}
5975
}
76+
else
77+
{
78+
// user refreshed page, go back to defaults
79+
delete history.state.griddly[this.options.url];
6080

61-
this.refresh();
81+
history.replaceState(history.state);
82+
}
6283
}
6384
}
6485

86+
if (!isLoadingHistory)
87+
{
88+
this.$element.removeClass("griddly-init");
89+
this.$element.prev(".griddly-init-flag").val("loaded");
90+
}
91+
6592
$("html").on("click", $.proxy(function (event)
6693
{
6794
if ($(event.target).parents('.popover.in').length == 0 && $(event.target).parents(".filter-trigger").length == 0 && !$(event.target).hasClass("filter-trigger"))
@@ -981,6 +1008,9 @@
9811008
pageSize: currentPageSize,
9821009
count: count
9831010
});
1011+
1012+
this.$element.removeClass("griddly-init");
1013+
this.$element.prev(".griddly-init-flag").val("loaded");
9841014
}, this))
9851015
.fail($.proxy(function (xhr, status, errorThrown)
9861016
{
@@ -1268,9 +1298,12 @@
12681298
}
12691299
};
12701300

1271-
$(function()
1301+
$("[data-role=griddly]").griddly();
1302+
1303+
$(function ()
12721304
{
12731305
$("[data-role=griddly]").griddly();
1306+
12741307
$(document).on("click", "[data-role=griddly-button]", GriddlyButton.handleClick);
12751308

12761309
// patch bootstrap js so it doesn't .empty() our inline filter dropdowns

Griddly/Views/Home/HistoryTest.cshtml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
ViewBag.Title = "History Test";
3+
}
4+
5+
<div class="row">
6+
<div class="col-md-12">
7+
<h2>History Test</h2>
8+
@Html.Griddly("IndexGrid")
9+
</div>
10+
</div>

Griddly/Views/Shared/Griddly/Griddly.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
@if (isFirstRender)
8888
{
8989
SortField[] defaultSort = settings.DefaultSort;
90-
91-
@:<div class="griddly @settings.ClassName" data-role="griddly"
90+
<input class="griddly-init-flag" type="hidden" />
91+
@:<div class="griddly griddly-init @settings.ClassName" data-role="griddly"
9292
@: @Html.AttributeNullable("data-griddly-url", !simple ? Url.Current() : null)
9393
@: data-griddly-count="@Model.Total"
9494
@: @Html.AttributeNullable("data-griddly-filtermode", settings.InitialFilterMode != FilterMode.None ? settings.InitialFilterMode.ToString() : null)

Griddly/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<div class="navbar-collapse collapse">
2424
<ul class="nav navbar-nav">
2525
<li class="@(ViewContext.RouteData.Values["action"].ToString() == "examples" ? "active" : null)">@Html.ActionLink("Examples", "Examples")</li>
26+
<li class="@(ViewContext.RouteData.Values["action"].ToString() == "historytest" ? "active" : null)">@Html.ActionLink("History Test", "HistoryTest")</li>
2627
<li><a href="https://github.com/programcsharp/griddly/issues">Issues</a></li>
2728
<li class="@(ViewContext.RouteData.Values["action"].ToString() == "about" ? "active" : null)">@Html.ActionLink("About", "About")</li>
2829
</ul>

0 commit comments

Comments
 (0)