Close

Entries with tag framework 8 .

Mission RIP Table: Migrate to Grid! - Selection

This is a series of tutorials for upgrading the Table component to Grid. It will be performed by migrating to Framework 7 style of Grid, then migrating to Framework 8. As a reference, Gridv7 is the Grid component from Framework 7, while Grid is the latest Grid component in Framework 8. Naturally, you can also migrate straight from Table to the Framework 8 style Grid.

 

In the previous post, we saw the migration of a basic Table to Grid for Framework 7, then to Framework 8. Moving forward, I will skip all similar code related to containers and populating data, and will only focus on the highlighted APIs for the examples we have.

 

In this part, I will cover two examples about the selection modes, and make some comparisons on how Grid deals with data, as well as some corner cases that got modified.

Single Selection

Migrate from Table to Framework 7 Grid

Grid by default has single selection, so we don’t need to explicitly write this:

table.setSelectable(true);

But at any point, if you want to move or revert back to default selection mode, you can use:

table.setSelectionMode(SelectionMode.SINGLE);
 

Grid is immediate by default, so we don’t need this line anymore:

table.setImmediate(true);
 

To keep track of a selected item, instead of:

table.addValueChangeListener( … )

We use:

table.addSelectionListener( … )
 

And the selection event does not contain information about the selected item, instead of:

event.getProperty().getValue().toString();

We get it from the table directly:

table.getSelectedRow();
 

Related commit: Single Select: To Grid v7

 

The event.getProperty().getValue() was always returning a raw Object, instead of a class of the type of the container’s data, and it was required to cast it. But losing information about the selected item all together was not an optimal solution either in the early phase of Grid, and this got fixed as we will see in Framework 8.

Migrate to Framework 8 Grid

No big difference from the previous code, except that the new Grid deals with data a bit differently, so we need to differentiate between SingleSelect and MultiSelect, this code:

if (table.getSelectedRow() != null)

Should be replaced with:

if (table.asSingleSelect().getValue() != null)
 

And to get the selected bean, you can replace this code:

table.getSelectedRow()

With:

table.asSingleSelect().getValue()
 

And here comes an important note: table.getSelectedRow() returns the ID of the selected row, which was a number because it is using an IndexedContainer. Most often developers were actually using BeanItemContainer, where the ID was the actual bean presented on the selected row. Now, in Framework 8, it is always the bean.

 

One more thing we notice, if we compare the final result here, is the order of the columns, as well as the case of the first letter in the column name.

You can adjust the order using setColumnOrder:

table.setColumnOrder("name", "city", "year");

And you can modify the captions manually using setCaption:

table.getColumn("name").setCaption("name");
 

In this demo I will skip all those corner cases, they are explained here just for elaboration.

 

Related commit: Single Select: To Grid v8

Multi Selection

Migrate from Table to Framework 7 Grid

Similar to what we have done in Single Selection, here is the only code difference to get multi selection enabled, instead of:

table.setMultiSelect(true);

Use:

table.setSelectionMode(Gridv7.SelectionMode.MULTI);
 

The result is different though, multi selection in Grid is done through checkboxes instead of holding ctrl/cmd while selecting other items. There is a feature request for it, however it is not highly demanded by many developers. If you want this feature to be implemented, I encourage you to show interest by giving it a +1 on Github, otherwise it might never be implemented due to low need by the community, and probably due to bad UX.

 

Related commit: Multi Select: To Grid v7

Migrate to Framework 8 Grid

In Framework 8, Grid multi selection would be defined as the following:

table.setSelectionMode(Grid.SelectionMode.MULTI);
 

And retrieving the set of selected beans is done by using:

table.asMultiSelect().getSelectedItems())
 

Related commit: Multi Select: To Grid v8

 

Do you think migration from Table to Grid is complicated or doable?

Mission RIP Table: Migrate to Grid! - Basic

As promised earlier, here is the first and most basic example. As a reference, Gridv7 is the Grid component from Framework 7, while Grid is the latest Grid component in Framework 8. Naturally, you can also migrate straight from Table to the Framework 8 style Grid.

Migrate from Table to Framework 7 Grid

Before starting, we need to remember that the fundamental difference between Table and Grid is that Grid is a pure Component, while Table is a mixture of a Component and a Container at the same time. So whenever we have a Table without an external declaration of Container, we must create one first to be able to use it with Grid:

IndexedContainer container = new IndexedContainer();

 

The declaration of Table and Grid is pretty similar, so replacing:

Table table = new Table("The Brightest Stars");

With:

Gridv7 table = new Gridv7("The Brightest Stars", container);

 

And here I defined the container in the constructor. We can do that in a separate line as well using setContainerDataSource. The important step now is to replace all Container-related calls that were directly using Table, to use container, for example replace:

