Search code examples
javawicketmarkup

Wicket visiblity and class toggle


I'm learning Wicket and now I'm struggling with how to do a sidebar menu.

<ul class="nav nav-tabs nav-stacked">
    <li><a wicket:id="linkA">A</a></li>
    <li class="active"><a wicket:id="linkB">B</a></li>
    <li><a wicket:id="linkC">C</a></li>
    <li><a wicket:id="linkD">D</a></li>
    <li><a wicket:id="linkE">E</a></li>
</ul>

Some links won't be visible to some users (according to the role of the user) and when I'm on the page where link goes to, I want <li> to have class active (like linkB has in the example). What's the Wicket way of doing this?


Solution

  • To add 'class="active"' you can simply add this to your java code:

    if(...condition...){
      link.add(new AttributeAppender("class", "active");
    }
    

    To toggle visibility you can simply do this:

    if(...condition...){
      item.setVisible(false);
    }
    

    where item is a WebMarkupContainer that is connected to one of the html li-tags via a wicket ID (you can also simply call link.setVisible(false), but the list bullet point would still be rendered then).