Browse Source

Add row index to ItemClickEvent in Grid (#10754)

tags/8.4.0.alpha1
Tatu Lund 6 years ago
parent
commit
98be8f19d0

+ 2
- 1
client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java View File

@@ -108,9 +108,10 @@ public class GridConnector extends AbstractListingConnector
NativeEvent mouseEvent) {
String rowKey = getRowKey((JsonObject) cell.getRow());
String columnId = columnToIdMap.get(cell.getColumn());
int rowIndex = cell.getRowIndex();
getRpcProxy(GridServerRpc.class).itemClick(rowKey, columnId,
MouseEventDetailsBuilder
.buildMouseEventDetails(mouseEvent));
.buildMouseEventDetails(mouseEvent), rowIndex);
}
}


+ 14
- 3
server/src/main/java/com/vaadin/ui/Grid.java View File

@@ -347,6 +347,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
private final T item;
private final Column<T, ?> column;
private final MouseEventDetails mouseEventDetails;
private final int rowIndex;

/**
* Creates a new {@code ItemClick} event containing the given item and
@@ -354,11 +355,12 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
*
*/
public ItemClick(Grid<T> source, Column<T, ?> column, T item,
MouseEventDetails mouseEventDetails) {
MouseEventDetails mouseEventDetails, int rowIndex) {
super(source);
this.column = column;
this.item = item;
this.mouseEventDetails = mouseEventDetails;
this.rowIndex = rowIndex;
}

/**
@@ -397,6 +399,15 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
public MouseEventDetails getMouseEventDetails() {
return mouseEventDetails;
}

/**
* Returns the clicked rowIndex.
*
* @return the clicked rowIndex
*/
public int getRowIndex() {
return rowIndex;
}
}

/**
@@ -625,10 +636,10 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,

@Override
public void itemClick(String rowKey, String columnInternalId,
MouseEventDetails details) {
MouseEventDetails details, int rowIndex) {
Column<T, ?> column = getColumnByInternalId(columnInternalId);
T item = getDataCommunicator().getKeyMapper().get(rowKey);
fireEvent(new ItemClick<>(Grid.this, column, item, details));
fireEvent(new ItemClick<>(Grid.this, column, item, details, rowIndex));
}

@Override

+ 3
- 1
shared/src/main/java/com/vaadin/shared/ui/grid/GridServerRpc.java View File

@@ -42,9 +42,11 @@ public interface GridServerRpc extends ServerRpc {
* column internal id identifying the clicked property
* @param details
* mouse event details
* @param rowIndex
* the row index of the clicked item
*/
void itemClick(String rowKey, String columnInternalId,
MouseEventDetails details);
MouseEventDetails details, int rowIndex);

/**
* Informs the server that a context click has happened inside of Grid.

+ 1
- 1
uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java View File

@@ -435,7 +435,7 @@ public class GridBasics extends AbstractTestUIWithLog {
!grid.isDetailsVisible(event.getItem()));
log("Item click on row "
+ event.getItem().getRowNumber() + ", Column '"
+ event.getColumn().getCaption() + "'");
+ event.getColumn().getCaption() + "' Index " + event.getRowIndex());
});
log("Registered an item click listener.");
}

+ 30
- 0
uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridEventListenersTest.java View File

@@ -0,0 +1,30 @@
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));
}
}

Loading…
Cancel
Save