Hello,
I am trying to achieve something and i spent few hours before coming with the solution.
There is a grid list of item which I need to show few columns only and when user selects an item ( Grid.SingleSelection), i show a form with all the columns from that particular row.
The user then updates the row with the values and when save is clicked, i need to update the grid as well. To update the grid, i was doing.
// Adds the updated item to the end of the list and it is
// not what i want.
// container.removeItem(originalBean);
// container.addItem(updated);
The above solution works but it only updates the grid at the end of the list.
Then i read, discussions:
https://vaadin.com/forum/#!/thread/408555/408554
&
https://vaadin.com/forum#!/thread/9319379
.
i realized that i had to get the property of the Grid to make it work.
Then i did:
// Does what i want but just wondering if there is any other
// solution.. better
Item item = grid.getContainerDataSource().getItem(originalBean);
item.getItemProperty("age").setValue(updated.getAge());
item.getItemProperty("firstName").setValue(updated.getFirstName());
item.getItemProperty("lastName").setValue(updated.getLastName());
It works like a charm…!!
My only concern now is there another better way of doing this, without getting the Properties and updating the values manually. [ no idea but as suggested in above discussion call: fireStateChanged?? couldn’t get my head wrapped around that…]
Could someone please advice and guide me on how to do it better. In the mean while i can deal with this okay solution.
Below is my sample code… of what i was trying::
public class TestingForms extends CssLayout {
private static final long serialVersionUID = -7255586307801066971L;
private List<Person> persons = new ArrayList<>();
private Grid grid = new Grid();
private BeanFieldGroup<Person> personBeanFieldGroup = new BeanFieldGroup<>(Person.class);
private FormLayout personForm = new FormLayout();
private TextField firstName = new TextField("First Name: ");
private TextField lastName = new TextField("Last Name: ");
private TextField age = new TextField("Age: ");
private Container.Indexed container;
private Person originalBean = null;
public TestingForms() {
populatePersons();
container = new BeanItemContainer<>(Person.class, persons);
createGridComponent();
addEvents();
populateFormLayout();
}
private void populateFormLayout() {
personForm.addComponent(firstName);
personForm.addComponent(lastName);
personForm.addComponent(age);
Button button = new Button("Save");
button.addClickListener(new ClickListener() {
private static final long serialVersionUID = -1621003547230015891L;
@Override
public void buttonClick(ClickEvent event) {
try {
personBeanFieldGroup.commit();
Person updated = personBeanFieldGroup.getItemDataSource().getBean();
// Adds the updated item to the end of the list and it is
// not what i want.
// container.removeItem(originalBean);
// container.addItem(updated);
// Does what i want but just wondering if there is any other
// solution.. better
Item item = grid.getContainerDataSource().getItem(originalBean);
item.getItemProperty("age").setValue(updated.getAge());
item.getItemProperty("firstName").setValue(updated.getFirstName());
item.getItemProperty("lastName").setValue(updated.getLastName());
} catch (CommitException e) {
e.printStackTrace();
}
}
});
personForm.addComponent(button);
Button clearButton = new Button("Clear");
clearButton.addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
personBeanFieldGroup.clear();
}
});
personForm.addComponent(clearButton);
}
private void addEvents() {
grid.addSelectionListener(new SelectionListener() {
private static final long serialVersionUID = -7936930180846481298L;
@Override
public void select(SelectionEvent event) {
Set<Object> selected = event.getSelected();
addComponent(personForm);
for (Iterator<Object> iterator = selected.iterator(); iterator.hasNext();) {
originalBean = (Person) iterator.next();
personBeanFieldGroup.bind(firstName, "firstName");
personBeanFieldGroup.bind(lastName, "lastName");
personBeanFieldGroup.bind(age, "age");
personBeanFieldGroup.setItemDataSource(originalBean);
}
}
});
}
private void populatePersons() {
for (int i = 0; i < 100; i++) {
Person person = new Person();
person.setAge(i);
person.setFirstName("firstName " + i);
person.setLastName("lastName " + i);
persons.add(person);
}
}
private void createGridComponent() {
grid.setImmediate(true);
grid.setContainerDataSource(container);
grid.setColumnOrder("age", "firstName", "lastName");
grid.getColumn("age").setHeaderCaption("Age");
grid.getColumn("firstName").setHeaderCaption("First name");
grid.getColumn("lastName").setHeaderCaption("Last name");
grid.setSelectionMode(SelectionMode.SINGLE);
this.addComponent(grid);
}
}