J'ai créé un lien avec du texte statique. Cela fonctionne bien, mais maintenant, je veux aussi modifier le texte lorsque vous cliquez sur le lien.
Je suis arrivé aussi loin que ceci: p> J'espère que vous pouvez me donner quelques Conseil facile - Je suis vraiment nouveau à ce sujet :) p> Cordialement Cordialement
Elias p> p>
6 Réponses :
Le HTML:
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"))) ); } }
Bonne explication avec toutes les informations requises.
Wicket ne peut pas changer l'attribut de texte de votre lien. Ajoutez donc un nouveau composant HTML pour faire le hack. P>
Je ne suis pas d'accord avec cette philosophie et préférez-le laisser à l'homme de la situation: JavaScript. P>
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(); } });
Aucune modification du HTML nécessaire:
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);
Dans notre projet de guichet 6, nous utilisons quelque chose comme ça:
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; } }
Utilisez la balise HTML comme suit pour votre lien et définissez le texte dans le fichier de propriétés pour la touche afin de l'utiliser.
html: p>
add(new BookmarkablePageLink<YOURCLASS>( "doAnything", YOURCLASS.class)));
Merci smittey pour le rendre meilleur :)