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;
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
/**
* 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");
}
this.userId = id;
getGrid().setColumnId(id, this);
+
return this;
}
/**
* 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
* 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
* 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
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;
Assert.assertEquals(new HashSet<>(Arrays.asList("Lorem", "2000")),
values);
+
+ assertSingleSortProperty(nameColumn, "name");
+ assertSingleSortProperty(bornColumn, "born");
}
@Test
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()) {