Copied to clipboard

Flag this post as spam?

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


  • Peter van Geffen 54 posts 290 karma points
    Feb 16, 2022 @ 11:37
    Peter van Geffen
    0

    Creating pagination in Umbraco Cloud (V9.3.0)

    In Umbraco 8 we had some code to create pagination. In Umbraco 9 (Cloud) it doesn't seem to work.

    Anyone some ideas?

    @{
    int pageSize = 1;
    IEnumerable<IPublishedContent> nodes = Umbraco.Content(Guid.Parse("5b8c723b-0b8b-44ef-80f4-9e4402afe54d")).ChildrenOfType("nieuwsItem").Where(x => x.IsVisible() && x.Value<DateTime>("datum") <= DateTime.Now.Date  && x.Value<DateTime>("datum") != null).OrderByDescending(x => x.Value<DateTime>("datum"));
    int totalNodes = nodes.Count();
    int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
    var page = 1;
    int.TryParse(Request.QueryString["page"], out page);
    if (page > totalPages){
        page = totalPages;
    }else if (page < 1){
        page = 1;
    }}
    
    @foreach (var item in nodes.Skip((page - 1) * pageSize).Take(pageSize)){
            <a href="@item.Url()" title="@item.Name()">@item.Name()</a>
    }
    
    @if (totalNodes > pageSize){
        <ul class="pagination">
            @if (page > 1){
                <li><a href="?page=@(page-1)">Prev</a></li>
            }
            @for (int p = 1; p < totalPages + 1; p++){
                string selected = (p == page) ? "selected" : String.Empty;
                <li class="@selected"><a href="?page=@p" title="Go to page @p">@p</a></li>
            }
            @if (page < totalPages){
                <li><a href="?page=@(page+1)">Next</a></li>
            }
        </ul>
    }
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 20, 2022 @ 03:39
    Dennis Aaen
    102

    Hi Peter,

    Hope that you are doing good. I have been trying to find a solution for you for the last couple of days.

    And today I got it to work with the following code

    First add the following lines below the using statements in your template

    @inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
    

    Then use the follow to make the create pagination in Umbraco 9

    @{
        int pageSize = 5;
        var nodes = Umbraco.Content(Guid.Parse("ec4aafcc-0c25-4f25-a8fe-705bfae1d324"))
        .Children()
        .Where(x => x.IsVisible());
        int totalNodes = nodes.Count();
        int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
        int page = 1;
        Int32.TryParse(HttpContextAccessor.HttpContext.Request.Query["page"], out page);  
    
    
    if (page > totalPages){
        page = totalPages;
    }else if (page < 1){
        page = 1;
    }
    
    
    }
    <ul>
        @foreach (var item in nodes.Skip((page - 1) * pageSize).Take(pageSize))
        {
            <li>
                <a href="@item.Url()">@item.Name()</a>
            </li>
        }
    </ul>
    
    
    
    
    
    @if (totalNodes > pageSize){
        <ul class="pagination">
            @if (page > 1){
                <li><a href="?page=@(page-1)">Prev</a></li>
            }
            @for (int p = 1; p < totalPages + 1; p++){
                string selected = (p == page) ? "selected" : String.Empty;
                <li class="@selected"><a href="?page=@p" title="Go to page @p">@p</a></li>
            }
            @if (page < totalPages){
                <li><a href="?page=@(page+1)">Next</a></li>
            }
        </ul>
    }
    

    Give it a go and remember to make changes so it matches your set up.

    I am looking forward to hearing from you if you can make it work too

    Best

    /Dennis

  • Peter van Geffen 54 posts 290 karma points
    Feb 21, 2022 @ 06:18
    Peter van Geffen
    0

    Dennis, you're the man! Thanks a lot! Works like a charm.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 21, 2022 @ 08:33
    Dennis Aaen
    1

    Hi Peter

    Great to hear that it works like a charm for you

    /Dennis

  • Valarmathi Ganessin 3 posts 73 karma points notactivated
    May 26, 2022 @ 09:58
    Valarmathi Ganessin
    0

    Please help me I am new to Umbraco Cloud Please help me how to do the the changes to the above template to get pagination may i have to create it as Macro or Partial ?

  • Valarmathi Ganessin 3 posts 73 karma points notactivated
    May 26, 2022 @ 15:23
    Valarmathi Ganessin
    0

    I got the answer it works well thanks

  • 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