Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Make automatic columns sortable by default if the property is Comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
gilberto-torrezan authored and pekam committed Jul 26, 2018
1 parent 3ea0274 commit d5e61ec
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
27 changes: 19 additions & 8 deletions src/main/java/com/vaadin/flow/component/grid/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,9 @@ public Grid(int pageSize) {
* properties. The property-values of the bean will be converted to Strings.
* Full names of the properties will be used as the
* {@link Column#setKey(String) column keys} and the property captions will
* be used as the {@link Column#setHeader(String) column headers}.
* be used as the {@link Column#setHeader(String) column headers}. The
* generated columns will be sortable by default, if the property is
* {@link Comparable}.
* <p>
* By default, only the direct properties of the bean are included and they
* will be in alphabetical order. Use {@link Grid#setColumns(String...)} to
Expand Down Expand Up @@ -1259,9 +1261,6 @@ public Column<T> addColumn(Renderer<T> renderer,
}

/**
* <strong>Note:</strong> This method can only be used for a Grid created
* from a bean type with {@link #Grid(Class)}.
* <p>
* Adds a new column for the given property name. The property values are
* converted to Strings in the grid cells. The property's full name will be
* used as the {@link Column#setKey(String) column key} and the property
Expand All @@ -1270,6 +1269,13 @@ public Column<T> addColumn(Renderer<T> renderer,
* <p>
* You can add columns for nested properties with dot notation, eg.
* <code>"property.nestedProperty"</code>
* <p>
* If the property is {@link Comparable}, the created column is sortable by
* default. This can be changed with the {@link Column#setSortable(boolean)}
* method.
* <p>
* <strong>Note:</strong> This method can only be used for a Grid created
* from a bean type with {@link #Grid(Class)}.
*
* @param propertyName
* the property name of the new column, not <code>null</code>
Expand Down Expand Up @@ -1297,12 +1303,17 @@ private Column<T> addColumn(PropertyDefinition<T, ?> property) {
item -> runPropertyValueGetter(property, item))
.setHeader(property.getCaption());
try {
return column.setKey(property.getName());
column.setKey(property.getName());
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException(
"Multiple columns for the same property: "
+ property.getName());
}

if (Comparable.class.isAssignableFrom(property.getType())) {
column.setSortable(true);
}
return column;
}

private Object runPropertyValueGetter(PropertyDefinition<T, ?> property,
Expand All @@ -1311,9 +1322,6 @@ private Object runPropertyValueGetter(PropertyDefinition<T, ?> property,
}

/**
* <strong>Note:</strong> This method can only be used for a Grid created
* from a bean type with {@link #Grid(Class)}.
* <p>
* Sets the columns and their order based on the given properties.
* <p>
* This is a shortcut for removing all columns and then calling
Expand All @@ -1323,6 +1331,9 @@ private Object runPropertyValueGetter(PropertyDefinition<T, ?> property,
* <code>"property.nestedProperty"</code>
* <p>
* Note that this also resets the headers and footers.
* <p>
* <strong>Note:</strong> This method can only be used for a Grid created
* from a bean type with {@link #Grid(Class)}.
*
* @param propertyNames
* the properties to create columns for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ public static class SortableBean {
private int integer;
private boolean bool;
private double number;
private Object notComparable;

private SortableBean innerBean;

public SortableBean(String string, int integer, boolean bool,
double number, SortableBean innerBean) {
double number, SortableBean innerBean, Object notComparable) {
this.string = string;
this.integer = integer;
this.bool = bool;
this.number = number;
this.innerBean = innerBean;
this.notComparable = notComparable;
}

public String getString() {
Expand Down Expand Up @@ -88,6 +90,14 @@ public SortableBean getInnerBean() {
public void setInnerBean(SortableBean innerBean) {
this.innerBean = innerBean;
}

public void setNotComparable(Object notComparable) {
this.notComparable = notComparable;
}

public Object getNotComparable() {
return notComparable;
}
}

private Grid<SortableBean> grid;
Expand Down Expand Up @@ -174,15 +184,43 @@ public void innerPropertiesAreSortedAsComparables() {
assertInMemorySorting((b1, b2) -> Double.compare(
b2.getInnerBean().getNumber(), b1.getInnerBean().getNumber()));
}

@Test
public void setSortableColumns_onlyComparablePropertiesAreSortable() {
Assert.assertTrue(grid.getColumnByKey("string").isSortable());
Assert.assertFalse(grid.getColumnByKey("notComparable").isSortable());

grid.setColumns("string", "notComparable", "innerBean",
"innerBean.string");

Assert.assertTrue(grid.getColumnByKey("string").isSortable());
Assert.assertFalse(grid.getColumnByKey("notComparable").isSortable());
Assert.assertFalse(grid.getColumnByKey("innerBean").isSortable());
Assert.assertTrue(grid.getColumnByKey("innerBean.string").isSortable());

grid.addColumn("bool");
grid.addColumn("innerBean.notComparable");

Assert.assertTrue(grid.getColumnByKey("bool").isSortable());
Assert.assertFalse(
grid.getColumnByKey("innerBean.notComparable").isSortable());

}

private List<SortableBean> createBeans() {
return Arrays.asList(
new SortableBean("Bean A", 9, false, 9.5,
new SortableBean("Sub A", 111, true, 111.5, null)),
new SortableBean("Sub A", 111, true, 111.5, null,
"Not comparable A"),
"Not comparable 1"),
new SortableBean("Bean B", 111, true, 111.5,
new SortableBean("Sub B", 1, false, 1.5, null)),
new SortableBean("Bean C", 1, false, 1.5,
new SortableBean("Sub C", 9, false, 9.5, null)));
new SortableBean("Sub B", 1, false, 1.5, null,
"Not comparable B"),
"Not comparable 2"),
new SortableBean(
"Bean C", 1, false, 1.5, new SortableBean("Sub C", 9,
false, 9.5, null, "Not comparable C"),
"Not comparable 3"));
}

private void assertInMemorySorting(Comparator<SortableBean> comparator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public BeanGridSortingPage() {
grid.setItems(new Person("Person 1", 99), new Person("Person 2", 1111),
new Person("Person 3", 1));
grid.setColumns("name", "born");
grid.getColumnByKey("born").setSortable(true);
add(grid);
}

Expand Down

0 comments on commit d5e61ec

Please sign in to comment.