tyl / field-binder

Vaadin FieldBinder Add-on
Apache License 2.0
2 stars 1 forks source link

How to use a combobox inside ListSelection table #2

Open mortoza opened 9 years ago

mortoza commented 9 years ago

Hi Evacchi Your add-on is amazing! It will help a lot! I have a question-

How can I add a combobox editor inside the list selection table (e.g. address table in the example) while the combobox is populated with e.g. country names. Is it possible while I am using filed-binder?

I do this when I am using a table, but wondering how I can perform the same in Field-Binder;

table.setTableFieldFactory(new DefaultFieldFactory() { @Override public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) { if ("countries".equals(propertyId)) { //for the ComboBox List cList = facade6.findAll(); BeanItemContainer cContainer = new BeanItemContainer<>(com.myentity.Country.class, cList); countries = new ComboBox(); countries.setContainerDataSource(cContainer);

evacchi commented 9 years ago

the detail table is a wrapped com.vaadin.Table so you should be able to do

final BeanTable<Country> beanTable = new BeanTable<>(Country.class, yourContainer);
beanTable.getTable().setTableFieldFactory(new DefaultFieldFactory() {
   // overrides here
});

You can also extend the ListContainerTableBehavior and use it with BeanTable.getNavigation().withBehavior(new MyCustomBehavior())

PS: I would advise to use Viritin's ListContainer instead of the Vaadin's BeanContainer. Viritin is already a dependency for FieldBinder, so you should be good to go already

mortoza commented 9 years ago

Hi Evacchi I have added the following codes inside init, anything wrong here?

    final BeanTable<Address2> beanTable = new BeanTable<>(Address2.class, container);
    beanTable.getTable().setTableFieldFactory(new DefaultFieldFactory() {
        @Override
        public Field createField(Container container, Object itemId,
                Object propertyId, Component uiContext) {
            if ("countries".equals(propertyId)) {
                List<Country> custList = facadecountry.findAll();
                BeanItemContainer<Country> custContainer = new BeanItemContainer<>(Country.class, custList);
                countries = new ComboBox();
                countries.setContainerDataSource(custContainer);

                return countries;
            }

            return super.createField(container, itemId, propertyId, uiContext);
        }

    });
evacchi commented 9 years ago

looks fine to me

mortoza commented 9 years ago

Thanks but I am wondering where is the gap. I don;t see any combobox in the address detail table. How the BeanTable linking to addressList?

evacchi commented 9 years ago

Oh, I see. You are not supposed to instantiate a new BeanTable, then. You should rather do:

FieldBinder<...> binder = new FieldBinder(...);

then, you request a ListTable<Address2> from the field binder, which will be automatically wired as a "detail" of the FieldBinder "master"; here's how:

// just assign the return value of binder.buildListOf(...)
ListTable<Address2> listTable = binder.buildListOf(Address2.class, "addressList");

now you can customize the field factory as follows:

listTable.getTable().setTableFieldFactory(...)

This is a bit experimental, though, you might not get what you expect. See code for ListContainerTableBehavior for reference. You might want to subclass/copy/extend that class and then set it manually with:

listTable.withBehavior(new MyCustomizedBehavior(...));

instead of using listTable.withDefaultBehavior()

V1.2 should simplify this and bring more features.

Hope this helps!

mortoza commented 9 years ago

Thank you, it is really helpful. I shall apply this thoughts and let you know. When are you planning to release version 1.2?

evacchi commented 9 years ago

soon. I think it will be a matter of a few days. But feel free to clone the repository and mvn install (checkout the 1.2-SNAPSHOT branch first) to play with it already