我正在使用带有MultiSelectionModel的GWT DataGrid。
网格项目的选择应通过
来实现a)带CheckboxCell的CheckboxColumn
另外同时由
b)标准线模式选择模型(点击线的其余部分)。
使用CheckboxColumn,应启用用户多选不同的条目。但是当点击datagrid-lines上的其他位置时,应该执行单行选择策略,这意味着,如果在使用复选框之前完成了多项选择,则应该重置此选择,并且只应该单击所选行。之后被选中。
这就是我所拥有的。有谁知道如何同时启用CheckBox模式和行选择模式?
public class JobDataGrid extends DataGrid<Job>
{
private MultiSelectionModel<Job> selectionModel;
private Column<Job, Boolean> checkboxColumn;
private TextColumn<Job> idColumn;
private TextColumn<Job> titleColumn;
private TextColumn<Job> timestampColumn;
private TexTColumn<Job> ...
public JobDataGrid ()
{
super ();
checkboxColumn = new Column<Job, Boolean> (new CheckboxCell (true, false)) {
@Override
public Boolean getValue (Job job)
{
// Get the value from the selection model.
return selectionModel.isSelected (job);
}
};
checkboxColumn.setFieldUpdater (new FieldUpdater<Job, Boolean> () {
public void update (int index, Job job, Boolean value)
{
// Called when the user clicks on a checkbox.
selectionModel.setSelected (job, value);
}
});
// [...]
// [...]
// [...]
selectionModel = new MultiSelectionModel<Job> ();
setSelectionModel (selectionModel);
// setKeyboardSelectionPolicy (KeyboardSelectionPolicy.DISABLED);
// [...]
// [...]
// [...]
}
}
我已经尝试了所有4种变种
new CheckboxCell (false, false);
new CheckboxCell (true, false);
new CheckboxCell (false, true);
new CheckboxCell (true, true);
但他们都没有出现我需要的东西。而且我也玩过
setSelectionModel (selectionModel, DefaultSelectionEventManager.<Job> createCheckboxManager ());
也许
createCustomManager(DefaultSelectionEventManager.EventTranslator<T> translator)
会有帮助吗?
感谢名单 托马斯
答案 0 :(得分:3)
您可以创建自己的“复选框管理器”并按照自己的意愿行事。
table.setSelectionModel(selectModel, DefaultSelectionEventManager.<DocumentListItemDTO> createCustomManager(
new DefaultSelectionEventManager.CheckboxEventTranslator<DocumentListItemDTO>() {
@Override
public SelectAction translateSelectionEvent(CellPreviewEvent<DocumentListItemDTO> event) {
SelectAction action = super.translateSelectionEvent(event);
if (action.equals(SelectAction.IGNORE)) {
if (!event.getNativeEvent().getCtrlKey() && !event.getNativeEvent().getShiftKey())
selectionModel.clear();
return SelectAction.TOGGLE;
}
return action;
}
}));
答案 1 :(得分:2)
Lista's answer出现了正确的方向!
为了显示在网上几乎找不到示例的DefaultSelectionEventManager.CheckboxEventTranslator
的用法,这里有一个功能齐全的解决方案:
setSelectionModel (selectionModel, DefaultSelectionEventManager.<Job> createCustomManager (
new DefaultSelectionEventManager.CheckboxEventTranslator<Job> () {
@Override
public SelectAction translateSelectionEvent (CellPreviewEvent<Job> event)
{
NativeEvent nativeEvent = event.getNativeEvent ();
// Determine if we clicked on a checkbox.
Element target = nativeEvent.getEventTarget ().cast ();
if ("input".equals (target.getTagName ().toLowerCase (Locale.ROOT)))
{
final InputElement input = target.cast ();
if ("checkbox".equals (input.getType ().toLowerCase (Locale.ROOT)))
{
// Synchronize the checkbox with the current selection state.
input.setChecked (event.getDisplay ().getSelectionModel ().isSelected (
event.getValue ()));
return SelectAction.TOGGLE;
}
}
else
{
if (BrowserEvents.CLICK.equals (nativeEvent.getType ()))
{
selectionModel.clear ();
return SelectAction.SELECT;
}
}
return SelectAction.IGNORE;
}
}));