I've got a CellTable, which has 4 columns:
| Column 1 | Column 2 | Column 3 | Column 4 |
Goal:
User can select multiple columns while holding mouse button down and hovering over the columns.
e.g User clicks on column 1 and holds the mouse button down, moves over column 2 and 3 resulting in column 2 and 3 being selected.
I tried:
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);
}
}
});
However it doesn't work.
Try this one
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());
}
}
});
Here is the dummy CSS for selected cell
.selectedCell{
border: 2px solid #F3F7FB;
background-color: #F00;
font-weight: bold;
}
In the above code, we are listening for few events and based on their sequence we can identify the selected columns.
Sequence of events:
focus
event on any columnmousedown
event to start dragging on anther columnsfocus
and mousedown
both are true then collect all the columns on mouseover
eventblur
event, change the style of all the selected cells.focus
event snapshots
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With