您可以從網格中獲取列,並可以從ComboBox中獲取這些列中的列。要做到這一點,我已經定義的類GridColumn
它代表com.vaadin.ui.Grid.Column我們感興趣的屬性(即id和標題字幕):
GridColumn.java
/**
* Represents a column in a Vaadin Grid
*/
public class GridColumn {
private String id; // propertyId in com.vaadin.ui.Grid.Column
private String header; // headerCaption in com.vaadin.ui.Grid.Column
// Getters and setters omitted
}
下面的代碼然後可以用於構建培訓相關的UI組件(網格和ComboBox),然後添加到佈局:
// Adapted from your code
List<Partner> partnerList = new ArrayList<Partner>();
partnerList.add(new Partner("AB", "Smith", "James", "Company A"));
partnerList.add(new Partner("DD", "Jones", "Robin", "Company B"));
BeanItemContainer<Partner> container = new BeanItemContainer<Partner>(Partner.class, partnerList);
Grid grid = new Grid();
grid.setColumns("code", "surname", "name", "companyName");
grid.getColumn("code").setHeaderCaption("Code");
grid.getColumn("lastname").setHeaderCaption("Last Name");
grid.getColumn("name").setHeaderCaption("First Name");
grid.getColumn("companyName").setHeaderCaption("Company");
grid.setContainerDataSource(container);
// Get the list of columns and as them as ComboBox items
List<Column> cols = grid.getColumns();
List<GridColumn> comboItems = new ArrayList<>();
for (Column column : cols) {
GridColumn col = new GridColumn();
col.setHeader(column.getHeaderCaption()); // Column Header Caption
col.setId((String) column.getPropertyId()); // Column "id"
comboItems.add(col);
}
// Construction of the ComboBox
ComboBox combo = new ComboBox();
// Use the List<GridColumn> as the container for the ComboBox items
combo.setContainerDataSource(new BeanItemContainer<GridColumn>(GridColumn.class, comboItems));
// Property of GridColumn to use as the value to display in the ComboBox
combo.setItemCaptionPropertyId("header");
combo.addBlurListener(e -> {
// Upon selection of a column, update the grid to only display that column
GridColumn selected = (GridColumn) combo.getValue();
System.out.println("Selected value is " + selected);
grid.setColumns(selected.getId());
grid.getColumn(selected.getId()).setHeaderCaption(selected.getHeader());
});
addComponents(combo, grid);
甲blurListener存在於組合框,使得當值是選中(焦點離開組合框),它會更新網格以僅顯示所選列。
圖1:在負載(顯示所有列)
圖2:組合框在動作
圖3:選擇在項該表中的組合框結果僅顯示該列
請詳細描述過濾應該如何工作。你還要檢查網格的[vaadin文檔](https://vaadin.com/docs/-/part/framework/components/components-grid.html)嗎? – Morfic