Reputation: 249
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
Reputation: 1
You using ListDataProvider set fof dataprovider of grid.
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(); }
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
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