Cuba平台LookupPickerField联动加载数据失败求助
Let's walk through the issues in your code and fix the dependent picker field problem step by step.
1. Critical Query Parameter Mistake
The most obvious error is in your cardSubTypeDs query. You're referencing the second picker's ID instead of the first one:
<!-- Wrong parameter: uses cardSubTypePicker instead of cardTypePicker --> <query> <![CDATA[select s from demo$CardSubType s where s.cardType.id = :component$cardSubTypePicker.id]]> </query>
Since cardSubTypePicker is empty initially (and stays empty until data loads), this query will always return no results. You need to point it to the first picker:
<!-- Corrected query --> <collectionDatasource id="cardSubTypeDs" class="com.tkbbank.client.entity.CardSubType" view="_minimal"> <query> <![CDATA[select s from demo$CardSubType s where s.cardType.id = :component$cardTypePicker.id]]> </query> </collectionDatasource>
2. Fix the View for CardSubType
Notice I changed the view from _local to _minimal in the above code. Here's why:
- The
_localview forStandardEntityonly includes system fields (likeid,createTs) but does not load your business fieldsubtype. - Your
CardSubTypeusessubtypein its@NamePattern, so the lookup picker needs this field to display options. Without it, even if data is loaded, the picker will appear empty.
If _minimal doesn't include subtype (depends on your entity setup), create a custom view:
@View(name = "cardSubType-view", properties = {"id", "subtype", "cardType"}) @Entity(name = "demo$CardSubType") public class CardSubType extends StandardEntity { // ... your existing code }
Then use it in the datasource:
<collectionDatasource id="cardSubTypeDs" class="com.tkbbank.client.entity.CardSubType" view="cardSubType-view">
3. Optimize the Value Change Listener
Your current listener refreshes the datasource, but it's good practice to clear the second picker's value when the first picker changes (to avoid invalid selections):
@Override public void init(Map<String, Object> params) { CardItem cardItem = metadata.create(CardItem.class); cardItemDs.setItem(cardItem); cardTypePicker.addValueChangeListener(e -> { // Clear the subtype picker to remove old invalid values cardSubTypePicker.setValue(null); // Refresh the subtype datasource with the new cardType value cardSubTypeDs.refresh(); }); }
4. Additional Verification Steps
If the above fixes don't work, check these:
- Database Integrity: Ensure the
DEMO_CARD_SUB_TYPEtable has rows whereCARD_TYPE_IDmatches existing IDs inDEMO_CARD_TYPE. - CardType Datasource: Confirm
cardTypeDsis loading allCardTyperecords correctly (check if the first picker has options). - Enable SQL Logging: Turn on Hibernate SQL logging to see the actual query executed. Add this to your
persistence.xml:
This will let you verify if the correct<property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/>cardType.idparameter is being passed to thecardSubTypeDsquery.
These changes should resolve the issue where the second picker stays empty after selecting a value in the first one.
内容的提问来源于stack exchange,提问作者anon

