Copied to clipboard

Flag this post as spam?

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


  • James 251 posts 1169 karma points
    Jul 22, 2014 @ 13:00
    James
    0

    Item Paging in Umbraco

    I have a portfolio page that displays a list of all my portfolio items using a Macro Partial View.

     

    Does anyone have a guide or knwo the best way to go about adding paging funcionality to this type of view?

     

    Kind regards.

  • Dan 1285 posts 3917 karma points c-trib
    Jul 22, 2014 @ 13:11
    Dan
    102

    Hi James,

    Here's some razor I've used to do paging - you should be able to pick something from it:

    @{
        var pageSize = 5;
        if(Model.Content.HasValue("numberOfItemsPerPage")){
            pageSize = Model.Content.GetPropertyValue<int>("numberOfItemsPerPage");
        }
        var page = 1; int.TryParse(Request.QueryString["page"], out page);
        var items = Umbraco.TypedContent(Model.Content.Id).Children.Where(x => x.DocumentTypeAlias == "Article" && x.IsVisible());
        var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);
    
        if (page > totalPages)
        {
            page = totalPages;
        }
        else if (page < 1)
        {
            page = 1;
        }
    
    
        foreach(var item in items.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x=>x.Name)){
            <div>
                <h2><a href="@item.Url">@item.Name</a></h2>
                <p>@item.GetPropertyValue("introText")</p>
            </div>
        }
    
    
        if (totalPages > 1)
        {
            <div class="pagination">
                <ul>
                    @if (page > 1)
                    {
                        <li><a href="?page=@(page-1)">Prev</a></li>
                    }
                    @for (int p = 1; p < totalPages + 1; p++)
                    {
                        <li class="@(p == page ? "active" : string.Empty)">
                            <a href="?page=@p">@p</a>
                        </li>
                    }
                    @if (page < totalPages)
                    {
                        <li><a href="?page=@(page+1)">Next</a></li>
                    }
                </ul>
            </div>
        }
    }

     

  • Shin Jin 7 posts 87 karma points
    Nov 25, 2015 @ 06:39
    Shin Jin
    0

    About this

    foreach(var item in items.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x=>x.Name)){
        <div>
            <h2><a href="@item.Url">@item.Name</a></h2>
            <p>@item.GetPropertyValue("introText")</p>
        </div>
    }
    

    Help what if i want to display image?

  • Søren Mastrup 122 posts 563 karma points c-trib
    Nov 25, 2015 @ 07:25
    Søren Mastrup
    1

    It would be something like this:

    foreach(var item in items.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x=>x.Name)){
        <div>
            <h2><a href="@item.Url">@item.Name</a></h2>
            <p>@item.GetPropertyValue("introText")</p>
            <img src="Umbraco.Media(item.MyMediaPickerAlias).Url" alt="" >
        </div>
    }
    
  • Shin Jin 7 posts 87 karma points
    Nov 25, 2015 @ 07:49
    Shin Jin
    0

    Thanks Søren Mastrup !

    About

    @item.GetPropertyValue("introText")

    can i do something like

    @item.GetPropertyValue("introText, 240, true")

    erm something like @Umbraco.Truncate(post.content, 150, true)

  • Søren Mastrup 122 posts 563 karma points c-trib
    Nov 25, 2015 @ 07:54
    Søren Mastrup
    0

    Pretty close! This should work:

    @Umbraco.Truncate(item.GetPropertyValue("introText"), 150)
    
  • Shin Jin 7 posts 87 karma points
    Nov 25, 2015 @ 08:02
    Shin Jin
    0

    @{ var pageSize = 5; if(Model.Content.HasValue("numberOfItemsPerPage")){ pageSize = Model.Content.GetPropertyValue

    if (page > totalPages)
    {
        page = totalPages;
    }
    else if (page < 1)
    {
        page = 1;
    }
    
    
    foreach(var item in items.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x=>x.Name)){
        <div>
            <h2><a href="@item.Url">@item.Name</a></h2>
            <p>@Umbraco.Truncate(item.GetPropertyValue("introText"), 150)</p>
        </div>
    }
    
    
    if (totalPages > 1)
    {
        <div class="pagination">
            <ul>
                @if (page > 1)
                {
                    <li><a href="?page=@(page-1)">Prev</a></li>
                }
                @for (int p = 1; p < totalPages + 1; p++)
                {
                    <li class="@(p == page ? "active" : string.Empty)">
                        <a href="?page=@p">@p</a>
                    </li>
                }
                @if (page < totalPages)
                {
                    <li><a href="?page=@(page+1)">Next</a></li>
                }
            </ul>
        </div>
    }
    

    }

    hmmmm it not working ...got the error....it show this... Compiler Error Message: CS1502: The best overloaded method match for 'Umbraco.Web.UmbracoHelper.Truncate(System.Web.IHtmlString, int)' has some invalid arguments error i guess is about this @Umbraco.Truncate(item.GetPropertyValue("introText"), 150)

    sorry for the trouble.... my coding very poor..

  • Søren Mastrup 122 posts 563 karma points c-trib
    Nov 25, 2015 @ 08:08
    Søren Mastrup
    1

    What do you get if you just output item.GetPropertyValue("introText").GetType()?

    As you can see in your error, Umbraco.Truncate only accepts a string.

    You could also try:

    @Umbraco.Truncate(item.GetPropertyValue("introText").ToString(), 150)
    
  • Shin Jin 7 posts 87 karma points
    Nov 25, 2015 @ 08:15
    Shin Jin
    0

    Thanks you so much Søren Mastrup it work OMG!! Sorry for the trouble.... and Thank you :)

  • Nelson 94 posts 246 karma points
    Mar 08, 2016 @ 07:38
    Nelson
    0

    Hello, I'm new with Umbraco and I'm working with my first website, I'm trying to implement the feature of pagination following the code displayed in this post. I'm using LeBlender for my grid editors, but I'm getting this error:

    c:...\umbraco\Views\Partials\Work.cshtml(5): error CS1061: 'Lecoati.LeBlender.Extension.Models.LeBlenderModel' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'Lecoati.LeBlender.Extension.Models.LeBlenderModel' could be found (are you missing a using directive or an assembly reference?)

    And here is my code:

    @inherits UmbracoViewPage<Lecoati.LeBlender.Extension.Models.LeBlenderModel    @{
    var pageSize = 6;
    if(Model.Content.HasValue("numberOfItemsPerPage")){
        pageSize = Model.Content.GetPropertyValue<int>("numberOfItemsPerPage");
    }
    var page = 1; int.TryParse(Request.QueryString["page"], out page);
    var items = Model.Items;
    var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);
    
    if (page > totalPages)
    {
        page = totalPages;
    }
    else if (page < 1)
    {
        page = 1;
    }
    
    
    foreach(var item in items.Skip((page - 1) * pageSize).Take(pageSize).OrderBy(x=>x.Name)){
    
         <h2><a href="#">Olaaaaaaaa</a></h2>    
    }
    
    
    if (totalPages > 1)
    {
        <div class="pagination">
            <ul>
                @if (page > 1)
                {
                    <li><a href="?page=@(page-1)">Prev</a></li>
                }
                @for (int p = 1; p < totalPages + 1; p++)
                {
                    <li class="@(p == page ? "active" : string.Empty)">
                        <a href="?page=@p">@p</a>
                    </li>
                }
                @if (page < totalPages)
                {
                    <li><a href="?page=@(page+1)">Next</a></li>
                }
            </ul>
        </div>
    }
    

    }

    Would really appreciate some help on this.

    Thank you very much!

Please Sign in or register to post replies

Write your reply to:

Draft