BourneAgain
BourneAgain

Reputation: 71

Replace text inside Wicket AjaxLink

I have created a new AjaxLink in my .java file

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

@Override                                                                                                                                              
public void onClick(AjaxRequestTarget target) {                                                                                                        
target.appendJavascript("window.open('http://www.cnn.com/2011/WORLD/africa/02/23/libya.protests/index.html?hpt="+T1+"')");              
}                                                                                                                                                      
});

And added it to my .html file

<a href="#" wicket:id="link">TEXT TO REPLACE</a> 

The url is just an example but T1 is dynamic and I get that from my .java file. I would like the "TEXT TO REPLACE" to equal the T1 string but I don't know how to do this. I have tried creating a Label and adding it like

<a href="#" wicket:id="link"><span wicket:id="linkLbl"></span></a> 

but that gives an error.

Any suggestions?

Thanks

Upvotes: 2

Views: 9362

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299168

a) Use a common Model for both Link and Label, b) don't forget to update the link

IModel<String> model = // get model data from somewhere

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

@Override                                                                                                                                              
public void onClick(AjaxRequestTarget target) {
target.addComponent(this); // update the link component
target.appendJavascript("window.open('http://www.cnn.com/2011/WORLD/africa/02/23"
  + "/libya.protests/index.html?hpt="+model.getObject()
     /* you probably want to encode this first */+"')");              
}                                                                                                                                                      
}).setOutputMarkupId().add(new Label("label",model));

Upvotes: 0

biziclop
biziclop

Reputation: 49804

The Label is the right direction, but you have to make sure that you add the label in the java code as well, it should be a child component of your ajax link.

(On a sidenote: you might want to consider using <wicket:container> instead of <span>. In this case it doesn't matter much, but there are cases where an extra <span> tag would make your HTML invalid.)

Upvotes: 1

Related Questions