[英]Accessing Vaadin 8 Grid cell value
我正在從 Vaadin 7 Table 遷移到 Vaadin 8 Grid。 在 Vaadin 7 表中,我們使用 IndexedContainer 或 Container with Property 並且可以使用IndexedContainer.getItem(propertyId).getItemProperty(itemId).getValue();
訪問表單元格值IndexedContainer.getItem(propertyId).getItemProperty(itemId).getValue();
或Container.getContainerProperty(itemId, propertyId).getValue()
。
Vaadin 8 Grid 的內容通過Grid.setItems(Collection<T>)
。 我找到了如何訪問列 - Grid.getColumns()
- 和數據提供者 - Grid.getDataProvider()
。 但是我沒有找到將它們組合起來並獲得特定單元格值的方法......
使用 Vaadin 8 Grid 時,有沒有辦法以類似的方式訪問單元格值?
我目前試圖找出通過 Grid 方法訪問數據的方式。 目前我有 Vaadin 7 Table 代碼,我們以通用方式訪問單元格值以將數據導出到例如 Excel、CSV 或 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);
}
}
在 Vaadin 8 中,我使用了 Grid 並嘗試適應:
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 */
}
}
這取決於你將如何處理數據/單元格,但這是我的鏡頭:
首先:Vaadin8 Grid
是generic
類型,因此類似於Grid<Entity>
,同樣適用於DataProvider<Entity>
。
使用數據提供者。 從Grid grid.getDataProvider()
獲取它,或者您可以直接訪問它。
您可以通過應用dataSource.fetch(new Query<>())
來擁有Stream<Entity>
。 並使用流循環或查找或任何東西。
例如:
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
});
注意
Query
時添加謂詞來過濾entites,或者使用流過濾器streamEntities.filter(...)
Grid
/ DataProvider
在您訪問時是原始類型,則需要一些轉換(Entity)
並且仍然需要了解實體類型。 Stream
, List
可能會更舒服List<Entity> list = streamEntities.collect(Collectors.toList());
首先,我將回答問題,然后展示我的解決方案。
@rmuller 網格值用於以 XLS、CSV 或 PDF 格式導出。
由於導出需要處理網格中使用的每種類型的數據,因此整個實現需要是通用的。 並且導出需要處理層次數據( TreeDataProvider
)以及剛剛列出的數據( ListDataProvider
)。
最后是到目前為止的代碼,我繼續使用更簡單的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()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.