I have recently upgraded to Wicket 6.13 from Wicket 1.5.11 After the upgrade i am facing an issue with onclick behavior of a link.
We have a clickable row which contains few columns (one of which is a link to new page). now, if we click on the link then we are taken to new page and we click on the row (apart from the link) the row get selected (using Ajax call).
This was working fine with Wicket 1.5.11, i am facing issues with Wicket 6.13
Link class:
public class MyLink extends Link {
private static final long serialVersionUID = 5808539933400105591L;
private MyRow myRow;
public MyLink(String id, MyRow myRow) {
super(id);
this.myRow = myRow;
}
/** {@inheritDoc} */
@Override
public void onClick() {
//sets the response page where this needs to be redirected.
this.setResponsePage(new ResponseReadPage(this.myRow));
}
}
Populate method:
@Override
protected void populateItem(final ListItem item) {
final MyRow myRow = (MyRow) item.getModelObject();
item.add(new Label("naam", myRow.getName()));
item.add(new Label("id", myRow.getCode()));
MyLink myLink = new MyLink("myLink", myRow);
item.add(myLink);
final MyRow selectedRow = this.session.getSelectedRow();
if (selectedRow != null
&& selectedRow.equals(myRow)) {
this.session.selectedRow(myRow);
item.add(new AttributeModifier("class", "activeRow"));
this.selecteditem = item;
//some business logic
}
item.add(new AjaxEventBehavior("onclick") {
/** {@inheritDoc} */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onEvent(final AjaxRequestTarget target) {
final WebMarkupContainer container = (WebMarkupContainer) MyListView.this.getParent()
.getParent().get("myContainer");
MyListView.this.session.setSelectedRow(myRow);
if (MyListView.this.currentActiveItem != null) {
MyListView.this.previousActiveItem = MyListView.this.currentActiveItem;
MyListView.this.previousActiveItem.add(new AttributeModifier("class", ""));
}
item.add(new AttributeModifier("class", "activeRow"));
MyListView.this.currentActiveItem = item;
if (MyListView.this.previousActiveItem != null) {
target.add(MyListView.this.previousActiveItem);
}
if (MyListView.this.selecteditem != null
&& !MyView.this.selecteditem.equals(item)) {
MyListView.this.selecteditem.add(new AttributeModifier("class", ""));
target.add(MyListView.this.selecteditem);
}
target.add(item);
target.add(container);
}
});
}
When i try to click on the LINK instead of onClick method of the link, the onclick event of AjaxBehavior of row gets called. Can anyone point me in the correct direction to get this sorted?
UPDATE: When i right click on the link and open it in another tab the call to onClick method of link happens successfully as expected.
I found the solution for this. Added the following lines to the code:
myLink.add(new AttributeAppender(
"onclick", new Model("if(event.stopPropagation) { "+
"event.stopPropagation();"+
"} else { "+"event.cancelBubble = true;"
+"}"), ";"));
The link onclick event was being propagated to the onclick event for the row, hence it was behaving in such a manner.