简体   繁体   中英

Accessing Vaadin 8 Grid cell value

I'm migrating from Vaadin 7 Table to Vaadin 8 Grid. In the Vaadin 7 Table we used IndexedContainer or Container with Property and could access the table cell value with IndexedContainer.getItem(propertyId).getItemProperty(itemId).getValue(); or Container.getContainerProperty(itemId, propertyId).getValue() .

The content of a Vaadin 8 Grid is set via Grid.setItems(Collection<T>) . I found how to access the columns - Grid.getColumns() - and the data provider - Grid.getDataProvider() . But I did not find a way to combine them and get the value of a specific cell...

Is there a way to access a cell value in a similar way when using a Vaadin 8 Grid?

Update:

I currently try to figure out the way in accessing data through Grid methods. Currently I have the Vaadin 7 Table code where we access the cell values in a generic way to export the data to eg Excel, CSV or PDF:


    Table table = ...; /* Is passed in while initializing and is used as kind of black box in the exporter */ 

    // Compiling cell values
    Object[] visibleColumns = table.getVisibleColumns();
    Container container = table.getContainerDataSource();
    for (Object itemId : container.getItemIds())
    {
        for (Object propertyId : visibleColumns)
        {
            Property property = container.getContainerProperty(itemId, propertyId);
            Object value = property == null ? null : property.getValue();
            buildCell(value);
        }
    }

In Vaadin 8 I have the Grid and I tried to adapt:


    Grid grid = ...; // Passed in while initializing

    // Compiling cell values
    List visibleColumns = new ArrayList();
    List columns = table.getColumns();
    for(Iterator iterator = columns.iterator(); iterator.hasNext();)
    {
        Column column = iterator.next();
        if (!column.isHidden())
        {
            visibleColumns.add(column);
        }
    }

    // The Grid gets a Collection set as items, so I assume the rows should be ListDataProvider
    ListDataProvider listContainer = (ListDataProvider)grid.getDataProvider();
    Collection items = listContainer.getItems();
    for (/* Now I should be able to get the stored values in each row, but I assume I need the data types ... */)
    {
        for (Column column : visibleColumns)
        {
            String propertyId = column.getId(); // Could be null if no ID is given
            /* At least here I'd like to have access to the cell value */
        }
    }

It depends greatly what you are going to do with the data/cell, but here is my shot:

Firstly: Vaadin8 Grid is a generic type so like Grid<Entity> and same applies to DataProvider<Entity> .

Use the data provider. Get it from the Grid grid.getDataProvider() or maybe you have the direct access to it already.

You can have a Stream<Entity> by applying dataSource.fetch(new Query<>()) . And use the stream to loop OR find or anything.

For example:

Stream<Entity> streamEntities = g.getDataProvider().fetch(new Query<>());
streamEntities.forEach( entity -> {
   entity.getStuff();   // THE POINT IS HERE
                        // no cell value but entitys field by getter
   addAsCsvRow(entity); // example of some method you might implement
                        // to process outside lambda
});

Note

  • you can filter entites by adding predicate when constructing Query for fetch OR use stream filter streamEntities.filter(...)
  • you need some casting (Entity) and still knowledge about the entity type if Grid / DataProvider is of raw type when you access it.
  • if not familiar with Stream a List might be more comfortable List<Entity> list = streamEntities.collect(Collectors.toList());

First of all, I'll answer the questions and then show my solution.

@rmuller The grid value is used for export in XLS, CSV or PDF.

As the export need to handle every type of data that is used in the grid, the whole implementation need to be generic. And the export need to handle hieranchical data ( TreeDataProvider ) as well as just listed data ( ListDataProvider ).

Finally the code so far, I keep on the easier ListDataProvider :

DataProvider dataProvider = grid.getDataProvider();
ListDataProvider listContainer = (ListDataProvider) dataProvider;
List<Grid.Column> columns = grid.getColumns();
for (Grid.Column column : columns)
{
    for (Object item : listContainer.getItems())
    {
        ValueProvider valueProvider = column.getValueProvider();
        Object appliedValue = valueProvider.apply(item);
        String sValue = appliedValue == null ? null : appliedValue.toString()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM