NativeEvent mouseEvent) { | NativeEvent mouseEvent) { | ||||
String rowKey = getRowKey((JsonObject) cell.getRow()); | String rowKey = getRowKey((JsonObject) cell.getRow()); | ||||
String columnId = columnToIdMap.get(cell.getColumn()); | String columnId = columnToIdMap.get(cell.getColumn()); | ||||
int rowIndex = cell.getRowIndex(); | |||||
getRpcProxy(GridServerRpc.class).itemClick(rowKey, columnId, | getRpcProxy(GridServerRpc.class).itemClick(rowKey, columnId, | ||||
MouseEventDetailsBuilder | MouseEventDetailsBuilder | ||||
.buildMouseEventDetails(mouseEvent)); | |||||
.buildMouseEventDetails(mouseEvent), rowIndex); | |||||
} | } | ||||
} | } | ||||
private final T item; | private final T item; | ||||
private final Column<T, ?> column; | private final Column<T, ?> column; | ||||
private final MouseEventDetails mouseEventDetails; | private final MouseEventDetails mouseEventDetails; | ||||
private final int rowIndex; | |||||
/** | /** | ||||
* Creates a new {@code ItemClick} event containing the given item and | * Creates a new {@code ItemClick} event containing the given item and | ||||
* | * | ||||
*/ | */ | ||||
public ItemClick(Grid<T> source, Column<T, ?> column, T item, | public ItemClick(Grid<T> source, Column<T, ?> column, T item, | ||||
MouseEventDetails mouseEventDetails) { | |||||
MouseEventDetails mouseEventDetails, int rowIndex) { | |||||
super(source); | super(source); | ||||
this.column = column; | this.column = column; | ||||
this.item = item; | this.item = item; | ||||
this.mouseEventDetails = mouseEventDetails; | this.mouseEventDetails = mouseEventDetails; | ||||
this.rowIndex = rowIndex; | |||||
} | } | ||||
/** | /** | ||||
public MouseEventDetails getMouseEventDetails() { | public MouseEventDetails getMouseEventDetails() { | ||||
return mouseEventDetails; | return mouseEventDetails; | ||||
} | } | ||||
/** | |||||
* Returns the clicked rowIndex. | |||||
* | |||||
* @return the clicked rowIndex | |||||
*/ | |||||
public int getRowIndex() { | |||||
return rowIndex; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
@Override | @Override | ||||
public void itemClick(String rowKey, String columnInternalId, | public void itemClick(String rowKey, String columnInternalId, | ||||
MouseEventDetails details) { | |||||
MouseEventDetails details, int rowIndex) { | |||||
Column<T, ?> column = getColumnByInternalId(columnInternalId); | Column<T, ?> column = getColumnByInternalId(columnInternalId); | ||||
T item = getDataCommunicator().getKeyMapper().get(rowKey); | T item = getDataCommunicator().getKeyMapper().get(rowKey); | ||||
fireEvent(new ItemClick<>(Grid.this, column, item, details)); | |||||
fireEvent(new ItemClick<>(Grid.this, column, item, details, rowIndex)); | |||||
} | } | ||||
@Override | @Override |
* column internal id identifying the clicked property | * column internal id identifying the clicked property | ||||
* @param details | * @param details | ||||
* mouse event details | * mouse event details | ||||
* @param rowIndex | |||||
* the row index of the clicked item | |||||
*/ | */ | ||||
void itemClick(String rowKey, String columnInternalId, | void itemClick(String rowKey, String columnInternalId, | ||||
MouseEventDetails details); | |||||
MouseEventDetails details, int rowIndex); | |||||
/** | /** | ||||
* Informs the server that a context click has happened inside of Grid. | * Informs the server that a context click has happened inside of Grid. |
!grid.isDetailsVisible(event.getItem())); | !grid.isDetailsVisible(event.getItem())); | ||||
log("Item click on row " | log("Item click on row " | ||||
+ event.getItem().getRowNumber() + ", Column '" | + event.getItem().getRowNumber() + ", Column '" | ||||
+ event.getColumn().getCaption() + "'"); | |||||
+ event.getColumn().getCaption() + "' Index " + event.getRowIndex()); | |||||
}); | }); | ||||
log("Registered an item click listener."); | log("Registered an item click listener."); | ||||
} | } |
package com.vaadin.tests.components.grid.basics; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | |||||
import com.vaadin.testbench.elements.GridElement; | |||||
public class GridEventListenersTest extends GridBasicsTest { | |||||
@Test | |||||
public void testItemClickListener() { | |||||
selectMenuPath("Component", "State", "Item click listener"); | |||||
selectMenuPath("Component", "State", "Selection model", "none"); | |||||
checkItemClickOnRow(0); | |||||
checkItemClickOnRow(2); | |||||
GridElement grid = getGridElement(); | |||||
grid.getHeaderCell(0, 7); | |||||
checkItemClickOnRow(0); | |||||
checkItemClickOnRow(2); | |||||
} | |||||
private void checkItemClickOnRow(int row) { | |||||
GridElement grid = getGridElement(); | |||||
grid.getCell(row, 2).click(); | |||||
String logRow = getLogRow(0); | |||||
Assert.assertTrue( | |||||
"Log row '" + logRow + "' did not contain index " + row, | |||||
logRow.endsWith("Index " + row)); | |||||
} | |||||
} |