Setting field value based on result from valueCollectionDatasource

Hi

I create valueCollectionDatasource which return sum of amount. Query below

select COALESCE(sum(c.destinationDocument.brutto),0) as settled 
from demo$amountConnector c 
where c.sourceDocument.id=:ds$xxxCreatorParamsDs.document.id

This datasource depend on other dataSource xxxCreatorParamsDs
Now I’d set result settled to field in editWindow. I try set datasource parameter for settled field.

obraz

I check datasource return correctly values. How can I set result from
sumDS datasource to column in xxxCreatorParamsDs datasource. Maybe there is other way for realize this case.

Hi Andrzej

The value datasources/data containers contain instances of KeyValueEntity which is an entity with attributes defined at runtime, in <properties> element of the datasource. So you can bind the visual components to these attributes as to regular entities/attributes.

Below is an example based on the GitHub - cuba-platform/sample-sales-cuba7: CUBA Platform Sample application project and new data containers API.

First, let’s define the value data container and the field bound to it in order-browse.xml:

<window xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd" ...>
    <data readOnly="true">
        <collection id="ordersDc" class="com.company.sales.entity.Order" view="order-with-customer">
            <loader id="ordersDl">
                <query>
                    <![CDATA[select e from sales_Order e]]>
                </query>
            </loader>
        </collection>
        <keyValueInstance id="orderAmountDc">
            <loader id="orderAmountDl">
                <query>select sum(e.quantity * e.product.price) from sales_OrderLine e where e.order = :order</query>
            </loader>
            <properties>
                <property name="amount" datatype="decimal"/>
            </properties>
        </keyValueInstance>
    </data>
    <layout expand="ordersTable"
            spacing="true">
        ...
        <groupTable ...>
            ...
            <buttonsPanel ...>
                ...
                <textField id="amountField" inputPrompt="Amount of the selected order"
                           dataContainer="orderAmountDc" property="amount"/>
            </buttonsPanel>
        </groupTable>

After that, change OrderBrowse controller as follows:

@UiController("sales_Order.browse")
@UiDescriptor("order-browse.xml")
@LookupComponent("ordersTable")
// notice there is no @LoadDataBeforeShow annotation
public class OrderBrowse extends StandardLookup<Order> {

    @Inject
    private CollectionLoader<Order> ordersDl;
    @Inject
    private KeyValueInstanceLoader orderAmountDl;

    @Subscribe
    private void onBeforeShow(BeforeShowEvent event) {
        // load orders list right before screen opening
        ordersDl.load();
    }

    @Subscribe(id = "ordersDc", target = Target.DATA_CONTAINER)
    private void onOrdersDcItemChange(InstanceContainer.ItemChangeEvent<Order> event) {
        // load order amount each time an order is selected 
        orderAmountDl.setParameter("order", event.getItem());
        orderAmountDl.load();
    }
}

Demo:
valuecontainer-bound-to-another-container

As for datasources, there is no direct counterpart to keyValueInstance container, so you should use ValueCollectionDatasource and set the first loaded item in it after refreshing.