Copied to clipboard

Flag this post as spam?

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


  • abdo 2 posts 32 karma points
    Feb 21, 2014 @ 18:08
    abdo
    0

    Paging through a query result - help

    Let’s say I have 24 items that I want to display in a page using some kind of paging (these items are contents from my umbraco backlog)

    On initial load I want to load only 5 items and have a ‘see more’ button/link to display the next 5 items (when clicked)

    At this point it should only show initial 5 and the next 5 items on the page, and upon every ‘see more’ button/link click it should continue to append the current number of items + 5, until there is no more items (in this case 24 items are all displayed).

    I have tried doing the following code (not sure where I am going wrong, or if there is a better solution to do this)

     

    int count = 5; //so it can initally load 5 items
    int pages = Convert.ToInt32(this.Request.QueryString["pages"]);
    int page = pages * 5; //ex. page 1 load 5 items, page 2 load 10 items

     

        if (items.Count() > 0)
        {
            foreach (Item item in items)
            {
                if (!this.Request.QueryString["pages"] == pages.ToString())
                {
                        if (count % 5 != 0 && remainingItems > 0 || count == 5)
                        {
                           HTML CODE

                            remainingItems--;
                            count++;

                        }

                        else if (count % 5 == 0 && remainingItems > 0)
                        {
                           HTML Code
                         
                            pages++; //this appends the page number
                            remainingItem--;

                            <a href="url">See more</a>
                        }

                    }
                }
            }

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Feb 21, 2014 @ 23:24
    Fuji Kusaka
    100

    Hi Abdo,

    Here you can find a good reference for paging 
    http://www.diplo.co.uk/blog/2011/6/21/creating-a-paged-list-in-umbraco-using-razor.aspx

     

    Anyways here is how i usually do it

    int pageSize = 5; // Number of News per page
    int page = 1; // The page we are viewing
    
                            if (!int.TryParse(Request.QueryString["page"], out page))
                            {
                                page = 1;
                            }
    
                    var totalNodes = item.Count();
    
                    int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
    
                        if(page > totalPages ){
                            page = totalPages;
                        }
    
                        else if( page < 1 ){
                            page = 1;
                        }
    
                        foreach(dynamic node in items.Skip((page -1) * pageSize).Take(pageSize)){
                            @node.Name
                        }                   
    
                        <ul id="paging">
                            @for(int p =1; p < totalPages + 1; p++){
                                string select = (p == page)? "selected": "";
                                    <li class="@select"><a href="?page=@p">@p</a></li>
                                    }
                                </ul>
  • abdo 2 posts 32 karma points
    Feb 22, 2014 @ 12:18
    abdo
    0

    fantastic!! thanks alot Fuji

Please Sign in or register to post replies

Write your reply to:

Draft