Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    Nov 22, 2011 @ 09:54
    René Andersen
    0

    TopNavi Current / Active CSS class

    Hi, i have read a lot of forum threads but i can't figure out how to make the CSS Active to stay on the selected TopNavi item. I have put in (ul id="topnav") and i works fine but i dont know where to put (li class="active") i have used the standard Razor code from Umbraco, see below:

    HTML:

                  <ul id="topnav">               
                      <li class="active"><a href="index.html" >Home</a></li>
                      <li><a href="#">About Us</a>
                        <ul>
                          <li><a href="index.html">Homepage Default</a></li>
    </ul>

    Razor:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
      var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "ul id=topnav" : String.Format(" class=\"{0}\"", Parameter.UlClass);
      var parent = @Model.AncestorOrSelf(level);
      if (parent != null) {
        <ul@Html.Raw(ulClass)>
        @foreach (var item in parent.Children.Where("Visible")) {
          var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
          <li@Html.Raw(selected)>
            <a href="@item.Url">@item.Name</a>
          </li>
          }
        </ul>
      }
    }

    Thanks in advance. :-)

       

     

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 22, 2011 @ 10:22
    Michael Latouche
    0

    Hi René,

    I'm not sure what your problem exactly is, but id what you are trying to do is get the above HTML in razor, I think this script will work better:

    @inheritsumbraco.MacroEngines.DynamicNodeContext
    @{
      varlevel =String.IsNullOrEmpty(Parameter.Level)?1:int.Parse(Parameter.Level);
      varulClass =String.IsNullOrEmpty(Parameter.UlClass)?" id=topnav":String.Format(" class=\"{0}\"",Parameter.UlClass);
      varparent =@Model.AncestorOrSelf(level);
      if(parent !=null){
        <ul@Html.Raw(ulClass)>
        @foreach(varitem inparent.Children.Where("Visible")){
          varselected =Array.IndexOf(Model.Path.Split(','),item.Id.ToString())>=0?" class=\"active\"":"";
          <li@Html.Raw(selected)>
            <a href="@item.Url">@item.Name</a>
          </li>
          }
        </ul>
      }
    }

     

    Hope this helps.

    Cheers,

    Michael.

  • René Andersen 238 posts 684 karma points
    Nov 22, 2011 @ 10:59
    René Andersen
    0

    Hi Michael

    Thanks for the quick reply, i have tried to make the changes above, but the class (active) still jumps back to the first menu item and does not stay on the selected page / menu item. The only thing the class (active) does is to highlight the selectet page / item in the TopNavi menu, so when you click the "About Us" it should stay highlighted, but it jumps to the first item in the TopNavi. I hope this explains in more detail what my problem is.

    Cheers,

    René

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 22, 2011 @ 11:31
    Michael Latouche
    0

    Hi René,

    I think the problem comes from the fact that the herf for the "about us" page is #, which means that you actually do not change the page your are on.

    So if you are on the home page, the url will be /index.html, and if you then click on "about us", the url will become /index.html#, so you are actually still on index.html and it is therefore that page that gets highlighted.

    So the easiest solution, I think, would be to put an actual url on the "about us" page. Is this possible, or is there a specific reason why you have put it to "#"?

    Cheers,

    Michael.

  • René Andersen 238 posts 684 karma points
    Nov 22, 2011 @ 12:15
    René Andersen
    0

    Hi Michael

    Sorry about the HTML shows that "about us" is #, but that was just an example. I really want the menu to use Razor and be dynamic for learning reasons, because im new to Razor. The menu works dynamically but as earlier mentioned the only thing that does not work is the "active" CSS. I am testing local, so i cant show you the site im working on. But the local url's are at the moment. "http://localhost:26193/om-refica.aspx" + "http://localhost:26193/produkter.aspx" which are createt dynamically from the content in Umbraco.

    Cheers,

    René

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 22, 2011 @ 13:07
    Michael Latouche
    0

    Hi René,

    Strange indeed, because the razor looks OK. Can you maybe post the HTML that is generated on your local site? Maybe this can help finding the issue.

    Cheers,

    Michael.

  • René Andersen 238 posts 684 karma points
    Nov 22, 2011 @ 13:42
    René Andersen
    0

    Hi Michael

    Below you can see the HTML which is generated:

        <ul id=topnav>
          <li>
            <a href="/om-refica.aspx">Om refica</a>
          </li>
          <li>
            <a href="/produkter.aspx">Produkter</a>
          </li>
        </ul>

    And i should look somthing like this, if "om-refica.aspx" is selected in the TopNavi:

        <ul id=topnav>
          <li class="active">
            <a href="/om-refica.aspx">Om refica</a>
          </li>
          <li>
            <a href="/produkter.aspx">Produkter</a>
          </li>
        </ul>

    I am very grateful that you are spending so much time on my problem. :-)

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 22, 2011 @ 13:54
    Michael Latouche
    0

    Hi René,

    Very strang indeed.

    Now, I don't think it will help, but to make sure, ther are two more changes you can make to the razor script:

    for the parent : change @Model.AncestorOrSelf by Model.AncestorOrSelf

    for the li : remove the Html.Raw call

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      var level =String.IsNullOrEmpty(Parameter.Level)?1:int.Parse(Parameter.Level);
      var ulClass =String.IsNullOrEmpty(Parameter.UlClass)?" id=topnav":String.Format(" class=\"{0}\"",Parameter.UlClass);
      var parent = Model.AncestorOrSelf(level);
      if(parent !=null){
        <ul@Html.Raw(ulClass)>
        @foreach(var item inparent.Children.Where("Visible")){
          var selected =Array.IndexOf(Model.Path.Split(','),item.Id.ToString())>=0?" class=\"active\"":"";
          <li @selected>
            <a href="@item.Url">@item.Name</a>
          </li>
          }
        </ul>
      }
    }

     If this still does not work, maybe in the loop you can output the value of Model.Path and of item.Id. At least we can see if those values seem logical.

    Cheers,

    Michael.

     

  • René Andersen 238 posts 684 karma points
    Nov 22, 2011 @ 14:47
    René Andersen
    0

    Hi Michael

    Yeeaaahh, now it works. :-) It has now generated the HTML perfectly with "active" included:

       
    <ul id=topnav> <li> <a href="/om-refica.aspx">Om refica</a> </li> <li class="active"> <a href="/produkter.aspx">Produkter</a> </li> </ul>

    Thank you very much, i really appreciate your help.

    Cheers,

    René

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 22, 2011 @ 14:53
    Michael Latouche
    0

    Hi René,

    Great news!

    Can you maybe mark my last post as answer, so that others may find it more easily?

    Thanks and good day further!

    Cheers,

    Michael.

  • René Andersen 238 posts 684 karma points
    Nov 22, 2011 @ 15:04
    René Andersen
    0

    Hi Michael

    Your last post is hereby marked as answer. Again...Thank you very much!

    Cheers,

    René

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Nov 22, 2011 @ 15:05
    Michael Latouche
    0

    Hi René,

    You're very welcome :-)

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft