Can I create a mesh without a bean class in Vaadin 8?

I am working on product development using Vaadin. Until today, we are using Vaadin 7.

We need to display data in a tabular form, as well as a chart in accordance with the rule. Also the user can change the rule at any time from the configuration option. therefore cannot define a bean with a fixed number of variables.

In Vaadin 7, using a container, we can create an empty container object and add the column runtime. But Vaadin 8 is not allowed as far as I know.

With 7 compatible package, it will work on 7 networks.

Example in scala i created container from dataframe

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
}

      

So how can I do the same in Vaadin 8?

Any help would be appreciated.

+3


source to share


1 answer


You can add columns to the grid using options or . addColumn(propertyName)

addColumn(ValueProvider, ...)

I'm not familiar with the spark data source, so I'm not sure what an object is Row

. If it is a generated runtime object with the property names that you get from the columns of the dataset, then the first method should be a direct replacement. If it Row

looks like a key value data structure (like a map), you can use the second method.

For this exercise, I will be modeling Row

in java as a keyed data structure using HashMap

.

Let me know if I am missing something.



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);
        }
    }
}

      

Result:

Dynamic Grid Columns

+5


source







All Articles