diff options
author | Tatu Lund <tatu@vaadin.com> | 2020-09-04 13:23:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-04 13:23:34 +0300 |
commit | 665f3355a2f6a759ca7ee088025a4546c3953b02 (patch) | |
tree | b0166df139e67730dfc805b183251f033e9d60ac | |
parent | 10f7e15da976f2cd9ffd19674121cb1d403ef355 (diff) | |
download | vaadin-framework-665f3355a2f6a759ca7ee088025a4546c3953b02.tar.gz vaadin-framework-665f3355a2f6a759ca7ee088025a4546c3953b02.zip |
Add convenience overloading for setColumn (#12093)8.12.0.alpha2
Fixes: https://github.com/vaadin/framework/issues/7773
-rw-r--r-- | server/src/main/java/com/vaadin/ui/Grid.java | 22 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java | 19 |
2 files changed, 41 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java index bba799690d..eb90328725 100644 --- a/server/src/main/java/com/vaadin/ui/Grid.java +++ b/server/src/main/java/com/vaadin/ui/Grid.java @@ -4020,6 +4020,28 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents, .forEach(this::removeColumn); } + /** + * Sets the columns and their order based on their column ids provided that + * collection supports preserving of the order. Columns currently in this + * grid that are not present in the collection of column ids are removed. + * This includes any column that has no id. Similarly, any new column in + * columns will be added to this grid. New columns can only be added for a + * <code>Grid</code> created using {@link Grid#Grid(Class)} or + * {@link #withPropertySet(PropertySet)}. + * + * + * @param columnIds + * the column ids to set + * + * @see Column#setId(String) + * @see #setColumns(String...) + */ + public void setColumns(Collection<String> columnIds) { + Objects.requireNonNull(columnIds, "columnIds can't be null"); + String[] columns = columnIds.toArray(new String[columnIds.size()]); + setColumns(columns); + } + private String getGeneratedIdentifier() { String columnId = "" + counter; counter++; 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 6f1a227a1c..5320959967 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 @@ -571,6 +571,25 @@ public class GridTest { } @Test + public void setColumns_addColumns_v2_beangrid() { + Grid<Person> grid = new Grid<>(Person.class); + + // Remove so we can add it back + grid.removeColumn("name"); + + List<String> columnIds = new ArrayList<>(); + columnIds.add("born"); + columnIds.add("name"); + + grid.setColumns(columnIds); + + List<Column<Person, ?>> columns = grid.getColumns(); + assertEquals(2, columns.size()); + assertEquals("born", columns.get(0).getId()); + assertEquals("name", columns.get(1).getId()); + } + + @Test public void setColumnOrder_byColumn() { grid.setColumnOrder(randomColumn, lengthColumn); |