在我的一个项目中,我必须这样做。我建议使用CellClickHandler
而不是尝试点击锚点。
首先为您的点击操作创建更方便的处理程序:
public interface CellClickAction<P> {
void onCellClick(P proxy);
}
使用这个插件来保存网格中每个 ValueProvider 的映射:
public class GridCellClickPlugin<P> implements ComponentPlugin<Grid<P>> {
private final Map<ValueProvider<P, ?>, CellClickAction<P>> mapping;
public GridCellClickPlugin() {
this.mapping = new HashMap<ValueProvider<P, ?>, CellClickAction<P>>();
}
@Override
public void initPlugin(final Grid<P> component) {
component.addCellClickHandler(new CellClickHandler() {
@Override
public void onCellClick(CellClickEvent event) {
if (!mapping.isEmpty()) {
final ColumnConfig<P, ?> columnConfig = component.getColumnModel().getColumn(event.getCellIndex());
final CellClickAction<P> action = mapping.get(columnConfig.getValueProvider());
if (action != null) {
final P proxy = component.getStore().get(event.getRowIndex());
action.onCellClick(proxy);
}
}
}
});
}
}
注册此列的点击处理程序并初始化插件:
final GridCellClickPlugin<LinkData> plugin = new GridCellClickPlugin<LinkData>();
plugin.getMapping().put(lp.url(), new CellClickAction<LinkData>() {
@Override
public void onCellClick(LinkData proxy) {
//do the desired action here: redirect to some url, show pop-up window, etc
}
});
plugin.init(grid);
将您的文本呈现为链接:
linkConf = new ColumnConfig<LinkData, String>(lp.url(), 200, "URL");
linkConf.setCell(new AbstractCell<LinkData>() {
@Override
public void render(com.google.gwt.cell.client.Cell.Context context, LinkData value, SafeHtmlBuilder sb) {
final Anchor anchor = new Anchor(value.getSomeText());
sb.appendHtmlConstant(anchor.getElement().toString());
}
});