nick
nick

Reputation: 249

Vaadin Flow grid add an empty row

How can someone add a new empty line (perhaps at the bottom or at the top of the grid) programmatically. At a later state the user enters data, or after some actions save all the rows perhaps in the database.

Upvotes: 1

Views: 1133

Answers (2)

Chương Lê
Chương Lê

Reputation: 1

  1. You using ListDataProvider set fof dataprovider of grid.

  2. Bean object Override hashCode method. for Example :

    @Transient private long sId;

    public ConstructorMethod () { sId = System.currentTimeMillis(); }

    @Override public int hashCode() { if (isNew()) return (int) (super.hashCode()+sId); else return super.hashCode(); }

  3. New a bean object and add to ListDataProvider of grid

     BeanClass item = new BeanClass();
    (ListDataProvider<BeanClass>)grid.getDataProvider().getItems().add(item);
    (ListDataProvider<BeanClass>)grid.getDataProvider().refreshAll();
    

Upvotes: 0

Tatu Lund
Tatu Lund

Reputation: 10623

Technically it should be rather simple, especially if you use ListDataProvider for the Grid, here simplified example:

Grid<Bean> grid = new Grid<>(Bean.class);
ListDataProvider<Bean> dp = new ListDataProvider<>(getData());
grid.setDataProvider(dp);
add(grid);

Button button = new Button("Add row");
button.addClickListener(event -> {
    Bean bean = new Bean();
    dp.getItems().add(bean);
    dp.refreshAll();
    grid.getEditor().editItem(bean);       
});
add(button);

Upvotes: 2

Related Questions