Help to choose address with partial address input

Hello!

We have field “full address” on our form, and our goal is to provide next functionality to our users:
when user inserts partial address, for example “london, baker street”, system advices:

  • london, baker street, 10
  • london, baker street, 11
  • london, baker street, 12

in the list “below” full address field, and user could choose one of these variants.

We know some services, where we can get this information raw, but we didn’t know best way to implement it in CUBA.
May be some kind of LookupField with custom options, which are filled with HTTP response from google maps api (e.g.).

Thank you in advance!

address_search_sample

Hi Max,

for now CUBA does not have completely async search but you can use SearchPickerField component that can search for suggestions by Enter key press: https://doc.cuba-platform.com/manual-6.2/gui_SearchPickerField.html

We are planning to add implementation of SuggestionField (with async search by delay and typing) for web client in one of the next minor releases, probably in Release 6.4.

Hello Yuriy!
Thank you for your answer. we’ll figure out how to “work around”, and will be waiting for further releases.

Yuriy, it’s required to set collectionDatasource, but we have no corresponding object in database.
We need to query to google API. Could you provide example?

Thanks!

In your case you can use non-persistent entities (inherited from AbstractNotPersistentEntity) and Custom collection datasource implementation that loads data from some web service. Example of Custom datasource implementation you can find here: https://doc.cuba-platform.com/manual-6.2/datasource_custom_class.html

Yuriy, could you describe a bit more?

We tried next:

  1. Create non-persistent entity AddressOffers:

@MetaClass(name = "cm$AddressOffers")
public class AddressOffers extends AbstractNotPersistentEntity {
    @MetaProperty
    protected String fullAddress;

    @MetaProperty
    protected String region;

    @MetaProperty
    protected String city;

    @MetaProperty
    protected String street;
}
  1. Create custom Datasource class

public class TestSearchDatasource extends CustomCollectionDatasource<AddressOffers, UUID> {

    @Override
    protected Collection<AddressOffers> getEntities(Map<String, Object> params) {

        ArrayList<AddressOffers> testCol = new ArrayList<AddressOffers>();

        AddressOffers entityAO;

        entityAO = new AddressOffers();
        entityAO.setValue("fullAddress", "Address street 10");
        testCol.add(entityAO);

        entityAO = new AddressOffers();
        entityAO.setValue("fullAddress", "Address street 11");
        testCol.add(entityAO);

        entityAO = new AddressOffers();
        entityAO.setValue("fullAddress", "Address street 12");
        testCol.add(entityAO);

        return testCol;
    }
}
  1. Add datasource to SearchPickerField on our screen

@Inject
protected SearchPickerField testSearch;

.@Override
    public void init(Map<String, Object> params) {
       TestSearchDatasource ds = new DsBuilder().setJavaClass(AddressOffers.class).buildCollectionDatasource(TestSearchDatasource.class);
        testSearch.setOptionsDatasource(ds);
    }

And it looks like it works
Is it way to invoke search, not just by “Enter” press, but with some Action (buttom click)?
For example - we’ve choosed some value to search field, and then we click button, and search field get’s focused, and Enter press is emulated, and list of options is occured below as usually.

Is it possible?

Thanks in advance!

Also a little question — how to set initial value to search field, on form open?

This code below lefts field empty :frowning:


testSearch.setValue(  <u>[some AddressOffers  object]</u>  );

Thanks!

Found answer for previous question:

testSearch.setNullOption( [my non-persistence object] );

And it works!

Hi,
it is not the correct way to set initial value, since setNullOption sets only representation of NULL value for field and getValue will return null.

I recommend to set initial value to your entity in editor from initNewItem (https://doc.cuba-platform.com/manual-6.2/init_values_in_initNewItem.html) method, or if field is not bound to datasource then you can set initial value from init, postInit or ready methods using setValue method of a field.