AjaxLink 的 Wicket AttributeModifier

发布于 2024-09-17 04:59:52 字数 1958 浏览 1 评论 0原文

在 wicket 中,AttributeModifier 不会更改 AjaxLink 的属性“class”。它应该更改类属性并更改链接的外观。

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")));
        }
    }
}

相应的 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>

谢谢

In wicket AttributeModifier doesn't change attribute "class" for 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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-09-24 04:59:58

正确答案已经给出,您必须将组件添加到 AjaxTarget。但是,为了创建更多“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);
    }
}

它使用模型,这是 Wicket 中推荐的。然而,那里存在大量的代码重复,所以我可能会建议使用一个不同的组件:

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);
        }
    }

当然,所有这些东西都没有经过测试或编译,所以不能保证。 :)

另外,Wicket 邮件列表中的人肯定会想出一些更优雅的东西......

享受吧。

The correct answer was already given, you have to add the components to the AjaxTarget. However, for the sake of creating more "Wickety" code, you could rewrite your class to something like this:

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. 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...

Enjoy.

若能看破又如何 2024-09-24 04:59:55

您实际上并没有告诉 wicket 在 ajax 响应中执行任何操作。

一个可能的解决方法是将行:(

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

或调用执行此操作的函数)添加到两个 onClick 方法中。

You're not actually telling wicket to do anything in the ajax response.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文