use*_*458 7 gwt mouseover event-handling multipleselection celltable
我有一个CellTable,它有4列:
| Column 1 | Column 2 | Column 3 | Column 4 |
Run Code Online (Sandbox Code Playgroud)
目标:
用户可以在按住鼠标按钮并将鼠标悬停在列上时选择多个列.
例如,用户单击第1列并按住鼠标按钮,在第2列和第3列上移动,从而选择第2列和第3列.
我试过了:
final MultiSelectionModel<data> selectionModel = new MultiSelectionModel<BestellungData>();
cellTable.setSelectionModel(selectionModel);
cellTable.addCellPreviewHandler(new Handler<data>()
{
@Override
public void onCellPreview(
CellPreviewEvent<data> event) {
// TODO Auto-generated method stub
if ("click".equals(event.getNativeEvent().getType())) {
selectionModel.setSelected(event.getValue(), true);
}
}
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
试试这个
private boolean isFocus, isFocusMouseDown;
private int lastStyledRow = -1;
private Set<Integer> columns = new HashSet<Integer>();
...
table.addCellPreviewHandler(new Handler<Contact>() {
@Override
public void onCellPreview(CellPreviewEvent<Contact> event) {
if ("focus".equals(event.getNativeEvent().getType())) {
isFocus = true;
if (lastStyledRow != -1) {
NodeList<TableCellElement> cells = table.getRowElement(lastStyledRow)
.getCells();
for (int col : columns) {
cells.getItem(col).removeClassName("selectedCell");
}
columns.clear();
}
}
if ("blur".equals(event.getNativeEvent().getType())) {
isFocus = false;
isFocusMouseDown = false;
lastStyledRow = event.getIndex();
NodeList<TableCellElement> cells = table.getRowElement(event.getIndex())
.getCells();
for (int col : columns) {
cells.getItem(col).addClassName("selectedCell");
}
}
if ("mousedown".equals(event.getNativeEvent().getType()) && isFocus) {
isFocusMouseDown = true;
}
if ("mouseover".equals(event.getNativeEvent().getType()) && isFocusMouseDown) {
columns.add(event.getColumn());
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是所选单元格的虚拟 CSS
.selectedCell{
border: 2px solid #F3F7FB;
background-color: #F00;
font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我们正在侦听几个事件,并且根据它们的顺序我们可以识别所选的列。
事件顺序:
focus任意列上的事件mousedown事件开始拖动花药列focus和两者都为 true,则收集事件mousedown的所有列mouseoverblur事件发生时,更改所有选定单元格的样式。focus在下一个事件中重置最后选定的单元格的样式快照

