简体   繁体   English

小门可见性和班级切换

[英]Wicket visiblity and class toggle

I'm learning Wicket and now I'm struggling with how to do a sidebar menu. 我正在学习Wicket,现在正在为如何制作侧边栏菜单而苦苦挣扎。

<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). 有些链接对于某些用户是不可见的(根据用户的角色),当我在链接所指向的页面上时,我希望<li> active类(例如示例中的linkB )。 What's the Wicket way of doing this? Wicket这样做的方式是什么?

To add 'class="active"' you can simply add this to your java code: 要添加“ class =“ active”',您可以简单地将其添加到您的java代码中:

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). 其中item是一个WebMarkupContainer,它通过小门ID连接到html li-tag之一(您也可以简单地调用link.setVisible(false),但是列表项目符号点仍将被呈现)。

Wicket is component oriented framework, so the "Wicket-way" is by overwriting components methods. Wicket是面向组件的框架,因此“ Wicket-way”是通过覆盖组件方法来实现的。 The simplest solution in your case would be to have the li -s as Wicket components. 在这种情况下,最简单的解决方案是将li -s作为Wicket组件。



    // your custom method to load the user
    final IModel<User> user = getUserModel();
    final private Class<? extends WebPage> pageLinkA;

    WebMarkupContainer li = new WebMarkupContainer("linkContainerA") {

        @Override
        protected void onConfigure() {
            super.onConfigure();
            boolean visible = user.getObject().hasRoleXY();
            setVisible(visible);
        }

        @Override
        protected void onComponentTag(ComponentTag t) {
            super.onComponentTag(t);
            if(getPage().getClass() == pageLinkA) {
               t.put("class", "active");
            }

        }
    }

   li.add(new BookmarkablePageLink("linkA", pageLinkA));

To avoid code repetition the most suitable way would be to create custom component (extending Panel or GenericPanel) or at least to extend WebMarkupContainer. 为了避免代码重复,最合适的方法是创建自定义组件(扩展Panel或GenericPanel)或至少扩展WebMarkupContainer。

Edit: As Tom pointed out, you can also use AttributeAppender and add it directly in onConfigure() (based on getPage().getClass() == pageLinkA ). 编辑:正如Tom所指出的,您还可以使用AttributeAppender并将其直接添加到onConfigure() (基于getPage().getClass() == pageLinkA )。 In this way overwriting of onComponentTag would not be necessary. 这样,就不必覆盖onComponentTag

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM