I'm having some trouble figuring out a paging mechanism in Umbracos Razor viewengine using AJAX.
Thing is, that the paging needs to be like this:
1 | 2 | 3 | 4 ... 16 Next >>
When the user has clicked on anything greater than 4
it needs to be like this:
<< Previous 1 ... 6 | 7 | 8 | 9 | 10 ... 16 Next >>
So it displays the previous two pages and the next to pages.
Now, when a user clicks all the way to the last 4 pages, the paging needs to be like this:
<< Previous 1 ... 13 | 14 | 15 | 16
I have got it working, but the code is ... not pretty, to say the least. It's clumsy and I've got a feeling that it could be made a lot more simple than it is - just not sure how exactly :-)
The code (make sure you have a nice, warm cup of coffee ;-))
@* Paging *@
var resultCount = result.Count();
if(resultCount > take)
{
<div class="paging">
@{
int previous = pageNumber - 1;
if(previous >= 0)
{
<a class="previous" id="prev" href="#" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @previous, '@action', '@ingredientIds', '@ingredientId');"><img src="/img/buttons/pink-hand-left.png" />@umbraco.library.GetDictionaryItem("Previous")</a>
}
double numOfPagingLinks = Convert.ToDouble(resultCount) / take;
int roundedNumOfPagingLinks = Convert.ToInt32(Math.Floor(numOfPagingLinks));
int lastPage = roundedNumOfPagingLinks + 1;
if(lastPage > 4)
{
// Always show first page number
<a href="#" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', '0', '@action', '@ingredientIds', '@ingredientId');">1</a>
if(pageNumber + 1 == lastPage)
{
<span> ... </span>
for(int i = lastPage - 4; i < lastPage - 1; i++)
{
int pagingNumber = i;
int numberToDisplay = i + 1;
string className = i == pageNumber ? "active" : "inactive";
<a href="#" class="@className" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @pagingNumber, '@action', '@ingredientIds', '@ingredientId');">@numberToDisplay</a>
<span> | </span>
}
}
else
{
if(pageNumber < 3)
{
for(int i = 1; i < 5; i++)
{
int pagingNumber = i;
int numberToDisplay = i + 1;
string className = i == pageNumber ? "active" : "inactive";
if(i == 1)
{
<span> | </span>
}
<a href="#" class="@className" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @pagingNumber, '@action', '@ingredientIds', '@ingredientId');">@numberToDisplay</a>
if(i < 4)
{
<span> | </span>
}
else
{
<span> ... </span>
}
}
}
else
{
if(pageNumber == 3)
{
for(int i = 1; i < 6; i++)
{
int pagingNumber = i;
int numberToDisplay = i + 1;
string className = i == pageNumber ? "active" : "inactive";
<a href="#" class="@className" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @pagingNumber, '@action', '@ingredientIds', '@ingredientId');">@numberToDisplay</a>
if(i < 5)
{
<span> | </span>
}
else
{
<span> ... </span>
}
}
}
else if(pageNumber > 3)
{
<span> ... </span>
if(pageNumber >= lastPage - 4)
{
for(int i = pageNumber - 2; i < lastPage - 1; i++)
{
int pagingNumber = i;
int numberToDisplay = i + 1;
string className = i == pageNumber ? "active" : "inactive";
<a href="#" class="@className" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @pagingNumber, '@action', '@ingredientIds', '@ingredientId');">@numberToDisplay</a>
<span> | </span>
}
}
else
{
var firstPrevious = pageNumber - 1;
var secondPrevious = pageNumber - 2;
var firstPreviousToDisplay = firstPrevious + 1;
var secondPreviousToDisplay = secondPrevious + 1;
<a href="#" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @secondPrevious, '@action', '@ingredientIds', '@ingredientId');">@secondPreviousToDisplay</a>
<span> | </span>
<a href="#" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @firstPrevious , '@action', '@ingredientIds', '@ingredientId');">@firstPreviousToDisplay </a>
<span> | </span>
for(int i = pageNumber; i < pageNumber + 3; i++)
{
int pagingNumber = i;
int numberToDisplay = i + 1;
string className = i == pageNumber ? "active" : "inactive";
<a href="#" class="@className" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @pagingNumber, '@action', '@ingredientIds', '@ingredientId');">@numberToDisplay</a>
if(i < pageNumber + 2)
{
<span> | </span>
}
}
<span> ... </span>
}
}
}
}
// Always show last pagenumber
<a href="#" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @roundedNumOfPagingLinks, '@action', '@ingredientIds', '@ingredientId');">@lastPage</a>
}
else
{
for (int i = 0; i < roundedNumOfPagingLinks + 1; i++)
{
int pagingNumber = i;
int numberToDisplay = i + 1;
string className = i == pageNumber ? "active" : "inactive";
<a href="#" class="@className" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @pagingNumber, '@action', '@ingredientIds', '@ingredientId');">@numberToDisplay</a><span> | </span>
}
}
int next = pageNumber + 1;
if (next <= roundedNumOfPagingLinks)
{
<a class="next" id="next" href="#" onclick="ajaxFilterSearch('@themeIds', '@video', '@brochure', @next, '@action', '@ingredientIds', '@ingredientId');">@umbraco.library.GetDictionaryItem("Next")<img src="/img/buttons/pink-hand-right.png" /></a>
}
}
</div>
}
The result
variable is the collection found in the filter/search. The pageNumber
variable is the page number that the user has clicked.
I know the above code is complex and not pretty, but any help/hint is greatly appreciated!
Thanks in advance.