Fieldgroup binder with collections

Hi,

I was wondering if there was any possiblity to bind collection properties like in Spring MVC taglibs.
For instance I would have 2 classes:

public class One {

    private String property;
    private List<Many> collection;
    // getters setters
}

public class Many {
    private String name;
    private String address;
    // getters setters
}

With Spring I would be able to bind things like this:

<form:input path="collection[${status.index}] .name" /> Is there a possibility to do the same with vaadin ? Something like:

fieldGroup.bind(textField, "collection["+index+"] .name"); Thanks,

Thomas

Unfortunately not, no. You will have to manually loop through the collection and bind the fields you’ll need.

Thanks for your answer.
So it means that to achieve such a thing I would need one different fieldgroup per item of my collection right ?

More or less something like:

[code]
public class MyForm extends VerticalLayout {

public MyForm(){

    One one = new One();

    TextField propertyField = new TextField();
    BeanFieldGroup<One> fieldGroupOne = new BeanFieldGroup<One>(One.class);
    fieldGroupOne.bind(propertyField, "property");

    fieldGroupOne.setItemDataSource(one);
    // add components to layout

    for (Many many : one.getCollection()){
        BeanFieldGroup<Many> fieldGroupMany = new BeanFieldGroup<Many>(Many.class);
        TextField nameField = new TextField();
        TextField addressField = new TextField();

        fieldGroupMany.bind(nameField, "name");
        fieldGroupMany.bind(addressField, "address");

        fieldGroupMany.setItemDataSource(many);

        // add components to layout
    }
}

}
[/code]There is no way to do it with only one field group ?

Thanks a lot!

Thomas

Technically, you don’t need many fielgroups if you combine all properties under one Item. This means that you can’t use BeanFieldGroup (or BeanItem for that matter), you need to change these to the base FieldGroup and e.g. a PropertySetItem. You also need to map each property to the item manually before you map them to fields, using e.g. MethodProperty. All in all quite a messy solution.

In your case I might just do as you suggested; creating many FieldGroups and manging the commits and discards myself. Makes for cleaner code, that’s for sure.

Thanks!

Do you think this could be developed at some point in Vaadin binder?
Shall I create a ticket or create a support request for it?

This would be very interesting for us to have this kind of things. For one project I need to manage a big form with lots of differents fields mapped to collections from my main object and keeping 100 different fieldgroups is very difficult :slight_smile: Using PropertySetItem seems also quite difficult to maintain.

Thomas

It’s not a bad idea. Yeah, please create an enhancement request at
dev.vaadin.com
and it’ll be discussed in a future roadmap meeting :slight_smile:

Done:


http://dev.vaadin.com/ticket/14057

Thanks!

Hi,

​Is there any development on this subject?

This is another old thread but I am new to Vaadin Framework, trying to understand Vaadin Framework’s logic and this subject is the closest one to my situation.

I’m currently trying to bind a property which contains contact information in an
ArrayList
(
ArrayList
) and show the data in a custom layout (address, telephone, email, etc.) for each one in a
VerticalLayout
.

Should I “create many FieldGroups and mange the commits and discards myself” as Thomas Mattsson says? or is there a new feature which does the some job?

Thank you…

Hi there :slight_smile:

Take a look at the last comment in the created ticket, where Michael suggest using beanItem.addItemProperty()

Hi Thomas.
Thank you for your reply. :slight_smile:

Yes. I took a look at the comment before my post but that example doesn’t fit my need, at least at first sight.

Think about a desktop email client application such as Outlook, Apple Mail App, Thunderbird, etc.

When you click
Inbox
, you see received messages. When you click
Sent
, you see sent messages. Simple. If we don’t talk about other details the applications have simply two
VerticalLayout
s in a
HorizontalLayout
. One for “master” and the other one for “detail”. I am trying to design similar layout for a client detail view.

Actually I hope Vaadin Framework has something similar to C#/XAML logic: Data inheritance, event tunneling/bubbling. :slight_smile:

Well… This is what I am try to achieve:


ContactInformation.java

[code]
public class ContactInformation {

private String address;
private String phone;

public ContactInformation() {
    this.address = null;
    this.phone = null;
}

getters...
setters...

}
[/code]
Client.java

[code]
public class Client {

private String name;
private ArrayList<ContactInformation> contactInformation;

public Client() {
    this.name = null;
    this.contactInformation = new ArrayList<>();
}

getters...
setters...

}
[/code]I think the code pieces above are enough to explain the basic data structure.


ClientDetail.java

[code]
public class ClientView extends Window {

private final BeanFieldGroup<Client> bfgClient = new BeanFieldGroup<>(Client.class);
private final TextField txflName = new TextField();
private final ContactInformationView csflContactInformation = new ContactInformationView();
...


public ClientView() {
    this.bfgClient.bind(this.txflName, "name");
    this.bfgClient.bind(this.csflContactInformation, "ContactInformation");
    ...
}

[/code]There is a

ContactInformationView

which extends
CustomField
. The
ArrayList
which contains contact information items is passed to

ContactInformationView

by
setPropertyDataSource
overrider’s parameter (newDataSource). Contact information view is created for each item successfuly but then what? :slight_smile: I am stuck here.

Should
BeanItem
(in the example of the link provided above) be instantiated for each item in the
ArrayList
and bound to the fields in

ContactInformationView

separately? If so, what about
commit()
method? Will

bfgClient

be aware of the data changes in

ContactInformationView

?

This is the simplest part of whole application. Changing design could solve this contact information problem but there are much more complex data structures must be shown in this logic.

Hi,

See ElementCollectionField in Viritin. I think that might be exactly what you are looking for.

The tests in the project edit similar
list of addresses
. The
EditPerson
test shows how to actually use it.

cheers,
matti

@Matti Tahvonen, your given links are not accessible anymore. Can you fix them?

Here:

https://github.com/viritin/viritin/blob/master/src/test/java/org/vaadin/viritin/it/EditPersonV8.java

The V8 of the element collection field is still not in a perfect shape, but you could try to use it already and fill enhancement issues if something don’t work.

The old V7 version is more robust and available in the compatibility (“v7”) package. Its example code is in the v7 branch: https://github.com/viritin/viritin/blob/vaadin7/src/test/java/org/vaadin/viritin/it/EditPerson.java

cheers,
matti