Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • hetaurhet 245 posts 267 karma points
    Jun 05, 2012 @ 08:56
    hetaurhet
    0

    paging enhancement needed

    Hello

    I have PagerHelper class created as follows which will show prev, next buttons and in between all page buttons.

    e.g. Prev - 1 - 2 -3 - 4 - 5 - 6 -7 - Next

    But I now need some thing like ... Prev - 1 - 2 -3 - ... - Next

    On clicking "..." it should show next 3 numbers say for e.g.     Prev - 4 - 5 - 6 - ... - Next

    My current code is as follows....

    public class PagerHelper
    {
    @helper GeneratePaging(int currentPage, int totalPages)
    {

    string url = HttpContext.Current.Request.Url.AbsoluteUri;

    string link = "/";

    if (totalPages > 1)
    {
    //<ul class="paging">
    if(currentPage-1 > 0)
    {
    int prev_p = currentPage-1;

    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", prev_p.ToString());
    //<li>
    <a class="pager-btn" href="@link">Prev</a>
    //</li>
    }
    else
    {
    <span class="aspNetDisabled pager-btn">Prev</span>
    }
    for (int p = 1; p < totalPages + 1; p++)
    {
    //string selected = (p == currentPage) ? "selected" : String.Empty;
    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", p.ToString());
    if(p == currentPage)
    {

    //<li>
    <a class="pager-btn current-pager-btn" href="@link" title="Go to page @p of results">@p</a>
    //</li>
    }
    else
    {
    //<li>
    <a class="pager-btn" href="@link" title="Go to page @p of results">@p</a>
    //</li>
    }
    }
    if(currentPage < totalPages)
    {
    int next_p = currentPage+1;

    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", next_p.ToString());

    //<li>
    <a class="pager-btn" href="@link">Next</a>
    //</li>
    }
    else
    {
    <span class="aspNetDisabled pager-btn">Next</span>
    }
    //</ul>
    }
    }
    }

    Also, I have one more file Pager.cs as follows to return nodes.

    public static class Pager
    {
    public static DynamicNodeList Paged(this DynamicNodeList nodes, int page, int pageSize)
    {
    return new DynamicNodeList(nodes.Items.Skip((page - 1) * pageSize).Take(pageSize));
    }

    }

    So, can anyone tell now how to implement the same?

     

  • michael Netonline 18 posts 62 karma points
    Jun 07, 2012 @ 12:45
    michael Netonline
    0

    like?

     

    var skipPage = currentPage + 3;

      for (int p = currentPage; p <= totalPages && p < skipPage; p++)
                                    {
                                            /
    /string selected =(p == currentPage)?"selected":String.Empty;
                                            link
    =GlobalRazorHelpers.AddOrReplaceParam(url,"page", p.ToString());
                                           
    if(p == currentPage)
                                           
    {
                                                   
                                             
    //<li>
                                                   
    <a class="pager-btn current-pager-btn" href="@link" title="Go to page @p of results">@p</a>
                                             /
    /</li>
                                           
    }
                                           
    else
                                           
    {
                                             
    //<li>
                                                   
    <a class="pager-btn" href="@link" title="Go to page @p of results">@p</a>
                                             /
    /</li>
                                           
    }
                                   
    }  

    @if (skipPage <= totalPages)

    {

        <a class="pager-btn" href="@Model.Url?page=@skipPage" title="">...</a>

        }

     

  • hetaurhet 245 posts 267 karma points
    Jun 09, 2012 @ 06:32
    hetaurhet
    0

    Thank you for the response. It was helpful. But I made few changes and finally following code works fine for me.

    public class PagerHelper
    {
    @helper GeneratePaging(int currentPage, int totalPages)
    {

    string url = HttpContext.Current.Request.Url.AbsoluteUri;

    string link = "/";

    int ButtonsCount = 3;

    if (totalPages > 1)
    {
    int cur_page = currentPage - 1;
    //finding the first linkbutton to be shown in the current display
    int start = cur_page - (cur_page % ButtonsCount);

    //finding the last linkbutton to be shown in the current display
    int end = cur_page + (ButtonsCount - (cur_page % ButtonsCount));

    if (currentPage-1 > 0)
    {
    //specify the position of prev button changed to -1

    int prev_p = currentPage - 1;

    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", prev_p.ToString());

    <a class="pager-btn" href="@link">Prev</a>


    }

    if (start > ButtonsCount - 1)
    {

    int dot = start - 1;
    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", dot.ToString());
    <a class="pager-btn" href="@link">...</a>

    }

    int i = 0, j = 0,page=0;

    for (i = start; i < end; i++)
    {

    if (i < totalPages)
    {
    if (i+1 == currentPage)
    {
    page = i + 1;
    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", page.ToString());
    <a class="pager-btn current-pager-btn" href="@link">@page</a>
    }
    else
    {
    page = i + 1;
    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", page.ToString());
    <a class="pager-btn" href="@link">@page</a>

    }
    }
    j++;
    }

    if (totalPages > end)
    {

    int dot2 = i + 1;
    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", dot2.ToString());

    <a class="pager-btn" href="@link">...</a>

    }

    //condition added to keep the next button till the click of last button
    if (currentPage < totalPages)
    {
    int next_p = currentPage + 1;

    link = GlobalRazorHelpers.AddOrReplaceParam(url, "page", next_p.ToString());

    <a class="pager-btn" href="@link">Next</a>
    }


    }
    }
    }

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies