Reputation: 11201
I am trying to add a hyperlink in celltable and on clicking on that link i want to call a method.
with the below code i am getting a hyperlink in my celltable correctly but I am not able to call a method by clicking on the link , when i click the link it takes me to the previous page.
Any Solution
Hyperlink link = new Hyperlink("Delete","");
Column<EmployerJobs, Hyperlink> linkColumn =
new Column<EmployerJobs, Hyperlink>(new HyperLinkCell()) {
@Override
public Hyperlink getValue(EmployerJobs list) {
link.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
deleteJobs(list);
}
});
return link;
}
});
Upvotes: 2
Views: 7202
Reputation: 2604
As suggested in above answer you can use updater when you want inplace editing. But if you want to capture a click to perform some action, you can do it using ClickableTextCell.
ClickableTextCell employerJobsCell = new ClickableTextCell();
Column<EmployerJobs, String> employerJobsColumn = new Column<EmployerJobs, String>(employerJobsCell) {
@Override
public String getValue(EmployerJobs object) {
return object.getWhichStringToDisplay();
}
@Override
public void render(Context context, EmployerJobs object, SafeHtmlBuilder sb) {
//this method is optional, can be used if the display needs to be customized
}
@Override
public void onBrowserEvent(Context context, Element elem, EmployerJobs object, NativeEvent event) {
super.onBrowserEvent(context, elem, object, event);
Event evt = Event.as(event);
int eventType = evt.getTypeInt();
if (eventType == Event.ONCLICK) {
//call delete job when cell is clicked
deleteJobs(object);
}
}
};
dataGrid.addColumn(employerJobsColumn, "The header goes here");
Upvotes: 1
Reputation: 121
I have post my answer on the similar thread: How can I render a ClickableTextCell as an anchor in a GWT CellTable? but for those who bookmarked this thread: This is what you need
public class ClickableSafeHtmlCell extends AbstractCell<SafeHtml> {
/**
* Construct a new ClickableSafeHtmlCell.
*/
public ClickableSafeHtmlCell() {
super("click", "keydown");
}
@Override
public void onBrowserEvent(Context context, Element parent, SafeHtml value, NativeEvent event,
ValueUpdater<SafeHtml> valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
if ("click".equals(event.getType())) {
onEnterKeyDown(context, parent, value, event, valueUpdater);
}
}
@Override
protected void onEnterKeyDown(Context context, Element parent, SafeHtml value,
NativeEvent event, ValueUpdater<SafeHtml> valueUpdater) {
if (valueUpdater != null) {
valueUpdater.update(value);
}
}
@Override
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
if (value != null) {
sb.append(value);
}
}
And then usage:
Column<YourProxy, SafeHtml> nameColumn = new Column<YourProxy, SafeHtml>(
new ClickableSafeHtmlCell()) {
@Override
public SafeHtml getValue(YourProxy object) {
SafeHtmlBuilder sb = new SafeHtmlBuilder();
sb.appendHtmlConstant("<a>");
sb.appendEscaped(object.getName());
sb.appendHtmlConstant("</a>");
return sb.toSafeHtml();
}
};
nameColumn.setFieldUpdater(new FieldUpdater<YourProxy, SafeHtml>() {
@Override
public void update(int index, YourProxy object, SafeHtml value) {
Window.alert("You have clicked: " + object.getName());
}
});
Upvotes: 3
Reputation: 17489
Instead of a HyperlinkCell you can either use a ClickableTextCell, a ButtonCell or an ActionCell.
ClickableTextCell:
Column<EmployerJobs, String> linkColumn =
new Column<EmployerJobs, String>(new ClickableTextCell()) {
@Override
public String getValue(EmployerJobs object) {
return TEXT_TO_DISPLAY;
}
},'linkheadertext');
linkColumn.setFieldUpdater(new FieldUpdater<EmployerJobs, String>() {
@Override
public void update(int index, EmployerJobs object, String value) {
deleteJobs(object);
}
});
ButtonCell:
Column<EmployerJobs, String> buttonColumn =
new Column<EmployerJobs, String>(new ButtonCell()) {
@Override
public String getValue(EmployerJobs object) {
return TEXT_TO_DISPLAY;
}
},'linkheadertext');
buttonColumn.setFieldUpdater(new FieldUpdater<EmployerJobs, String>() {
@Override
public void update(int index, EmployerJobs object, String value) {
deleteJobs(object);
}
});
ActionCell:
Column<EmployerJobs, EmployerJobs> actionColumn =
new Column<EmployerJobs, EmployerJobs>(new ActionCell<EmployerJobs>("Click Me",
new ActionCell.Delegate<EmployerJobs>() {
@Override
public void execute(EmployerJobs jobs) {
deleteJobs(jobs);
}
})
{
@Override
public EmployerJobs getValue(EmployerJobs object) {
return object;
}
},'linkheadertext');
Check out the CellSample showcase for more infos.
Upvotes: 8