Copied to clipboard

Flag this post as spam?

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


  • Chris 92 posts 238 karma points
    Jul 31, 2015 @ 08:00
    Chris
    0

    Get every parent unless DokumentType != ProductGroup

    Hello,

    I'm really new to razor only changed some of the existing umbraco .cshtml files so far and encountered a problem when trying to build my own templates now.

    I want to create a template for a "ProductGroup", each ProductGroup can have children of Type "ProductGroup" or "Product". In a live example it would maybe look like this:

    expandable Items (ProductGroup) => Ink (ProductGroup) => someInkCartridges (Product) => Paper (ProductGroup) => somePaperTypes (Product)

    I hope it's unterstandable.

    What I know want is that when I'm on the Page for a Product, in the top example lets take the somePaperTypes-Page, I want a navigation to be displayed with the different Parent.Names and give the customer the option to klick on them as a anker-tag. So for our example it should look like this: expandable Items => Paper => SomePaperTypes Als three should be anker tags of course.

    Here my code I've got so far, but it only works for .Children at the moment:

    @{
        var groups = Model.Children;
    
        <ul>
        @foreach (var g in groups)
        {
            <li>
                <a href="@g.Url">@g.Name =></a>
            </li>
        }
            <li>
                <a href="@Model.Url">@Model.Name</a>
            </li>
        </ul>
    
    }
    

    How can I change the code know to achieve this with the parents until the parents DocumentType is not of type "ProductGroup" ||/or "Product"?

    Best Regards,

    Chris

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Jul 31, 2015 @ 09:31
    Steve Morgan
    100

    I think I'm following what you need..

    Try this (adjusting the .Level > 1 to be relevant to your node structure):

    var parentNodes = Model.Content.Ancestors().OrderBy("Level");
    
    foreach (var curAncestor in parentNodes)
    {
        if (curAncestor.Level > 1)
        {
            <h2>@curAncestor.Name</h2>
        }
    }
    
  • Chris 92 posts 238 karma points
    Jul 31, 2015 @ 09:32
    Chris
    0

    @Steve: .OrderBy("Level") Thats what I was looking for, it now works as intendet! Thanks.

    edit: final code:

    @{
        var productgroups = @Model.AncestorsOrSelf("ProductGroup").OrderBy("Level");
    
        <ul class="productgrouplist">
        @foreach (var pg in productgroups)
        {
                <li>
                    <a href="@pg.Url">@pg.Name =></a>
                </li>
        }
            <li>
                <a href="@Model.Url">@Model.Name</a>
            </li>
        </ul>
    }
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Jul 31, 2015 @ 09:40
    Steve Morgan
    1

    What had me scratching my head was I was sure there was a method to get all Ancestors up to a certain level rather than from a level. There is the Up() traversing method which you can say .. Model.Content.Up(3) and go up 3 levels but this is no good for a traversa as it only returns the node at that particular levell!

    I guess this might be a more succinct way of doing what you want too - also shows how to filter out document types.

    var parentNodes = Model.Content.Ancestors().Where(x => x.DocumentTypeAlias != "ProductGroup" && x.Level > 1).OrderBy("Level");
    

    Glad you've sorted it.

Please Sign in or register to post replies

Write your reply to:

Draft