From 7cb23bc63f794a7549dd79c37da2f8bb8e88e20d Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Tue, 14 Apr 2015 11:45:19 +0300 Subject: [PATCH] Fix Grid inline data declarative support (#16596) Change-Id: Ief0238c04ff16674a4102d359381531ae30e9a9f --- server/src/com/vaadin/ui/Grid.java | 28 ++++- .../declarative/GridDeclarativeTestBase.java | 7 +- .../GridInlineDataDeclarativeTest.java | 106 ++++++++++++++++++ 3 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridInlineDataDeclarativeTest.java diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 3b602abf1a..4fda7f7288 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -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); diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java index 0a32ec1734..2a4b3f01fc 100644 --- a/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java +++ b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeTestBase.java @@ -35,6 +35,11 @@ public class GridDeclarativeTestBase extends DeclarativeTestBase { } 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 { 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 index 0000000000..fefd49a587 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridInlineDataDeclarativeTest.java @@ -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 = ""// + + "" + + " " + + "" // + + "" // No headers read or written + + "" // + + "" // + + "" // + + "" // + + "
Foo
Bar
Baz
"; + + 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 = ""// + + "" + + " " + + " " + + " " // + + "" // + + "" // No headers read or written + + "" // + + "" // + + "" // + + "
FooBarBaz
MySummerCar
"; + + 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 = ""// + + "" + + " " + + " " + + " " // + + "" // + + "" // No headers read or written + + "" // + + "" // + + "" // + + "
BarBazFoo
SummerCarMy
"; + + 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); + } +} -- 2.39.5