Ok, so I am doing a little happy dance right now, because I managed to get pagination into our application in less than a day. It is not your traditional pagination: where you specify the page number (and if you are lucky enough, also the page size). I find no real user meaning behind this. Instead, it allows you to specfy the range you want to look at. So, for the first 20 items, you would look at the items starting at 1 and ending at 20. To look at the next 20 items, you start at 21 and ending at 40. So far, that is just a different implementation to Page 2. Now, suppose the items that are actually interesting to you are items 10-30. In the Page model, you need to go back and forth between page 1 and 2. In this model, you only need to look at items starting at 10 and ending at 30. Brilliant huh! Nice implementations of this would be sliding windows, facebook style “more items”, and the version that we have (a hybrid between predefined pages and the ability to specify an exact range).
Here is how I did it (tests and other junk removed for readability):
<div><span>There are currently <spanclass="treasure-count"><%= Pages.MaximumCount %></span> treasures available.</span></div><divclass="pagination custom-page-select"><divclass="options">To view specific treasures, please enter the number to start at <%= (RawHtml)Html.TextBox("StartingAt", Pages.CurrentPage.StartingAt)%> and to finish at <%= (RawHtml)Html.TextBox("EndingAt", Pages.CurrentPage.EndingAt)%> and click <%=(RawHtml)Html.ActionLink("Go", "Show", "Treasure", new { }, new { id = "paginateDirectly" })%></div><divclass="clear"/></div><divclass="treasures-list"><% new TreasuresRenderer().RenderTreasures(Pages.PaginatedCollection); %>
</div><divclass="pagination menu"><spanclass="info">Please select a range of treasures to view.</span><ul><% foreach (var page in Pages) { %>
<liclass="pages<%= Pages.IsTheCurrentPage(page)? "currentPage":string.Empty%>"><%=(RawHtml) Html.ActionLink(page.Name(), "Show", "Treasure", new {startingAt=page.StartingAt,endingAt=page.EndingAt}, new {}) %></li><% } %>
</ul></div>
Helpful Javascript
1234567891011121314151617181920212223242526272829
functionSetupPagination(){$(".pages a").click(function(){$("#content-main-container").load($(this).attr('href'),function(){SetupPagination();});returnfalse;});$('a#paginateDirectly').click(function(){varurl=$(this).attr('href')+'?StartingAt='+$('input[name="StartingAt"]').val()+'&EndingAt='+$('input[name="EndingAt"]').val();$("#content-main-container").load(url,function(){SetupPagination();});returnfalse;});$('input[name="StartingAt"]').numeric().limit(9);$('input[name="EndingAt"]').numeric().limit(9);$('input[name="EndingAt"]').keyup(function(e){varkey=e.charCode?e.charCode:e.keyCode?e.keyCode:0;// return should trigger the paginationif(key==13){$('a#paginateDirectly').click();}});}