How do I change link-text in Wicket?
Asked Answered
D

6

11

I created a link with static text. That works fine but now I also want to change the text when you click the link.

I got as far as this:

add(new Link("doAnything") {
    @Override
    public void onClick() {
            // change the text!
            // this.modelChanging();
            // this.detach();
    }
});

I hope you can give me some easy advice - I'm really new to this :)

Best regards Elias

Dire answered 14/7, 2010 at 7:44 Comment(0)
R
20

The HTML:

<html><head></head><body>
<wicket:panel>

    <a href="" wicket:id="link">
        <wicket:container wicket:id="label" />
    </a>

</wicket:panel>
</body></html>

The java:

public class MyPanel extends Panel{

    private static class Mybean{

        String labelText = "click me";

        public String getLabelText(){
            return this.labelText;
        }

        public void setLabelText(final String labelText){
            this.labelText = labelText;
        }

    }

    public MyPanel(final String id){
        super(id);
        final Mybean bean = new Mybean();
        this.add(new Link<Void>("link"){

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(){
                bean.setLabelText("Thanks for clicking");
            }
        }.add(new Label("label", new PropertyModel<String>(bean, "labelText")))

        );

    }

}

I tend to use wicket:container in order to not pollute the HTML with superfluous elements (the wicket:container won't be rendered in production)

Rosalynrosalynd answered 14/7, 2010 at 8:3 Comment(1)
Good explanation with all the required info.Concavity
B
9

No change to the html necessary:

Link link = new Link("doAnything") {
    @Override
    public void onClick() {
            // change the text!
            // this.modelChanging();
            // this.detach();
    }
});
link.setBody(Model.of("visible link name"));

add(link);
Brock answered 9/12, 2015 at 16:58 Comment(0)
V
8

you need to back the text in the link with its own model:

<a wicket:id="doAnything"> <span wicket:id="linktext"/> </a>

and in java:

add(new Link("doAnything").add(new Label("linktext", Model.of("i 'm the text"));

better yet if you use a (Compound)PropertyModel and have a getLinktext() function the returns the text, depending on the state.

Veroniqueverras answered 14/7, 2010 at 7:52 Comment(0)
M
1

use the HTML tag as follow for your link and set the text in the property file for key in order to use it.

HTML:

<a wicket:id="doAnything"><wicket:message key="label.LABLENAME" /></a>

Property:

label.LABLENAME        =YOUR OWN TEXT FOR LABLE!

Java:

add(new BookmarkablePageLink<YOURCLASS>(
            "doAnything", YOURCLASS.class)));

Output would be a link label with the text specified in the property file. Simply change the corresponding text in properties to whatever text you want.

Monarch answered 20/9, 2016 at 12:37 Comment(1)
Thanks Smittey for making it better :)Monarch
E
0

Wicket can't change the text attribute of your link, so, add a new html component to do the hack.

I don't agree with this philosophy, and prefer to leave it to the man of the situation: javascript.

add(new AjaxLink("doAnything") {
    @Override
    public void onClick(final AjaxTarget t) {
            // change the text!
            t.appendJavaScript( "document.getElementById('idofyourlink').text = 'newtext';" );
            // this.modelChanging();
            // this.detach();
    }
});

This way you don't need the additional label/span inside your <a> element.

You can modify your text from java if you need to. If you don't, just do it from javascript then.

Erskine answered 27/8, 2014 at 0:2 Comment(0)
C
0

In our Wicket 6 project we use something like that:

public abstract class AjaxLabeledLink extends AjaxLink {

private IModel<String> text = new Model<>();

public AjaxLabeledLink(String string) {
    super(string);
    this.text = new Model<>("");
}

public AjaxLabeledLink(String string, String linkText) {
    super(string);
    this.text = new Model<>(linkText);
}

public AjaxLabeledLink(String string, IModel<String> linkText) {
    super(string);
    this.text = linkText;
}

@Override
public IModel<?> getBody() {
    return text;
}

public IModel<String> getText() {
    return text;
}

public void setText(IModel<String> text) {
    this.text = text;
}
}

This component satisfy our needs.

Chicane answered 4/3, 2016 at 3:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.