Copied to clipboard

Flag this post as spam?

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


  • Alex 30 posts 120 karma points
    Feb 21, 2015 @ 02:43
    Alex
    0

    Question about macros to list children / filter children.

     

    Hi, I have two questions about macros meant to list children elements. It is slowling me down on my blog unfortunately :( So I figured I should ask for advice.

    First, with my current macro, I list children just fine, unless there is only one, and then it does not render anything. I basically have a parent element with 5 children, but usually only one or two of them will be published at the same time. When it is one the issue occurs. Could anyone have a peek at the following code and let me know if they spot something obvious?

    @{ foreach (var page in CurrentPage.Children.Where("Visible")) {

    } }

    Secondly, is there an easy way to let's say render only 5 children element on one page (.Take() I presume),and then the next 5 on a second page using the same node/template?

    Please let me know if you have any thoughts. Thanks in advance.

    Cheers,

    Alex.

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 21, 2015 @ 08:33
    Jan Skovgaard
    0

    Hi Alex

    When there is only one node published is that one node then actually visible? Or has it been hidden for some reason? If so that's why it does not show.

    In regards to your second question...how are the nodes you want to list selected? Are they selected using multinode picker and picked on a certain node?

    I think the proper way to do it is to have a multinode picker setup on each node where you can specifically pick 5 different nodes for each page and then have a make just loop through the nodes selected on each node.

    Does this make sense?

    /Jan

  • Martin 114 posts 313 karma points
    Feb 21, 2015 @ 08:59
    Martin
    101

    Hi Alex,

    I think you ask for paging with 5 items per page. For example you can use @foreach (var item in items.Skip((page - 1) * pageSize).Take(pageSize)) to step forward. Below you find an example I use. In your case you should use pageSize = 5;. Good Luck!

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
            var pageSize =2;
            var page =1;int.TryParse(Request.QueryString["page"],out page);
            var items = CurrentPage.Children.OrderBy("nyhetsdatum descending");
            var totalPages =(int)Math.Ceiling((double)items.Count()/(double)pageSize);
    
            if(page > totalPages)
            {
                    page = totalPages;
            }
            else if(page < 1)
            {
                    page =1;
            }
    }
    <ul class="nyhetslista">
        @foreach (var item in items.Skip((page - 1) * pageSize).Take(pageSize))
        { 
            <ul>
            <li>@item.nyhetsdatum.ToString("d MMMM yyyy")</li>
            <li>@item.huvudrubrik</li>
            <li>@item.underrubrik</li>
            <li>@item.bodyText</li>
            <hr class="line_gradient" />
            <br>
            </ul>
        }
    </ul>
    
    @if (totalPages > 1)
        {
            <div class="pagination">
                <div>
                    @if (page > 1)
                    {
                        <span><a href="?page=@(page-1)">Previous</a></span>
                    }
                    @for (int p = 1; p < totalPages + 1; p++)
                    {
                        var active = (p == page) ? "active" : string.Empty;
                        <span class="@active">
                            <a href="?page=@p">@p</a>
                        </span>
                    }
                    @if (page < totalPages)
                    {
                        <span><a href="?page=@(page+1)">Next</a></span>
                    }
                </div>
            </div>
        }
    
  • Alex 30 posts 120 karma points
    Feb 21, 2015 @ 23:36
    Alex
    0

    Thank you for the piece of code Martin.

    Jan, yes, for some reason when it is only one child, even though it is published and I can access it my macros does not render it unfortunately. For my second question, they are children articles of my blog, I have like 80 of them, I don't use a node picker for this.

  • Alex 30 posts 120 karma points
    Feb 21, 2015 @ 23:45
    Alex
    0

    First problem is now sorted. Seems like the first child had to be republished, it did the trick. Funny enough it still displayed when more than one was present.. hmm.

Please Sign in or register to post replies

Write your reply to:

Draft