当用户向上或向下按下键时,我想在 GWT CellTable 中获取选定的行。在我的示例中,我有一个包含三列(ID、名称、日期)的表,并且我已经实现了处理点击事件的代码。单击一行时,我得到选定的对象。现在我想在使用向上和向下箭头键移动表格的行时做同样的事情。
这是我的代码:
// Create id column.
TextColumn<MyObject> idColumn = new TextColumn<MyObject>()
{
@Override
public String getValue(MyObject obj)
{
return String.valueOf(obj.getId());
}
};
// Create name column.
TextColumn<MyObject> nameColumn = new TextColumn<MyObject>()
{
@Override
public String getValue(MyObject obj)
{
return obj.getName();
}
};
// Create date column.
TextColumn<MyObject> dateColumn = new TextColumn<MyObject>()
{
@Override
public String getValue(MyObject obj)
{
return String.valueOf(obj.getDate());
}
};
// Create the CellTable element and add columns
CellTable<MyObject> table = new CellTable<MyObject>();
table.addColumn(idColumn, "ID");
table.addColumn(nameColumn, "Name");
table.addColumn(dateColumn, "Date");
// Create the selectioModel and the SelectionChangeEvent Handler
NoSelectionModel<MyObject> selectionModelMyObj = new NoSelectionModel<MyObject>();
Handler tableHandler = new SelectionChangeEvent.Handler()
{
@Override
public void onSelectionChange(SelectionChangeEvent event)
{
MyObject clickedObj = selectionModelMyObj.getLastSelectedObject();
Window.alert("Object selected: " + clickedObj);
}
};
// Add the handler to the selection model
selectionModelMyObj.addSelectionChangeHandler( tableHandler );
// Add the selection model to the table
table.setSelectionModel(selectionModelMyObj);
谢谢大家。