Copied to clipboard

Flag this post as spam?

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


  • RunnicFusion 62 posts 145 karma points
    Sep 19, 2012 @ 15:26
    RunnicFusion
    0

    Dropdown menu with special style of last item

    Hello,

    I'm a little bit stuck on coding the menu structure of my website.
    The menu has to do the following:

    • when there are 4 items (te maximum of the menu) it has to create a different class in the li tag.
    • The menu also have a dropdown (menu is horizontal and has on the root a dropdown, 1 level deep

     

    I allready have to following code: (1047 is the root of the document tree). how to make it work?

    <ul id="nav">
    @if (@Model.NodeById(1047).Children.Where("umbracoNaviHide!= true").Count() == 4)
    {
     foreach (var item in @Model.NodeById(@Parameter.NodeID).Children.Where("umbracoNaviHide!= true"))
        {
     <text>
     <li>
      <a href="#" title="minder">
        @Model.NodeById(1049).Name
      </a>
      </li>
     </text>
     }
    }
    @if (@Model.NodeById(@Parameter.NodeID).Children.Where("umbracoNaviHide!= true").Count() < 4)
    {
     foreach (var item in @Model.NodeById(@Parameter.NodeID).Children.Where("umbracoNaviHide!= true"))
        {
     <text>
     <li>
      <a href="#" title="meer">
        @Model.NodeById(@Parameter.NodeID).Name
      </a>
     </li>
     </text>
    }
    }
    </ul>

     

    Thanks for your help!

     

  • RunnicFusion 62 posts 145 karma points
    Sep 19, 2012 @ 16:08
    RunnicFusion
    0

    Have now the following code, seems to be working but i don't get my class on the last item:

     

    <ul id="nav">
      @if (@Model.NodeById(1049).Children.Where("umbracoNaviHide!= true").Count() < 3)
      {
        foreach (var item in @Model.NodeById(1047).Children.Where("umbracoNaviHide!= true"))
          {
             <text>
                <li>
                  <a href="@item.Url" title="@item.Name">
                    @item.Name
                  </a>
               </li>
             </text>
          }
      }

    @if (@Model.NodeById(1049).Children.Where("umbracoNaviHide!= true").Count() == 3)
      {
         foreach (var item in @Model.NodeById(1047).Children.Where("umbracoNaviHide!= true"))
           {
             <text>
               <li>
                 <a href="@item.Url" title="@item.Name">
                   @item.Name Lst
                 </a>
               </li>
             </text>
           }
      }
    </ul>
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 19, 2012 @ 16:46
    Dan Diplo
    0

    You can use the IsLast() method on a DynamicNode to determine if it is the last item in a collection:

    eg.

    foreach (var item in @Model.NodeById(1047).Children.Where("Visible"))
    {
            <text>
            <li class="@item.IsLast("last")">
              <a href="@item.Url" title="@item.Name">
                @item.Name
              </a>
           </li>
           </text>
    }
    
    The above will add the class "last" to the last item. Also note you can use Where("Visible") instead of Where("umbracoNaviHide!= true").

    See: http://umbraco.com/follow-us/blog-archive/2011/9/18/umbraco-razor-feature-walkthrough%E2%80%93part-7.aspx

     

  • RunnicFusion 62 posts 145 karma points
    Sep 20, 2012 @ 15:52
    RunnicFusion
    0

    Thanks, te following code is working for me, but how to do the dropdown items? (allready included, no errors but not working)

    <ul id="nav"> 

    @foreach (var item in @Model.NodeById(1047).Children.Where("umbracoNaviHide!= true"))
    {
            <li class="@item.IsLast("laatsteitem")">
              <a href="@item.Url" title="@item.Name">
                @item.Name
              </a>

             @if (@Model.NodeById(1047).Children.Where("umbracoNaviHide!= true").Count() > 1)
              {
               <text>
                <ul>
                foreach(var item in Model.Children)
                  {
                      <li><a href="@item.Url">@item.Name</a></li>
                  }
                </ul>
               </text>
               }
     
           </li>
    }
    </ul>

    When i change

    foreach(var item in Model.Children)

    to

    @foreach(var item in Model.Children)

    I get a error:

    error CS0136: A local variable named 'item' cannot be declared in this scope because it would give a different meaning to 'item', which is already used in a 'parent or current' scope to denote something else

    How to solve this problem?

     

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 20, 2012 @ 16:50
    Dan Diplo
    100

    Use a different variable name than "item" in your second loop. ie.

    foreach(var subitem in Model.Children)
    {
        <li><ahref="@subitem.Url">@subitem.Name</a></li>
    }
  • RunnicFusion 62 posts 145 karma points
    Sep 20, 2012 @ 17:12
    RunnicFusion
    0

    Thank i have the folowwing code, but something is wrong with the } (missing??)

     

    <ul id="nav">
      @foreach (var item in @Model.NodeById(1047).Children.Where("umbracoNaviHide!= true"))
      {
        <li class="@item.IsLast("laatsteitem")">
          <a href="@item.Url" title="@item.Name">
            @item.Name
          </a>
        @if (@Model.NodeById(1047).Children.Where("umbracoNaviHide!= true").Count() > 1)
        {
          <ul>
            @foreach(var subitem in Model.Children)
            {
               <li><a href="@subitem.Url">@subitem.Name</a></li>
            }
          </ul>
        }
        </li>
      }
    </ul>
Please Sign in or register to post replies

Write your reply to:

Draft