@Subscribe(“createDataGridBtn”)
protected void onCreateDataGridBtnClick(ClickEvent event) {
box.removeAll();
Integer rowCount = columnCountField.getValue(); // Reusing the field for row count
if (rowCount == null || rowCount < 1 || rowCount > 10) {
notifications.create("Row count must be between 1 and 10")
.withType(Notifications.Type.WARNING)
.withCloseable(false)
.show();
return;
}
KeyValueCollectionContainer container = createDataContainer(rowCount);
DataGrid<KeyValueEntity> dataGrid = createDataGrid(container); // Now passing container directly
box.add(dataGrid);
}
// Create editable DataGrid
protected DataGrid<KeyValueEntity> createDataGrid(KeyValueCollectionContainer container) {
DataGrid<KeyValueEntity> dataGrid = uiComponents.create(DataGrid.class);
dataGrid.setWidthFull();
dataGrid.setMinHeight("12em");
// Add editable columns (each column will contain an input field)
dataGrid.addColumn("prop1", container.getEntityMetaClass().getPropertyPath("prop1"))
.setHeader("Prop1")
.setEditorComponent(createEditorField()); // Set the input field for editing
dataGrid.addColumn("prop2", container.getEntityMetaClass().getPropertyPath("prop2"))
.setHeader("Prop2")
.setEditorComponent(createEditorField()); // Set the input field for editing
dataGrid.setItems(new ContainerDataGridItems(container));
// Optional: Handle saving data after editing
// dataGrid.addEditorSaveListener(event → {
// KeyValueEntity entity = event.getItem();
// saveData(entity); // Implement saving logic here
// });
return dataGrid;
}
// Create an editor component (TextField in this case)
protected TextField createEditorField() {
TextField textField = uiComponents.create(TextField.class);
textField.setWidthFull();
return textField;
}
protected KeyValueCollectionContainer createDataContainer(Integer rowCount) {
KeyValueCollectionContainer container = dataComponents.createKeyValueCollectionContainer();
// Create columns with properties for each row
container.addProperty("prop1", String.class);
container.addProperty("prop2", String.class);
container.setItems(loadData(rowCount)); // Load data for rows
return container;
}
protected Collection<KeyValueEntity> loadData(Integer rowCount) {
Collection<KeyValueEntity> list = new ArrayList<>();
// Create rows with empty or default values
for (int row = 0; row < rowCount; row++) {
KeyValueEntity entity = new KeyValueEntity();
entity.setValue("prop1", ""); // Empty value for prop1
entity.setValue("prop2", ""); // Empty value for prop2
list.add(entity);
}
return list;
}
Cause I wanna create the rows as I want and the rows is input fields for user putting the value. But there is no field for all the rows for user to put the data