您可以使用addColumn(propertyName)
oraddColumn(ValueProvider, ...)
方法变体将列添加到网格。
我不熟悉 spark-data 源,所以我不确定Row
对象到底是什么。如果它是运行时生成的对象,具有从数据集列获得的属性名称,那么第一种方法应该是直接替换。如果Row
它类似于键值数据结构(例如地图),那么您可以使用第二种方法。
Row
对于本练习,我将使用HashMap
.
如果我遗漏了什么,请告诉我。
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.VerticalLayout;
import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;
public class GridWithMapObjects extends VerticalLayout {
// data to use in random generations
private static final Random RANDOM = new Random();
private static final String[] NAMES = {"Alex", "Jay", "John", "Mary", "Joan", "Corrine"};
private static final String[] SURNNAMES = {"Carlisle", "Dunn", "Albert", "Crow", "Picket", "Valden"};
private static final String[] NICKNAMES = {"The one", "The second", "The third", "The fourth", "The fifth", "The sixth"};
private static final String[] NATIONALITIES = {"American", "Algerian", "Italian", "Japanese", "Australian", "Romanian"};
private static final LocalDate[] BIRTYHDAYS = {LocalDate.parse("1970-01-01"), LocalDate.parse("1980-08-15"), LocalDate.parse("1990-05-02"), LocalDate.parse("2000-10-26"), LocalDate.parse("2010-06-06"), LocalDate.parse("2015-12-12")};
public GridWithMapObjects() {
// basic setup
Grid<Row> grid = new Grid<>();
regen(grid);
addComponents(grid, new Button("Regen grid", event -> regen(grid)));
}
// utility method to regenerate grid columns and data
private void regen(Grid<Row> grid) {
// generate some random data
DataSet dataSet = generateRandomDataSet();
// setup columns
grid.removeAllColumns();
for (String column : dataSet.getColumns()) {
grid.addColumn(row -> row.getValue(column)).setCaption(column);
}
// add items
grid.setItems(dataSet.getRows());
}
// utility method to generate some random data
private DataSet generateRandomDataSet() {
// randomly select some columns
int numberOfColumns = RANDOM.nextInt(ColumnGenerator.values().length) + 1;
Set<ColumnGenerator> generators = new HashSet<>(numberOfColumns);
while (generators.size() < numberOfColumns) {
generators.add(ColumnGenerator.values()[RANDOM.nextInt(ColumnGenerator.values().length)]);
}
// randomly generate rows with the selected columns
List<Row> rows = new ArrayList<>();
for (int i = 0; i < RANDOM.nextInt(10) + 1; i++) {
Row row = new Row();
for (ColumnGenerator generator : generators) {
row.setValue(generator.name(), generator.randomize());
}
rows.add(row);
}
return new DataSet(generators.stream().map(Enum::name).collect(Collectors.toList()), rows);
}
// column generator
private enum ColumnGenerator {
DATE_OF_BIRTH {
@Override
public Object randomize() {
return BIRTYHDAYS[RANDOM.nextInt(BIRTYHDAYS.length)];
}
},
NAME {
@Override
public Object randomize() {
return NAMES[RANDOM.nextInt(NAMES.length)];
}
},
SURNAME {
@Override
public Object randomize() {
return SURNNAMES[RANDOM.nextInt(SURNNAMES.length)];
}
},
NICK_NAME {
@Override
public Object randomize() {
return NICKNAMES[RANDOM.nextInt(NICKNAMES.length)];
}
},
SEX {
@Override
public Object randomize() {
return RANDOM.nextBoolean() ? "F" : "M";
}
},
NATIONALITY {
@Override
public Object randomize() {
return NATIONALITIES[RANDOM.nextInt(NATIONALITIES.length)];
}
},
STATUS {
@Override
public Object randomize() {
return RANDOM.nextBoolean() ? "ONLINE" : "OFFLINE";
}
},
LICENSE_ACTIVE {
@Override
public Object randomize() {
return RANDOM.nextBoolean();
}
},
REPUTATION {
@Override
public Object randomize() {
return RANDOM.nextInt(1000);
}
};
public abstract Object randomize();
}
// simulation of data set object
public class DataSet {
private List<String> columns;
private List<Row> rows;
public DataSet(List<String> columns, List<Row> rows) {
this.columns = columns;
this.rows = rows;
}
public List<String> getColumns() {
return columns;
}
public List<Row> getRows() {
return rows;
}
}
// simulation of row object
public class Row {
private Map<String, Object> values = new HashMap<>();
public Object getValue(String column) {
return values.get(column);
}
public void setValue(String column, Object value) {
values.put(column, value);
}
}
}
结果: