父小部件中“单击”的事件处理来自 CellTable 中的 AbstractCell
我是一个 GWT 菜鸟,我希望能在这个问题上朝着正确的方向推动。
使用 GWT 2.4,
我无法弄清楚如何侦听 EntryPoint 类中添加 CellTable 的事件。 CellTable 有一个自定义的 AbstractCell,它呈现带有超链接的字符串。 Cell 处理点击事件。
我的 EntryPoint 类如何处理该事件,以便它可以采取一些操作(例如隐藏 CellTable 并显示一些其他小部件)?
我能够成功处理 Cell 和 CellTable 中的事件,但无法弄清楚如何获取父窗口小部件(处理它的 EntryPoint 类)。
EntryPoint 类:
public class Tags_GWT implements EntryPoint {
private final TagGwtServiceAsync tagService = GWT.create(TagGwtService.class);
final TagCellTable tagCellTable = new TagCellTable();
public void onModuleLoad() {
RootPanel.get("tagCellTable").add(tagCellTable);
// load the table with data
tagService.getTagsAsList(new AsyncCallback<List<TagDto>>() {
public void onFailure(Throwable caught) {
GWT.log("Error loading data");
}
public void onSuccess(List<TagDto> tags) {
tagCellTable.setInput(tags);
}
});
CellTable 类:
public class TagCellTable extends CellTable<TagDto>{
code removed for simplicity
// Add a field updater to be notified when the user enters a new name.
nameColumn.setFieldUpdater(new FieldUpdater<TagDto, String>() {
@Override
public void update(int index, TagDto object, String value) {
// Inform the user of the change.
Window.alert("TagCellTable.update is executing for: '" + value + "'");
}
});
AbstractCell 类:
public class LinkCell extends AbstractCell<String>{
code omitted for simplicity
@Override
public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
if ("click".equals(event.getType())) {
// Ignore clicks that occur outside of the outermost element.
EventTarget eventTarget = event.getEventTarget();
if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
doAction(value, valueUpdater);
}
}
}
@Override
protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
doAction(value, valueUpdater);
}
private void doAction(String value, ValueUpdater<String> valueUpdater) {
Window.alert("LinkCell.doAction is executing for: ' " + value + "'");
if (valueUpdater != null) {
valueUpdater.update(value);
}
}
I'm a bit of a GWT noob and I'd appreciate a nudge in the right direction on this one.
Using GWT 2.4
I'm having trouble figuring out how to listen for an event in my EntryPoint class which adds a CellTable. The CellTable has a custom AbstractCell which renders a String with a hyperlink. The Cell handles a click event.
How can my EntryPoint class handle the event so that it can take some action (like hide the CellTable and show some other widgets)?
I'm able to successfully work with the event in the Cell and CellTable but can't figure out how to get the parent widget (the EntryPoint class to handle it).
The EntryPoint class:
public class Tags_GWT implements EntryPoint {
private final TagGwtServiceAsync tagService = GWT.create(TagGwtService.class);
final TagCellTable tagCellTable = new TagCellTable();
public void onModuleLoad() {
RootPanel.get("tagCellTable").add(tagCellTable);
// load the table with data
tagService.getTagsAsList(new AsyncCallback<List<TagDto>>() {
public void onFailure(Throwable caught) {
GWT.log("Error loading data");
}
public void onSuccess(List<TagDto> tags) {
tagCellTable.setInput(tags);
}
});
The CellTable class:
public class TagCellTable extends CellTable<TagDto>{
code removed for simplicity
// Add a field updater to be notified when the user enters a new name.
nameColumn.setFieldUpdater(new FieldUpdater<TagDto, String>() {
@Override
public void update(int index, TagDto object, String value) {
// Inform the user of the change.
Window.alert("TagCellTable.update is executing for: '" + value + "'");
}
});
The AbstractCell class:
public class LinkCell extends AbstractCell<String>{
code omitted for simplicity
@Override
public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
if ("click".equals(event.getType())) {
// Ignore clicks that occur outside of the outermost element.
EventTarget eventTarget = event.getEventTarget();
if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
doAction(value, valueUpdater);
}
}
}
@Override
protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event, ValueUpdater<String> valueUpdater) {
doAction(value, valueUpdater);
}
private void doAction(String value, ValueUpdater<String> valueUpdater) {
Window.alert("LinkCell.doAction is executing for: ' " + value + "'");
if (valueUpdater != null) {
valueUpdater.update(value);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须为
LinkCell
提供对EventBus
对象的引用。然后,在doAction
中,您可以在EventBus
上触发一些Event
。您的入口点模块可以在同一EventBus
上侦听相关事件,并从那里做出响应。没有一个好方法可以从已经内置的外部代码中挂钩到单元格,因为 CellTable 并不是真正由小部件组成的。普通的小部件事件层次结构永远不会激活,因此无需添加侦听器 - 您必须创建自己的 EventBus。
I think you'll have to give your
LinkCell
a reference to anEventBus
object. Then, indoAction
, you can fire someEvent
on theEventBus
. Your entrypoint module can be listening on the sameEventBus
for the relevant events, and respond from there.There's not a good way to hook into the cell from the outer code that's already built in because the CellTable is not really made out of widgets. The normal widget event hierarchy is never activated, so there's nothing to add a listener to - you have to create your own EventBus.