You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cuba平台:如何使用ValueDataSource结合Label显示标量值?

使用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 valueCollectionDatasource here 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 to select sum(o.amount) from demo$Order o and only define the sum property 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: customer is a reference to your Customer entity, while sum is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:26:08