在 CAP 中显示与 SAP Fiori 注释的“一对多”关联

问题描述 投票:0回答:2

我尝试用 SAP cap 做一个数据库项目。我想显示与父实体关联的所有实体。如果协会是一对一的,那就可以正常工作。但如果是一对多,则不会显示任何内容。

--我有以下架构定义:

entity Company{
key ID : Integer @title: 'Company ID';
cName : String @title: 'Company name';
pers: Association to many Person on pers.comp = $self; //association to one works
}


entity Person{
    key ID : Integer @title: 'ID';
    pName: String @titel: 'Name';
    comp: Association to Company;
}

--以及以下 fiori 注释:

annotate Admin.Company with @(
UI.LineItem:[
    {$Type: 'UI.DataField', Value: cName},

],
UI.HeaderInfo:{
    TypeName: 'Company',
TypeNamePlural: 'Companys'
},

);

//Details view:
annotate Admin.Company with @(
UI: {
        HeaderInfo: {
    Description: {Value: ID}
    },
        HeaderFacets: [
        {$Type: 'UI.ReferenceFacet', Target: '@UI.FieldGroup#Comp'},
        ],
        Facets: [
             {$Type: 'UI.ReferenceFacet', Target: '@UI.FieldGroup#Pers'}
        ],
        FieldGroup#Comp: {
            Data: [
                {Value: cName},
            ]
        },
        FieldGroup#Pers: {
            Data: [
                {Value: pers.pName},
            ]
        },
    });

管理服务只是给定实体的投影。
有谁知道我必须更改什么才能让

pers.Name
显示在列表或类似的内容中?
正如已经提到的,如果我将模式定义“与许多的关联”更改为“与一个的关联”,它就会起作用。但我希望它有多个关联实体。

sap-fiori sap-cap
2个回答
0
投票

如果您使用的是 Fiori Elements List Report Object Page 模板,我认为您只能使用 1:1 关联。这是平面图的预期设计(列表→详细信息)。 Fiori Elements 如何知道“许多”人员项目中的哪一个应绑定到一个详细信息视图。

但是,您可以并且可能想要做的是将详细信息视图绑定到所选公司项目并创建字段组。您可以在其中显示引用不同实体的表。因此,您保持当前的

schema.cds
,但更改您的
annotation.cds
:

annotate Admin.Company with @(
  UI: {
    // columns of the list view
    LineItem:[
      {$Type: 'UI.DataField', Value: cName},
    ],
    // title (singular/plural) of the list view and description of the detail view
    HeaderInfo:{
      TypeName: 'Company',
      TypeNamePlural: 'Companies',
      Description: {Value: ID}
    },
    // facet for the person table
    HeaderFacets: [
      {$Type: 'UI.ReferenceFacet', Target: 'Person/@UI.LineItem'}
    ]

);

annotate Admin.Person with @(
  // person table (only displayed in detail view) with columns
  UI.LineItem: [
    {$Type: 'UI.DataField', Value: 'comp/cName'},
    {$Type: 'UI.DataField', Value: 'pName'}
  ]);

上面的代码可能无法立即运行,因为我没有测试它。但我使用 Northwind OData V2 服务创建了一个工作示例,使用列表视图的客户实体和详细视图的表中的订单实体。您可以通过使用 OData 服务 URL

https://services.odata.org/Northwind/Northwind.svc/
和主实体
Customer
运行 Fiori 列表报表对象页面向导来重新创建示例。项目生成完成后,您可以将
annotation.xml
的内容替换为以下内容:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/Common.xml">
        <edmx:Include Namespace="com.sap.vocabularies.Common.v1" Alias="Common" />
    </edmx:Reference>
    <edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/UI.xml">
        <edmx:Include Namespace="com.sap.vocabularies.UI.v1" Alias="UI" />
    </edmx:Reference>
    <edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/Communication.xml">
        <edmx:Include Namespace="com.sap.vocabularies.Communication.v1" Alias="Communication" />
    </edmx:Reference>
    <edmx:Reference Uri="/Northwind/Northwind.svc/$metadata">
        <edmx:Include Namespace="NorthwindModel" />
        <edmx:Include Namespace="ODataWebV3.Northwind.Model" />
    </edmx:Reference>
    <edmx:DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="local">
            <Annotations Target="NorthwindModel.Customer">
                <Annotation Term="UI.LineItem">
                    <Collection>
                        <Record Type="UI.DataField">
                            <PropertyValue Property="Value" Path="CompanyName" />
                        </Record>
                    </Collection>
                </Annotation>
                <Annotation Term="UI.Facets">
                    <Collection>
                        <Record Type="UI.ReferenceFacet">
                            <PropertyValue Property="Target" AnnotationPath="Orders/@UI.LineItem" />
                        </Record>
                    </Collection>
                </Annotation>
            </Annotations>
            <Annotations Target="NorthwindModel.Order">
                <Annotation Term="UI.LineItem">
                    <Collection>
                        <Record Type="UI.DataField">
                            <PropertyValue Property="Value" Path="OrderID" />
                        </Record>
                        <Record Type="UI.DataField">
                            <PropertyValue Property="Value" Path="Customer/CompanyName" />
                        </Record>
                    </Collection>
                </Annotation>
            </Annotations>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

我希望这能解决您的问题。如果您有任何疑问,请随时询问。


0
投票

你可以尝试使用Composition代替Association吗?

在您的示例中,要显示 Person 表,您需要添加

Company.pers
LineItem
注释:

annotate Admin.Company.pers with @UI: {LineItem #Address: [{
  Value         : pName,
}, ], };

如果您尝试添加此内容,您应该在您使用的编辑器中看到以下错误消息:

Term 'UI.LineItem' cannot be applied for target kind 'NavigationProperty,  Property,  Collection'. It can be applied to 'EntityType'
。您可以使用 Composition 来解决此错误。

您是否尝试过使用 Fiori 页面编辑器(作为 Fiori Tools 的一部分)?您可以在那里生成这些注释。默认情况下,它是 Business Application Studio 的一部分。您还可以将其本地添加到 VS Code。 在this链接,您会找到一个很好的教程,描述如何使用它以及它是如何工作的。

© www.soinside.com 2019 - 2024. All rights reserved.