简体   繁体   English

用于AjaxLink的Wicket AttributeModifier

[英]Wicket AttributeModifier for AjaxLink

In wicket AttributeModifier doesn't change attribute "class" for AjaxLink. 在小门中,AttributeModifier不会更改AjaxLink的属性“类”。 It should change class attribute and change how link looks like. 它应该更改类属性并更改链接的外观。

public class TestPage extends WebPage {
    private AjaxLink link1;
    private AjaxLink link2;

    public TestPage() {
        super();

        link1 = new AjaxLink("link1") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                switchView("view1");
            }
        };

        link2 = new AjaxLink("link2") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                switchView("view2");
            }
        };

        link1.setOutputMarkupId(true);
        link2.setOutputMarkupId(true);

        link1.add(new AttributeModifier("class", true, new Model<String>("active")));
        link2.add(new AttributeModifier("class", true, new Model<String>("inactive")));

        add(link1);
        add(link2);
    }

    private void switchView(String viewName) {
        if (viewName.equals("view1")) {
            link1.add(new AttributeModifier("class", true, new Model<String>("active")));
            link2.add(new AttributeModifier("class", true, new Model<String>("inactive")));
        } else if (viewName.equals("view2")) {
            link1.add(new AttributeModifier("class", true, new Model<String>("inactive")));
            link2.add(new AttributeModifier("class", true, new Model<String>("active")));
        }
    }
}

Corresponding html file looks like: 相应的html文件如下所示:

<html xmlns:wicket>
<body>
<wicket:extend>
 <div id="tabs">
 <ul>
  <li><a wicket:id="link1">View1</a></li>
  <li><a wicket:id="link2">View2</a></li>
 </ul>
 </div>
</wicket:extend>
</body>
</html>

Thanks 谢谢

You're not actually telling wicket to do anything in the ajax response. 您实际上并不是在告诉wicket在ajax响应中做任何事情。

A likely fix is to add the lines: 可能的解决方法是添加以下行:

target.addComponent(link1);
target.addComponent(link2);

(or a call to a function doing this) to both of your onClick methods. (或对此功能的调用)两个onClick方法。

The correct answer was already given, you have to add the components to the AjaxTarget. 已经给出了正确的答案,您必须将组件添加到AjaxTarget。 However, for the sake of creating more "Wickety" code, you could rewrite your class to something like this: 但是,为了创建更多的“ Wickety”代码,您可以将类重写为如下形式:

public class TestPage extends WebPage {
    private AjaxLink link1;
    private AjaxLink link2;

    public TestPage() {
        super();

        final Model<Boolean> link1Model = new Model<Boolean>(Boolean.True);
        final Model<Boolean> link2Model = new Model<Boolean>(Boolean.False);

        link1 = new AjaxLink<Boolean>("link1", link1Model) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                this.getModel().setObject(!this.getModel().getObject());
                target.addComponent(this);
            }
        };

        link2 = new AjaxLink<Boolean>("link2", link2Model) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                this.getModel().setObject(!this.getModel().getObject());
                target.addComponent(this);
            }
        };

        link1.setOutputMarkupId(true);
        link2.setOutputMarkupId(true);

        link1.add(new AttributeModifier("class", true, new Model<String>() {
           public String getObject() {
              return link1Model.getObject() ? "active" : "inactive";
           }
        }));
        link2.add(new AttributeModifier("class", true, new Model<String>() {
           public String getObject() {
              return link1Model.getObject() ? "active" : "inactive";
           }
        }));

        add(link1);
        add(link2);
    }
}

It makes use of models and that is recommended in Wicket. 它利用了模型,这在Wicket中是推荐的。 However, there is heavy code duplication going on there, so I would maybe suggest going for a distinct component: 但是,这里有大量的代码重复,所以我建议使用一个不同的组件:

public final class ActiveInactiveLink extends AjaxFallbackLink<Boolean> {
    public ActiveInactiveLink(String id) {
        super(id, new Model<Boolean>(Boolean.True));
        this.add(new AttributeModifier("class", true, new Model<String>() {
            public String getObject() {
               Model<Boolean> model = ActiveInactiveLink.this.getModel();
               return model.getObject() ? "active" : "inactive";
            }
         }));
         this.setOutputMarkupId(true);
    }

    @Override
    public void onClick(AjaxRequestTarget target) {
       this.getModel().setObject(!this.getModel().getObject());
       target.addComponent(this);
    }

    @Override
    public void setModel(IModel<Boolean> model) {
        if(model == null)
           return;
        this.model = model;
    }

 }

 public class TestPage extends WebPage {
        private AjaxLink link1;
        private AjaxLink link2;

        public TestPage() {
            link1 = new ActiveInactiveLink("link1");
            link2 = new AjaxLink<Boolean>("link2");

            add(link1);
            add(link2);
        }
    }

All this stuff is not tested or compiled, of course, so no guarantees. 当然,所有这些东西都未经测试或编译,因此不能保证。 :) :)

Also, the guys from the Wicket mailing list will most definitely come up with something more elegant... 而且,Wicket邮件列表中的人肯定会提出更优雅的东西...

Enjoy. 请享用。

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

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