Skip to content

Commit 1dac430

Browse files
committed
Merge pull request #47 from programcsharp/feature-history
Add history support for all griddly settings
2 parents 089640c + 47b2143 commit 1dac430

File tree

9 files changed

+131
-8
lines changed

9 files changed

+131
-8
lines changed

Build/CommonAssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("1.2.1")]
19-
[assembly: AssemblyFileVersion("1.2.1")]
18+
[assembly: AssemblyVersion("1.2.2")]
19+
[assembly: AssemblyFileVersion("1.2.2")]

Griddly/Content/griddly.css

+5
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

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public ActionResult Index()
1616
return View();
1717
}
1818

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

Griddly/Griddly.csproj

+1
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

+88-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,65 @@
3030
count: this.options.count
3131
});
3232

33+
var isLoadingHistory = false;
34+
35+
if (window.history && history.replaceState && history.state && history.state.griddly)
36+
{
37+
var state = history.state.griddly[this.options.url];
38+
39+
if (state && state.filterValues)
40+
{
41+
if (this.$element.prev(".griddly-init-flag").val() == "loaded")
42+
{
43+
try
44+
{
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");
54+
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+
}
68+
69+
this.refresh();
70+
}
71+
catch (e)
72+
{
73+
isLoadingHistory = false;
74+
}
75+
}
76+
else
77+
{
78+
// user refreshed page, go back to defaults
79+
delete history.state.griddly[this.options.url];
80+
81+
history.replaceState(history.state, document.title);
82+
}
83+
}
84+
}
85+
86+
if (!isLoadingHistory)
87+
{
88+
this.$element.removeClass("griddly-init");
89+
this.$element.prev(".griddly-init-flag").val("loaded");
90+
}
91+
3392
$("html").on("click", $.proxy(function (event)
3493
{
3594
if ($(event.target).parents('.popover.in').length == 0 && $(event.target).parents(".filter-trigger").length == 0 && !$(event.target).hasClass("filter-trigger"))
@@ -780,7 +839,7 @@
780839
value = date.toLocaleDateString();
781840
break;
782841
case "Currency":
783-
value = value.toFixed(2);
842+
value = parseFloat(value).toFixed(2);
784843
break;
785844
}
786845
}
@@ -871,6 +930,27 @@
871930

872931
var postData = this.buildRequest();
873932

933+
if (window.history && history.replaceState)
934+
{
935+
var state =
936+
{
937+
pageNumber: this.options.pageNumber,
938+
pageSize: this.options.pageSize,
939+
sortFields: this.options.sortFields,
940+
filterMode: this.getFilterMode(),
941+
filterValues: this.getFilterValues()
942+
};
943+
944+
var globalState = history.state || {};
945+
946+
if (!globalState.griddly)
947+
globalState.griddly = {};
948+
949+
globalState.griddly[this.options.url] = state;
950+
951+
history.replaceState(globalState, document.title);
952+
}
953+
874954
// TODO: cancel any outstanding calls
875955

876956
$.ajax(this.options.url,
@@ -931,6 +1011,9 @@
9311011
pageSize: currentPageSize,
9321012
count: count
9331013
});
1014+
1015+
this.$element.removeClass("griddly-init");
1016+
this.$element.prev(".griddly-init-flag").val("loaded");
9341017
}, this))
9351018
.fail($.proxy(function (xhr, status, errorThrown)
9361019
{
@@ -1218,9 +1301,12 @@
12181301
}
12191302
};
12201303

1221-
$(function()
1304+
$("[data-role=griddly]").griddly();
1305+
1306+
$(function ()
12221307
{
12231308
$("[data-role=griddly]").griddly();
1309+
12241310
$(document).on("click", "[data-role=griddly-button]", GriddlyButton.handleClick);
12251311

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

Griddly/Views/Home/Examples.cshtml

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717

1818
<h3>Other Random Examples/Tests</h3>
1919
@Html.Griddly("TestGrid")
20+
21+
@* TODO:
22+
Columns
23+
Buttons
24+
Filters
25+
Selection
26+
Grid settings
27+
Exports
28+
LINQ results
29+
Dapper results
30+
Templating
31+
Server hooks
32+
Client events
33+
Client methods
34+
*@
2035
</div>
2136
</div>
2237
<script>
@@ -54,7 +69,7 @@
5469
var grid = $(".griddly.filter-list-grid");
5570
var table = grid.find("table");
5671
57-
72+
5873
// some basic changes to make the grid more conducive to scrolling
5974
grid.find(".griddly-scrollable-container").css({ "overflow": "auto", "height": "250px" });
6075
grid.find("thead td").css({ "background-color": "white" });
@@ -72,7 +87,7 @@
7287
7388
grid.griddly("pageSize", 20);
7489
grid.griddly("refresh");
75-
90+
7691
return false;
7792
}
7893
</script>

Griddly/Views/Home/HistoryTest.cshtml

+10
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

+2-2
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

+1
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)