3

我正在使用 Vaadin 进行产品开发。到目前为止,我们使用的是 Vaadin 7。

我们需要根据规则以表格形式和图表形式显示数据。用户也可以随时从配置选项中修改规则。所以不能用固定数量的变量定义bean。

在 Vaadin 7 中,我们可以使用容器创建空容器对象并添加列运行时间。但是据我所知,在 Vaadin 8 中不允许。

使用 7 兼容包,它将适用于 7 网格。

scala中的示例我从数据框创建容器

def getcontainer(dataframe: Dataset[Row]): IndexedContainer = {

    var container = new IndexedContainer();
/*get number column need to create in data frame. it will be change on evertime based on output dataframe.*/
    val columns = dataframe.columns

    for (x <- columns) {
      try {
        container.addContainerProperty(x, classOf[Any], "");
      } catch {
        case e: NullPointerException =>
          e.printStackTrace()
          println("Column name must not be null")
        case e: Exception =>
          e.printStackTrace()
          println("Some went wrong with dataframe")

      }
    }
return container
}

那么我怎样才能在 Vaadin 8 中做同样的事情呢?

任何帮助将不胜感激。

4

1 回答 1

7

您可以使用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);
        }
    }
}

结果:

网格动态列

于 2017-08-02T13:55:18.107 回答