cloudscribe / cloudscribe.Web.Navigation

ASP.NET Core MVC ViewComponent for navigation menus and breadcrumbs
Apache License 2.0
69 stars 30 forks source link

Keep Parent Node Active #26

Closed ghost closed 7 years ago

ghost commented 7 years ago

I have a navigation node like this:

<NavNode key="Search" area="Search" parentKey="RootNode" controller="Search" action="Index" text="Employee Search" title="Employee Search">
      <Children>
        <NavNode key="SearchResult" parentKey="Search" area="Search" controller="Search" action="Result" text="Search Result">
          <Children />
        </NavNode>
      </Children>
    </NavNode>

when Employee Search is selected, it takes the user a search view were they can enter a search criteria, on submit the user is redirected to the Search Result view.

Is there any way to keep the Search node .active while in the child node?

Thanks.

joeaudette commented 7 years ago

I use breadcrumbs as an indicator of the active path. If you want to assign a css class or change the markup you can customize the views by copying them into your own project

simon25608 commented 7 years ago

As @joeaudette says, you could change the view with something like that:

if (!Model.HasVisibleChildren(childNode)) { string active = null; if(!Model.ShouldAllowView(Model.CurrentNode)) { if (childNode.Value.Key == Model.CurrentNode.Value.ParentKey) { active = " active "; } }
}

ghost commented 7 years ago

thanks guys, got it working. I did this:

 @if (Model.HasVisibleChildren(Model.RootNode)) {
    string active = null;
    <ul class="nav navbar-nav" role="menubar">
        <li class='nav-item @Model.GetClass(Model.RootNode.Value)' role="menuitem"><a class="nav-link custom-nav-link" href="@Url.Content(Model.AdjustUrl(Model.RootNode))">@Html.Raw(Model.GetIcon(Model.RootNode.Value))@Model.AdjustText(Model.RootNode)</a></li>
        @foreach (var node in Model.RootNode.Children) {
            if (!Model.ShouldAllowView(node)) { continue; }
            if (!Model.HasVisibleChildren(node)) {
                <li class='nav-item @Model.GetClass(node.Value)' role="menuitem"><a class="nav-link custom-nav-link" href="@Url.Content(Model.AdjustUrl(node))">@Html.Raw(Model.GetIcon(node.Value))@Model.AdjustText(node)</a></li>
            } else {
                foreach (var child in node.Children) {
                    if (child.Value.EqualsNode(Model.CurrentNode.Value)) {
                        active = "active";
                    }
                }
                <li class='nav-item @Model.GetClass(node.Value, active)' role="menuitem"><a class="nav-link custom-nav-link" href="@Url.Content(Model.AdjustUrl(node))">@Html.Raw(Model.GetIcon(node.Value))@Model.AdjustText(node)</a></li>
            }
        }
    </ul>
}