diff options
-rw-r--r-- | server/src/main/java/com/vaadin/ui/Grid.java | 30 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java | 35 |
2 files changed, 63 insertions, 2 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index fb5eeb6428..b11bef7eed 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -818,7 +818,15 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, private final SerializableFunction<T, ? extends V> valueProvider; - private SortOrderProvider sortOrderProvider; + private SortOrderProvider sortOrderProvider = direction -> { + String id = getId(); + if (id == null) { + return Stream.empty(); + } else { + return Stream.of(new QuerySortOrder(id, direction)); + } + }; + private SerializableComparator<T> comparator; private StyleGenerator<T> styleGenerator = item -> null; private DescriptionGenerator<T> descriptionGenerator; @@ -849,7 +857,6 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, state.renderer = renderer; state.caption = ""; - sortOrderProvider = d -> Stream.of(); // Add the renderer as a child extension of this extension, thus // ensuring the renderer will be unregistered when this column is @@ -1040,9 +1047,17 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, /** * Sets the user-defined identifier to map this column. The identifier * can be used for example in {@link Grid#getColumn(String)}. + * <p> + * The id is also used as the {@link #setSortProperty(String...) backend + * sort property} for this column if no sort property or sort order + * provider has been set for this column. + * + * @see #setSortProperty(String...) + * @see #setSortOrderProvider(SortOrderProvider) * * @param id * the identifier string + * @return this column */ public Column<T, V> setId(String id) { Objects.requireNonNull(id, "Column identifier cannot be null"); @@ -1052,6 +1067,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, } this.userId = id; getGrid().setColumnId(id, this); + return this; } @@ -1143,6 +1159,9 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, /** * Sets strings describing back end properties to be used when sorting * this column. + * <p> + * By default, the {@link #setId(String) column id} will be used as the + * sort property. * * @param properties * the array of strings describing backend properties @@ -1159,6 +1178,9 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, * Sets the sort orders when sorting this column. The sort order * provider is a function which provides {@link QuerySortOrder} objects * to describe how to sort by this column. + * <p> + * By default, the {@link #setId(String) column id} will be used as the + * sort property. * * @param provider * the function to use when generating sort orders with the @@ -1176,6 +1198,10 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, * Gets the sort orders to use with back-end sorting for this column * when sorting in the given direction. * + * @see #setSortProperty(String...) + * @see #setId(String) + * @see #setSortOrderProvider(SortOrderProvider) + * * @param direction * the sorting direction * @return stream of sort orders diff --git a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java index 9762b7235e..fafbd4c900 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java @@ -27,6 +27,7 @@ import com.vaadin.data.Binder.Binding; import com.vaadin.data.ValidationException; import com.vaadin.data.ValueProvider; import com.vaadin.data.provider.GridSortOrder; +import com.vaadin.data.provider.QuerySortOrder; import com.vaadin.data.provider.bov.Person; import com.vaadin.event.selection.SelectionEvent; import com.vaadin.server.SerializableComparator; @@ -306,6 +307,9 @@ public class GridTest { Assert.assertEquals(new HashSet<>(Arrays.asList("Lorem", "2000")), values); + + assertSingleSortProperty(nameColumn, "name"); + assertSingleSortProperty(bornColumn, "born"); } @Test @@ -520,6 +524,37 @@ public class GridTest { grid.addColumn("name", new NumberRenderer()); } + @Test + public void columnId_sortProperty() { + assertSingleSortProperty(lengthColumn, "length"); + } + + @Test + public void columnId_sortProperty_noId() { + Assert.assertEquals(0, + objectColumn.getSortOrder(SortDirection.ASCENDING).count()); + } + + @Test + public void sortProperty_setId_doesntOverride() { + objectColumn.setSortProperty("foo"); + objectColumn.setId("bar"); + + assertSingleSortProperty(objectColumn, "foo"); + } + + private static void assertSingleSortProperty(Column<?, ?> column, + String expectedProperty) { + QuerySortOrder[] sortOrders = column + .getSortOrder(SortDirection.ASCENDING) + .toArray(QuerySortOrder[]::new); + + Assert.assertEquals(1, sortOrders.length); + Assert.assertEquals(SortDirection.ASCENDING, + sortOrders[0].getDirection()); + Assert.assertEquals(expectedProperty, sortOrders[0].getSorted()); + } + private static <T> JsonObject getRowData(Grid<T> grid, T row) { JsonObject json = Json.createObject(); if (grid.getColumns().isEmpty()) { |