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.).
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.
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?
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: Custom Implementation Classes - CUBA Platform. Developer’s Manual
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;
}
}
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.
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 (Using initNewItem Method - CUBA Platform. Developer’s Manual) 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.