SAP CAP项目:在植物创建/修改界面实现浇水事件的多选值帮助
SAP CAP项目:在植物创建/修改界面实现浇水事件的多选值帮助
嘿,这个需求我太熟悉了!多对多关联场景下的多选值帮助,在SAP CAP搭配Fiori UI的架构里其实配置路径很清晰,我结合你现有的代码给你一步步拆解:
第一步:调整服务层配置,支持中间表的Draft与操作权限
你的PlantWateringAssociation是连接Plants和WateringEvent的多对多中间表,因为主实体Plants已经启用了Draft,中间表也得同步开启Draft支持,不然编辑Plant草稿时没法关联浇水事件。同时要确保它有可插入、可更新、可删除的权限:
修改farmbot-service.cds里的PlantWateringAssociation定义:
entity PlantWateringAssociation @( Capabilities : { InsertRestrictions : { Insertable : true }, UpdateRestrictions : { Updatable : true }, DeleteRestrictions : { Deletable : true } } ) as SELECT from my.PlantWateringAssociation; // 给中间表启用Draft,和主实体联动 annotate PlantWateringAssociation with @odata.draft.enabled;
第二步:配置UI注解,添加多选值帮助字段
接下来要在Plant的创建/修改界面里,给wateringEvents关联配置多选值帮助,并且在UI上展示这个字段:
1. 给wateringEvents关联添加值帮助配置
在app/plants/annotations.cds里,给Plants的wateringEvents字段添加ValueList注解,明确指向WateringEvent实体,并开启多选:
annotate service.Plants with { wateringEvents @( Common.ValueList : { $Type : 'Common.ValueListType', CollectionPath : 'WateringEvent', // 值帮助的数据源是WateringEvent实体 Parameters : [ { $Type : 'Common.ValueListParameterOut', LocalDataProperty : wateringEvents.wateringEvent_ID, ValueListProperty : 'ID' // 关联字段映射 } ], MultiSelect : true // 关键配置:允许用户多选浇水事件 }, // 配置显示的文本,让用户能直观看到已选的浇水事件类型 Common.Text : { $value : wateringEvents.wateringEvent.WateringType, @UI.TextArrangement : #TextOnly } ); }
2. 在UI界面里添加这个多选字段
你可以选择在现有字段组里添加,或者新建一个专门的Facet来放浇水事件关联。我推荐新建一个独立的Facet,界面更清晰:
annotate service.Plants with @( // 追加新的Facet到现有列表 UI.Facets : [ { $Type : 'UI.ReferenceFacet', ID : 'GeneratedFacet1', Label : '{i18n>GeneralInformation}', Target: '@UI.FieldGroup#GeneratedGroup1', }, { $Type : 'UI.ReferenceFacet', ID : 'WateringEventsFacet', Label : '{i18n>WateringEvents}', Target: '@UI.FieldGroup#WateringEventsGroup' } ], // 新建字段组承载多选字段 UI.FieldGroup #WateringEventsGroup: { $Type: 'UI.FieldGroupType', Data : [ { $Type: 'UI.DataFieldForCollection', Value: wateringEvents, Label: '{i18n>AssociatedWateringEvents}', @Common.ValueListDisplay: #Modal // 值帮助以弹窗形式打开 } ] } );
3. 优化浇水事件值帮助的显示列表
为了让用户在值帮助弹窗里能清晰选择,给WateringEvent添加UI LineItem注解:
annotate service.WateringEvent with @( UI: { LineItem: [ {Value: ID, Label: '{i18n>WateringEventID}'}, {Value: WateringType, Label: '{i18n>Type}'}, {Value: Occurrence, Label: '{i18n>Occurrence}'}, {Value: createdAt, Label: '{i18n>CreatedAt}'} ], Identification: [{Value: WateringType}] // 值帮助里的标题显示浇水类型 } );
第三步:测试验证
部署更新后的服务和UI应用,打开Plant的创建/修改页面:
- 你会看到新增的「浇水事件」Facet,点击字段旁边的值帮助按钮
- 弹窗里会列出所有可用的浇水事件,你可以多选
- 保存Plant之后,系统会自动在
PlantWateringAssociation表中创建对应的关联条目,关联你选择的浇水事件和当前Plant
注意事项
- 一定要确保
PlantWateringAssociation启用Draft,否则在编辑Plant草稿时,新增的关联不会被同步到草稿中,保存时会丢失 - 如果你的Fiori应用是基于CDS自动生成的,这些注解会自动生效,不需要额外修改前端代码
备注:内容来源于stack exchange,提问作者youssef khemiri
相关产品推荐
相关产品推荐

