Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1287 posts 2744 karma points
    Jan 15, 2014 @ 16:30
    Amir Khan
    0

    Navigation including parent and siblings or just siblings depending on level

    Hi,

    I'm trying to create a global sidebar nav that works like this:

    Home
     - Parent (show siblings when here)
       - Child (show parent and siblings when here)
       - Child (show parent and siblings when here)
     -Parent

    For some reason, I can't get the script to only show the children of the parent you're on, here's what I'm working with. Any ideas?

     

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
    //Walk up the tree
    var topNavItem = Model.Content.Level > 2 ? Model.Content.Parent : Model.Content;

    //Check currentpage is same as topNaviItem Id
    var isSelected = topNavItem.Id == Model.Content.Id ? "active" : string.Empty;

    //Get Child Pages of Current Page
    var pages = topNavItem.Children.Where(x => x.IsVisible());
    }
    <nav class="leftColumnNav">
    <ul>
    @foreach (var page in pages)
    {
    var isPageSelected = page.IsAncestorOrSelf(Model.Content) ? "active" : string.Empty;

    <li class="@isPageSelected">
    <a href="@page.Url">
    @page.Name
    </a>
    @if (CurrentPage.Children.Where("umbracoNaviHide!= true").Count() > 1)
    {
    <ul>
    @foreach(var subitem in @page.Children.Where("umbracoNaviHide!= true"))
    {
    <li><a href="@subitem.Url">@subitem.Name</a></li>
    }
    </ul>
    }
    </li>
    }
    </ul>
    </nav>
  • Dan 1288 posts 3921 karma points c-trib
    Jan 15, 2014 @ 21:24
    Dan
    100

    Hi Amir,

    Currently it's showing all of the children nodes for all of the parent nodes, regardless of which page you're on, but you only want to show child nodes when you're in that section, right?

    So I think if you change this:

    @if (CurrentPage.Children.Where("umbracoNaviHide!= true").Count() > 1)
    

    ...to check whether you're in this particular section, it should work:

    @if (CurrentPage.Children.Where("umbracoNaviHide!= true").Count() > 1 && page.IsAncestorOrSelf(Model.Content))
    

    Hope I've understood correctly and this helps :)

  • Amir Khan 1287 posts 2744 karma points
    Jan 17, 2014 @ 20:58
    Amir Khan
    0

    Dan, this is fanastic, thank you!

  • ibrahim TUNC 55 posts 133 karma points
    Sep 15, 2014 @ 03:33
    ibrahim TUNC
    0

    how can add or change @subitem.Name I want to different field name.  For Exmp: @Model.PageName

    Thanks Best Regard.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 15, 2014 @ 06:54
    Dennis Aaen
    0

    Hi ibrahim,

    Try this:

    @subitem.PageName 

    /Dennis

  • ibrahim TUNC 55 posts 133 karma points
    Sep 15, 2014 @ 08:44
    ibrahim TUNC
    0

    Hi!. Denniz. 

    I Did. but gave me  following error.

     

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

    Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'PageName' and no extension method 'PageName' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    Source Error:

     
    Line 396:        
    Line 397:                        <li class="@isPageSelected">
    Line 398: <a href="@page.Url">@page.PageName</a> Line 399:
    Line 400:                            @if (CurrentPage.Children.Where("umbracoNaviHide!= true").Count() > 1 && page.IsAncestorOrSelf(Model.Content))


    Source File: c:\Users\ibrahim.tunc\Documents\My Web Sites\UIS\Views\ContentPage.cshtml    Line: 398 

  • ibrahim TUNC 55 posts 133 karma points
    Sep 15, 2014 @ 11:48
    ibrahim TUNC
    0
    input
                    <ul>
                       
    @foreach(varpageinModel.Content.Siblings())
                       
    {
                       
    <li>
                           
    <ahref="@page.Url">page.GetPropertyValue<string>("pageName")a>li>}ul>
    output 
Please Sign in or register to post replies

Write your reply to:

Draft