Reputation: 80340
I'm trying to create a link (anchor) in GWT that can be clicked and it's ClickEvent
can be processed, while at the same time this anchor wouldn't reload the page. This basically means that a href
must not be set.
In javascript this is done like this:
<a href="javascript:handleAnchorClick();">Link</a>
or with
<a href="#" onclick="return handleAnchorClick()">Link</a>
where handleAnchorClick()
returns false.
What would be the best way to achieve this in GWT?
Upvotes: 9
Views: 21145
Reputation: 1572
Don't forget to add a style to anchor otherwise it doesnt behave like a normal html link:
.anchor {
text-decoration: underline;
font-weight: bold;
cursor: pointer;
display: block;
}
Upvotes: 8
Reputation: 113
As you describe, using href="#" will reload the page.
This is what you can do in UIBinder:
<g:Anchor ui:field="myScriptedAnchor" href="javascript:;">
MyScriptedAnchorText
</g:Anchor>
Then you can do whatever you want handling the ClickEvent in the view implementation.
@UiHandler("myScriptedAnchor")
void onMyScriptedAnchorClick(ClickEvent event) {
// TODO whatever you want to do...
}
Upvotes: 8
Reputation:
Listeners are deprecated in GWT use handlers
Something like that :
Anchor a = new Anchor("hi");
a.addClickhandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("hi");
}
});
Upvotes: 10
Reputation: 14746
Use tha Anchor element, and call its addClickListener() method and add in what ever logic you wish. This sort of anchor doesn't reload the page.
Upvotes: 12