[英]GWT DataGrid row selection
I have a GWT DataGrid with a multi-selection model and check-boxes to show selection/select/deselect rows. 我有一个具有多选模型的GWT DataGrid和复选框以显示选择/选择/取消选择行。 That's all well and good.
一切都很好。
But, I also want to have a second, independent selection model. 但是,我也想拥有第二个独立的选择模型。 If a user double-clicks on a row, I want to handle that event, and for the event handler to know which row was double-clicked.
如果用户双击某行,则我想处理该事件,并使事件处理程序知道双击哪一行。 The double-clicking should not affect the check-box selection.
双击不应影响复选框的选择。
I tried this: 我尝试了这个:
final SelectionModel<MyRecord> selectionModel = new MultiSelectionModel...
//Yes I need a MultiSelectionModel
dataGrid.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(DoubleClickEvent event) {
selectionModel.get??? //no suitable getter for double-clicked
}
}, DoubleClickEvent.getType());
But ran into a dead-end when I found now way to get the double-clicked row in the event handler. 但是,当我发现现在可以在事件处理程序中获取双击的行时,遇到了死胡同。 One way would be to register both a Multi- and Single- selection model, but doubt DataGrid will support that.
一种方法是同时注册多选模型和单选模型,但怀疑DataGrid是否会支持该模型。
Neither can I work out how to get the clicked row from the DoubleClickEvent object. 我也无法解决如何从DoubleClickEvent对象获取被点击的行。
I have implemented a button cell with a FieldUpdater. 我已经用FieldUpdater实现了一个按钮单元。 This works, but it's not ideal.
这可行,但是并不理想。
Any suggestions? 有什么建议么?
If I understand correctly you want to get the index of the row. 如果我理解正确,则希望获取该行的索引。
You could do it like this: (this way you'll get the "Real" index) 您可以这样做:(这样,您将获得“真实”索引)
AbstractSelectionModel<T> selectionModel = (AbstractSelectionModel<T>)dataGrid.getSelectionModel();
ArrayList<Integer> intList = new ArrayList<Integer>();
List<Row> list = (List<Row>)_dataProvider.getList();
int i = 0;
for(Row row : list)
{
if( selectionModel.isSelected(row) )
intList.add(i);
i++;
}
UPDATE: 更新:
To get only the current row you could do that: 要仅获取当前行,可以执行以下操作:
datagrid.getKeyboardSelectedRow()
I'm 3 years too late to the party, but I think the more correct solution would be: 我参加聚会已经晚了3年,但我认为更正确的解决方案是:
dataGrid.addCellPreviewHandler(new CellPreviewEvent.Handler<YOUR_MODEL_TYPE>() {
@Override
public void onCellPreview(final CellPreviewEvent<YOUR_MODEL_TYPE> event) {
if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
int row = event.getIndex();
doStuff(row); // do whatever you need the row index for
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.