diff options
author | Tatu Lund <tatu@vaadin.com> | 2018-03-27 14:53:15 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-03-27 14:53:15 +0300 |
commit | 98be8f19d046f8f0fd7fe5a753bff2c67efed9fd (patch) | |
tree | b7f9a91abdc40c2c791414c80b12e7cdee22f728 | |
parent | 8b2da456bdbda7cd493bd9dc11bbb1dead368d5c (diff) | |
download | vaadin-framework-98be8f19d046f8f0fd7fe5a753bff2c67efed9fd.tar.gz vaadin-framework-98be8f19d046f8f0fd7fe5a753bff2c67efed9fd.zip |
Add row index to ItemClickEvent in Grid (#10754)
5 files changed, 50 insertions, 6 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java index b9521d06f5..77bcdf0d97 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridConnector.java @@ -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); } } diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index 22ac453216..0eeb132d50 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -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 diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/GridServerRpc.java b/shared/src/main/java/com/vaadin/shared/ui/grid/GridServerRpc.java index 644ae9b40f..c3718d4d2c 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/grid/GridServerRpc.java +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/GridServerRpc.java @@ -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. diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java index 484452b6ac..7391cbf4ff 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/GridBasics.java @@ -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."); } diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridEventListenersTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridEventListenersTest.java new file mode 100644 index 0000000000..9682b31251 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/GridEventListenersTest.java @@ -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)); + } +} |