table.addContainerProperty("Name", String.class, null);

With:

container.addContainerProperty("Name", String.class, null);

 

There is no addItem() API anymore in the Containers. You can not do something like this:

table.addItem(new Object[]{"Canopus", -0.72f}, 2);

But instead:

itemId = container.addItem(2);
itemId.getItemProperty("Name").setValue("Canopus");
itemId.getItemProperty("Mag").setValue(-0.72f);

 

While this sounds like a drawback at first, the main reason behind this architecture was to get rid of manual filling of data components and make them all based on Containers. Basically you should not fill any data components one by one using addItem, that’s why it has disappeared completely in Framework 8.

 

The setPageLength API specifies the height of the Table. Grid’s height can be specified using different methods, that’s why this line:

table.setPageLength(table.size());

Needs to be replaced with two lines:

table.setHeightMode(HeightMode.ROW);
table.setHeightByRows(container.size());
 

Before you feel like you have done everything, there is one small thing missing if you want an exact migration from Table to Grid. You must disable the selection because that was the case by default in Table:

table.setSelectionMode(Gridv7.SelectionMode.NONE);

But in future examples I will ignore this extra line.

 

Related commit: Basic: To Grid v7 first method

 

A perfect migration would involve architecture modernization as well, that’s why I prefer not to stop at this point, and change the way how the data is populated inside this basic Grid. This will also help in future migration to Framework 8.

 

So I define a simple hard coded data and populate it inside the container:

Object[][] data = {
  {"Sirius", -1.46f},
  {"Canopus", -0.72f},
  {"Arcturus", -0.04f},
  {"Alpha Centauri", -0.01f}
};

IndexedContainer container
    = new IndexedContainer(Arrays.asList(data));
 

And this will make it easy to iterate over them while giving values:

for(Object[] item: data) {
  container.getItem(item).getItemProperty("Name")
      .setValue(item[0]);
  container.getItem(item).getItemProperty("Mag")
      .setValue(item[1]);
}
 

This replaces the manual hard coded definition of items. Note that we don’t need to define items anymore, we just set the values.

 

Related commit: Basic: To Grid v7 second method

Migrate to Framework 8 Grid

Grid in Framework 8 is way much simpler, we don’t use Containers nor Property anymore. Grid is parameterized with the type of data you want to display. So instead of:

IndexedContainer container
    = new IndexedContainer(Arrays.asList(data));
Gridv7 table = new Gridv7("The Brightest Stars", container);

We set the data directly into Grid:

Grid<object> table = new Grid<>("The Brightest Stars");
table.setItems(data);
 

We don’t need to define properties manually anymore, nor define items one by one, we can use some lambda magic:

table.addColumn(v -> v[0]).setCaption("Name");
table.addColumn(v -> v[1]).setCaption("Mag");
 

The first part table.addColumn(v -> v[i]) defines how the columns are provided from the given data, and the second part setCaption("Name") is to manually give a name for the column, since we are not using a proper POJO that can help us automatically generate the column names. Later there will be some more examples about this, but now we can see how this first example is easily transformed into a very simple code with the power of Framework 8.

 

Related commit: Basic: To Grid v8

 

Does this give you a good kickstarter for your migration process? Let us know how you feel about it so far in a comment below!

Mission RIP Table: Migrate to Grid! - Intro

Vaadin Grid was announced over two years ago, and since then it has evolved with many new features and enhancements. If this wasn’t enough killer for the Table component, then the announcement of Vaadin Framework 8 earlier this year, with explicit deprecation of Table, places it in a critical state, and it’s now time to upgrade your source code and enjoy the latest features, bug fixes, and all latest updates.

 

Vaadin Table was one of the oldest components in Vaadin Framework, and it survived many years of continuous development and upgrades. But technology is fast moving forward and the decision of replacing it with a new rewritten implementation was a must, to be able to cope with the latest technology requirements and web standards. There are plenty of resources available online comparing Table and Grid, so I’m not going to make yet another comparison here, but instead, I’ll be going over a series of tutorials showing you how to migrate from Table to Grid.

 

I will start with this project (note the tag) that consists of many Table examples, and migrate them one by one to use Grid. Those examples are a little bit modified versions of the book examples, and I will publish the migration guide in a few chunks over the coming few weeks.

 

The upgrade will be performed by first migrating to Framework 7 style of Grid, then migrating to Framework 8. This will give double advantages for all legacy code out there!


So stay tuned, in the next post I will be migrating some of those examples!

 

Which of those examples you want to see migrated first? Let me know in a comment below.

— 3 Items per Page
Showing 1 - 3 of 5 results.