当选择一行时,我想知道该行的哪一列被选中。
目前,我会覆盖onBrowserEvent2
protected void onBrowserEvent2(Event event)
{
// Get the event target.
EventTarget eventTarget = event.getEventTarget();
if (!Element.is(eventTarget)){
return;
}
final Element target = event.getEventTarget().cast();
// Find the cell where the event occurred.
TableCellElement tableCell = findNearestParentCell(target);
if (tableCell == null) {
return;
}
int col = tableCell.getCellIndex();
.... blah ... blah ....
}
其中,
//exact replica code from {@link AbstractCellTable} private method findNearestParentCell
private TableCellElement findNearestParentCell(Element elem) {
while ((elem != null) && (elem != getElement())) {
// TODO: We need is() implementations in all Element subclasses.
// This would allow us to use TableCellElement.is() -- much cleaner.
String tagName = elem.getTagName();
if ("td".equalsIgnoreCase(tagName) || "th".equalsIgnoreCase(tagName)) {
return elem.cast();
}
elem = elem.getParentElement();
}
return null;
}
这是唯一的方法吗?是否有一个带有更改事件的选择模型,它会告诉我该行的哪一列被点击。
有没有办法在列或列的单元格上实现点击处理程序?
答案 0 :(得分:4)
对列使用ClickableTextCell的扩展名。
public class SelectableTextCell
extends ClickableTextCell
{
@Override
public void onBrowserEvent(
Context context,
Element parent,
String value,
NativeEvent event,
ValueUpdater<String> valueUpdater)
{
GWT.log("event="+event.getType()); //$NON-NLS-1$
doYourThing();
if (IWishToPropagateTheClick)
super.onBrowserEvent(context, parent, value, event, valueUpdater);
}
}
或者对于更一般的情况,扩展AbstractCell,并在构造函数中指定消耗的事件。