2
當選中一行時,我想知道該行的哪一列被選中。GWT CellTable/DataGrid - 如何檢測列選擇?
目前,我會覆蓋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;
}
這是唯一的辦法?是否有一個帶有更改事件的選擇模型,它會告訴我該行的哪一列被點擊。
有沒有辦法在列或列的單元格上實現點擊處理程序?
也許值得注意的是,在上面的例子中,你可以引用context.getColumn(),它將返回你的單元格的列索引。 – 2011-12-20 00:50:07