]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Grid inline data declarative support (#16596)
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Tue, 14 Apr 2015 08:45:19 +0000 (11:45 +0300)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Tue, 14 Apr 2015 14:11:05 +0000 (17:11 +0300)
Change-Id: Ief0238c04ff16674a4102d359381531ae30e9a9f

server/src/com/vaadin/ui/Grid.java
server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java
server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridInlineDataDeclarativeTest.java [new file with mode: 0644]

index 3b602abf1a64c81edd23b1f72022c59d83e94bd0..4fda7f728897642a45b8fd3ae463eb63499c88d2 100644 (file)
@@ -98,6 +98,7 @@ import com.vaadin.shared.util.SharedUtil;
 import com.vaadin.ui.declarative.DesignAttributeHandler;
 import com.vaadin.ui.declarative.DesignContext;
 import com.vaadin.ui.declarative.DesignException;
+import com.vaadin.ui.renderers.HtmlRenderer;
 import com.vaadin.ui.renderers.Renderer;
 import com.vaadin.ui.renderers.TextRenderer;
 import com.vaadin.util.ReflectTools;
@@ -5468,7 +5469,19 @@ public class Grid extends AbstractComponent implements SelectionNotifier,
                 if (child.tagName().equals("thead")) {
                     header.readDesign(child, context);
                 } else if (child.tagName().equals("tbody")) {
-                    // TODO: Inline data
+                    for (Element row : child.children()) {
+                        Elements cells = row.children();
+                        Object[] data = new String[cells.size()];
+                        for (int c = 0; c < cells.size(); ++c) {
+                            data[c] = cells.get(c).html();
+                        }
+                        addRow(data);
+                    }
+
+                    // Since inline data is used, set HTML renderer for columns
+                    for (Column c : getColumns()) {
+                        c.setRenderer(new HtmlRenderer());
+                    }
                 } else if (child.tagName().equals("tfoot")) {
                     footer.readDesign(child, context);
                 }
@@ -5529,7 +5542,18 @@ public class Grid extends AbstractComponent implements SelectionNotifier,
         // Always write thead. Reads correctly when there no header rows
         header.writeDesign(tableElement.appendElement("thead"), context);
 
-        // TODO: Body
+        if (context.shouldWriteData(this)) {
+            Element bodyElement = tableElement.appendElement("tbody");
+            for (Object itemId : datasource.getItemIds()) {
+                Element tableRow = bodyElement.appendElement("tr");
+                for (Column c : getColumns()) {
+                    Object value = datasource.getItem(itemId)
+                            .getItemProperty(c.getPropertyId()).getValue();
+                    tableRow.appendElement("td").append(
+                            (value != null ? value.toString() : ""));
+                }
+            }
+        }
 
         if (footer.getRowCount() > 0) {
             footer.writeDesign(tableElement.appendElement("tfoot"), context);
index 0a32ec1734ceb26af16b9d51275124e8a3fbd64b..2a4b3f01fcafa90ae19e164d2b73203a9844d3ef 100644 (file)
@@ -35,6 +35,11 @@ public class GridDeclarativeTestBase extends DeclarativeTestBase<Grid> {
     }
 
     public Grid testRead(String design, Grid expected, boolean retestWrite) {
+        return testRead(design, expected, retestWrite, false);
+    }
+
+    public Grid testRead(String design, Grid expected, boolean retestWrite,
+            boolean writeData) {
         Grid actual = super.testRead(design, expected);
 
         compareGridColumns(expected, actual);
@@ -42,7 +47,7 @@ public class GridDeclarativeTestBase extends DeclarativeTestBase<Grid> {
         compareFooters(expected, actual);
 
         if (retestWrite) {
-            testWrite(design, actual);
+            testWrite(design, actual, writeData);
         }
 
         return actual;
diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridInlineDataDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridInlineDataDeclarativeTest.java
new file mode 100644 (file)
index 0000000..fefd49a
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.server.component.grid.declarative;
+
+import org.junit.Test;
+
+import com.vaadin.ui.Grid;
+
+public class GridInlineDataDeclarativeTest extends GridDeclarativeTestBase {
+
+    @Test
+    public void testSimpleInlineData() {
+        String design = "<v-grid><table>"//
+                + "<colgroup>"
+                + "   <col sortable=true property-id='Col1' />"
+                + "</colgroup>" //
+                + "<thead />" // No headers read or written
+                + "<tbody>" //
+                + "<tr><td>Foo</tr>" //
+                + "<tr><td>Bar</tr>" //
+                + "<tr><td>Baz</tr>" //
+                + "</table></v-grid>";
+
+        Grid grid = new Grid();
+        grid.addColumn("Col1", String.class);
+        grid.addRow("Foo");
+        grid.addRow("Bar");
+        grid.addRow("Baz");
+
+        // Remove default header
+        grid.removeHeaderRow(grid.getDefaultHeaderRow());
+
+        testWrite(design, grid, true);
+        testRead(design, grid, true, true);
+    }
+
+    @Test
+    public void testMultipleColumnsInlineData() {
+        String design = "<v-grid><table>"//
+                + "<colgroup>"
+                + "   <col sortable=true property-id='Col1' />"
+                + "   <col sortable=true property-id='Col2' />"
+                + "   <col sortable=true property-id='Col3' />" //
+                + "</colgroup>" //
+                + "<thead />" // No headers read or written
+                + "<tbody>" //
+                + "<tr><td>Foo<td>Bar<td>Baz</tr>" //
+                + "<tr><td>My<td>Summer<td>Car</tr>" //
+                + "</table></v-grid>";
+
+        Grid grid = new Grid();
+        grid.addColumn("Col1", String.class);
+        grid.addColumn("Col2", String.class);
+        grid.addColumn("Col3", String.class);
+        grid.addRow("Foo", "Bar", "Baz");
+        grid.addRow("My", "Summer", "Car");
+
+        // Remove default header
+        grid.removeHeaderRow(grid.getDefaultHeaderRow());
+
+        testWrite(design, grid, true);
+        testRead(design, grid, true, true);
+    }
+
+    @Test
+    public void testMultipleColumnsInlineDataReordered() {
+        String design = "<v-grid><table>"//
+                + "<colgroup>"
+                + "   <col sortable=true property-id='Col2' />"
+                + "   <col sortable=true property-id='Col3' />"
+                + "   <col sortable=true property-id='Col1' />" //
+                + "</colgroup>" //
+                + "<thead />" // No headers read or written
+                + "<tbody>" //
+                + "<tr><td>Bar<td>Baz<td>Foo</tr>" //
+                + "<tr><td>Summer<td>Car<td>My</tr>" //
+                + "</table></v-grid>";
+
+        Grid grid = new Grid();
+        grid.addColumn("Col1", String.class);
+        grid.addColumn("Col2", String.class);
+        grid.addColumn("Col3", String.class);
+        grid.addRow("Foo", "Bar", "Baz");
+        grid.addRow("My", "Summer", "Car");
+        grid.setColumnOrder("Col2", "Col3", "Col1");
+
+        // Remove default header
+        grid.removeHeaderRow(grid.getDefaultHeaderRow());
+
+        testWrite(design, grid, true);
+        testRead(design, grid, true, true);
+    }
+}