Cuba平台:如何使用ValueDataSource结合Label显示标量值?
Got it, let's walk through how to display scalar values (like aggregated order totals) using a ValueDataSource paired with a Label component in CUBA. Here's a practical example focused on showing total order amounts per customer:
Step 1: Configure the Value Collection Data Source
First, set up the data source in your screen's XML dsContext. This will handle fetching the aggregated data from your database:
<dsContext> <valueCollectionDatasource id="salesDs"> <query> <![CDATA[select o.customer, sum(o.amount) from demo$Order o group by o.customer]]> </query> <properties> <property class="com.company.demo.entity.Customer" name="customer"/> <property datatype="decimal" name="sum"/> </properties> </valueCollectionDatasource> </dsContext>
A quick breakdown of the key parts:
- We use
valueCollectionDatasourcehere because we're fetching a collection of grouped results (one entry per customer). If you need a single scalar value (like the total of all orders across all customers), tweak the query toselect sum(o.amount) from demo$Order oand only define thesumproperty in the<properties>section. - The query uses
sum(o.amount)to calculate each customer's total order value, grouped by the customer entity. - The
<properties>block explicitly defines the data types of the returned values:customeris a reference to your Customer entity, whilesumis a decimal scalar representing the total amount.
Step 2: Bind the Data Source to a Label
To display the scalar value in a Label, add the component to your screen layout and bind it to the data source's property. For example, if you have a table listing customers and want to show the selected customer's total order amount in a Label, use this configuration:
<Label id="totalAmountLabel" datasource="salesDs" property="sum" caption="Total Order Amount"/>
If you're working with a single global total (not per customer), switch to using a valueDatasource instead of valueCollectionDatasource, then bind the Label directly to its single property.
内容的提问来源于stack exchange,提问作者Matthias Hamann

