]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add convenience overloading for setColumn (#12093) 8.12.0.alpha2
authorTatu Lund <tatu@vaadin.com>
Fri, 4 Sep 2020 10:23:34 +0000 (13:23 +0300)
committerGitHub <noreply@github.com>
Fri, 4 Sep 2020 10:23:34 +0000 (13:23 +0300)
Fixes: https://github.com/vaadin/framework/issues/7773
server/src/main/java/com/vaadin/ui/Grid.java
server/src/test/java/com/vaadin/tests/server/component/grid/GridTest.java

index bba799690dc1a05adaeb99f6c4e8e386b5e8ee58..eb903287255a48e0e764f1c98193b9c2da580fb2 100644 (file)
@@ -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++;
index 6f1a227a1c4c12d415f08f8e92f52cf0303ef02b..532095996707144522bb04188435d706d5787840 100644 (file)
@@ -570,6 +570,25 @@ public class GridTest {
         assertEquals("name", columns.get(1).getId());
     }
 
+    @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);