创建投影层和元数据扩展

ABAP RESTful 应用程序编程模型 › 开发 › 开发应用程序 › 使用分层数据结构开发应用 › 具有草稿功能的可编辑树视图 › 可编辑树视图:概览中的开发流程 › 创建投影层和元数据扩展

创建投影层

在映射视图中,来自基本 CDS 视图实体的公开数据与层次结构实体中数据的层次结构解释相连接。

要连接层次结构,请在数据定义中添加以下注释 /DMO/C_EmployeeTP_HD:

@OData.hierarchy.recursiveHierarchy:[{ entity.name: '/DMO/I_EmployeeHN_HD' }]

将映射视图定义为根视图实体(基础 CDS 视图实体)上的投影 /DMO/R_EmployeeTP_HD:

示例代码

define view entity /DMO/C_EmployeeTP_HD
  as projection on /DMO/R_EmployeeTP_HD

定义视图实体 /DMO/R_AgencyTP_HD 的映射视图:

示例代码

define root view entity /DMO/C_AgencyTP_HD
  provider contract transactional_query
  as projection on /DMO/R_AgencyTP_HD

展开以下代码示例以查看数据定义的源代码 /DMO/C_EmployeeTP_HD。

示例代码

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Hierarchy: Draft: Employee'

@Metadata.allowExtensions: true

@OData.hierarchy.recursiveHierarchy:[{ entity.name: '/DMO/I_EmployeeHN_HD' }]

@Search.searchable: true

define view entity /DMO/C_EmployeeTP_HD
  as projection on /DMO/R_EmployeeTP_HD

{
      @ObjectModel.text.element: ['AgencyName']
  key Agency,
  key Employee,

      _Agency.Name       as AgencyName,

      @Search.defaultSearchElement: true
      @Search.fuzzinessThreshold: 0.87
      FirstName,

      @Search.defaultSearchElement: true
      @Search.fuzzinessThreshold: 0.87
      LastName,
      Salary,
      @Consumption.valueHelpDefinition: [
        { entity: {
            name: 'I_CurrencyStdVH',
            element: 'Currency'
          },
          useForValidation: true
        }
      ]
      SalaryCurrency,
      @Consumption.valueHelpDefinition: [
          {
            entity: { name: '/DMO/I_Employee_ManagerVH_HD', element: 'Employee' },
            additionalBinding: [{ element: 'Agency', localElement: 'Agency', usage: #FILTER }]
          }
        ]
      Manager,
      _Manager.FirstName as ManagerFirstName,
      _Manager.LastName  as ManagerLastName,
      SiblingOrderNumber,
      LocalLastChangedAt,

      /* Associations */
      _Agency   : redirected to parent /DMO/C_AgencyTP_HD,
      _Manager  : redirected to /DMO/C_EmployeeTP_HD,
      _Employee : redirected to /DMO/C_EmployeeTP_HD,
      _Currency

}

在行为定义中添加 projection /DMO/C_AgencyTP_HD

示例代码

projection;
strict ( 2 );
use draft;
use side effects;

不要忘记使用语法 use action changeNextSibling 重用重新排序操作。通过语法 link 重用 association_Manager 时,还必须指定 unlink 和 use association _Manager 操作。

示例代码

  use action changeNextSibling;
  use action copy;

  use association _Agency { with draft; }
  use association _Manager
  {
    with draft;
    link action linkManager;
    unlink action unlinkManager;
  }

展开以下代码示例以查看行为定义的源代码 /DMO/C_AgencyTP_HD。

示例代码

projection;
strict ( 2 );
use draft;
use side effects;

define behavior for /DMO/C_AgencyTP_HD alias Agency
use etag
{
  use create;
  use update;
  use delete;

  use action Resume;
  use action Edit;
  use action Activate;
  use action Discard;
  use action Prepare;

  use association _Employee { create; with draft; }
}

define behavior for /DMO/C_EmployeeTP_HD alias Employee
use etag
{
  use update;
  use delete;

  use action changeNextSibling;
  use action copy;

  use association _Agency { with draft; }
  use association _Manager
  {
    with draft;
    link action linkManager;
    unlink action unlinkManager;
  }
  use association _Employee { with draft; }
}

创建元数据扩展

对于映射视图,我们将为 SAP Fiori UI 上的显示创建两个元数据扩展 /DMO/C_EmployeeTP_HD 和 /DMO/C_AgencyTP_HD。

展开以下代码示例以查看元数据扩展的源代码 /DMO/C_EmployeeTP_HD。

示例代码

@Metadata.layer: #CORE

@UI:
  {
    headerInfo:
      {
        typeName: 'Employee',
        typeNamePlural: 'Employees',
        description: { type: #STANDARD, value: 'Employee' },
        title: { type: #AS_CONNECTED_FIELDS, valueQualifier: 'FullName' }
      },
    presentationVariant:
      [
        {
           sortOrder: [{ by: 'Employee', direction: #ASC }],
           visualizations: [{type: #AS_LINEITEM}]
        }
      ]
  }

annotate view /DMO/C_EmployeeTP_HD with
{

  @UI:
    {
      facet:
        [
           {
             id:            'Employee',
             purpose:       #STANDARD,
             type:          #COLLECTION,
             label:         'Employee',
             position:      10
           },
           {
             id:              'General',
             targetQualifier: 'General_FG',
             parentId:        'Employee',
             purpose:         #STANDARD,
             type:            #FIELDGROUP_REFERENCE,
             label:           'General',
             position:        10
           },
           {
             id:              'Name',
             targetQualifier: 'Name_FG',
             parentId:        'Employee',
             purpose:         #STANDARD,
             type:            #FIELDGROUP_REFERENCE,
             label:           'Name',
             position:        20
           },
           {
             id:              'Salary',
             targetQualifier: 'Salary_FG',
             parentId:        'Employee',
             purpose:         #STANDARD,
             type:            #FIELDGROUP_REFERENCE,
             label:           'Salary Data',
             position:        30
           }
         ],
      hidden: true
    }
  Agency;

  @UI: {
      lineItem:   [{ position: 20 }],
      fieldGroup: [{ position: 20, qualifier: 'General_FG' }]
    }
  Employee;

  @UI: {
      selectionField: [{ position: 30 }],
      lineItem:   [{ position: 30 }],
      identification: [{ position: 30 }],
      fieldGroup: [{ position: 30, qualifier: 'Name_FG' }],
      connectedFields: [{
          qualifier: 'FullName',
          groupLabel: 'Full Name',
          name: 'First',
          template: '{First} {Last}'
        }]
    }
  FirstName;

  @UI: {
      lineItem:   [{ position: 40 }],
      fieldGroup: [{ position: 40, qualifier: 'Name_FG' }],
      connectedFields: [{
          qualifier: 'FullName',
          name: 'Last'
        }]
    }
  LastName;

  @UI: {
      lineItem:   [{ position: 50 }],
      fieldGroup: [{ position: 50, qualifier: 'Salary_FG' }]
    }
  Salary;

  @UI: {
      lineItem:   [{ position: 70 }],
      fieldGroup: [{ position: 70, qualifier: 'General_FG', type: #AS_CONNECTED_FIELDS, valueQualifier: 'ManagerFullName' }],
      connectedFields: [{
          qualifier: 'ManagerFullName',
          groupLabel: 'Manager',
          name: 'ID',
          template: '{ID} {First} {Last}'
        }]
    }
  Manager;

  @UI: {
      connectedFields: [{
          qualifier: 'ManagerFullName',
          name: 'First'
        }]
    }
  ManagerFirstName;

  @UI: {
      connectedFields: [{
          qualifier: 'ManagerFullName',
          name: 'Last'
        }]
    }
  ManagerLastName;

  @UI: {
      lineItem:   [{ position: 80 }],
      fieldGroup: [{ position: 80, qualifier: 'General_FG' }]
    }
  SiblingOrderNumber;

  @UI.hidden: true
  LocalLastChangedAt;
}

展开以下代码示例以查看元数据扩展的源代码 /DMO/C_AgencyTP_HD。

示例代码

@Metadata.layer: #CORE

@UI: { headerInfo: {
                     typeName: 'Agency',
                     typeNamePlural: 'Agencies',
                     title: { type: #STANDARD, value: 'Name' },
                     description: { type: #STANDARD, value: 'Agency' }
                    },
       presentationVariant: [{
           sortOrder: [{ by: 'Agency', direction: #ASC }],
           visualizations: [{type: #AS_LINEITEM}]
         }]
     }

annotate view /DMO/C_AgencyTP_HD with
{
  @UI.facet: [
       {
         id:              'Agency',
         purpose:         #STANDARD,
         type:            #COLLECTION,
         label:           'Agency',
         position:        10
       },
       {
         id:              'General',
         targetQualifier: 'General_FG',
         parentId:        'Agency',
         purpose:         #STANDARD,
         type:            #FIELDGROUP_REFERENCE,
         label:           'General',
         position:        10
       },
       {
         id:              'Address',
         targetQualifier: 'Address_FG',
         parentId:        'Agency',
         purpose:         #STANDARD,
         type:            #FIELDGROUP_REFERENCE,
         label:           'Address',
         position:        20
       },
       {
         id:              'Contact',
         targetQualifier: 'Contact_FG',
         parentId:        'Agency',
         purpose:         #STANDARD,
         type:            #FIELDGROUP_REFERENCE,
         label:           'Contact Data',
         position:        30
       },
       {
         id:            'Employee',
         purpose:       #STANDARD,
         type:          #LINEITEM_REFERENCE,
         label:         'Employee',
         position:      20,
         targetElement: '_Employee'
       }
     ]
  @UI: { lineItem:       [ { position: 10, importance: #HIGH } ] ,
         selectionField: [ { position: 10 } ] }
  @UI.textArrangement: #TEXT_SEPARATE
  Agency;

  @UI: { lineItem:       [ { position: 20, importance: #HIGH } ] ,
         fieldGroup:     [ { position: 20, qualifier: 'General_FG' } ],
         selectionField: [ { position: 20 } ] }
  Name;

  @UI: { fieldGroup:     [ { position: 30, qualifier: 'Address_FG' } ],
         identification: [ { position: 30 } ] }
  Street;

  @UI: { fieldGroup:     [ { position: 40, qualifier: 'Address_FG' } ],
         identification: [ { position: 40 } ] }
  PostalCode;

  @UI: { lineItem:       [ { position: 50, importance: #MEDIUM, type: #AS_FIELDGROUP, valueQualifier: 'Address_FG', label: 'Address' } ] ,
         identification: [ { position: 50 } ],
         fieldGroup:     [ { position: 50, qualifier: 'Address_FG' } ],
         selectionField: [ { position: 50 } ] }
  City;

  @UI: { identification: [ { position: 60 } ],
         fieldGroup:     [ { position: 60, qualifier: 'Address_FG' } ],
         selectionField: [ { position: 60 } ] }
  @UI.textArrangement: #TEXT_FIRST
  CountryCode;

  @UI: { lineItem:       [ { position: 70, importance: #LOW, type: #AS_FIELDGROUP, valueQualifier: 'Contact_FG', label: 'Contact Data' } ] ,
         fieldGroup:     [ { position: 70, qualifier: 'Contact_FG' } ],
         identification: [ { position: 70 } ] }
  PhoneNumber;

  @UI: { fieldGroup:     [ { position: 80, qualifier: 'Contact_FG' } ],
         identification: [ { position: 80 } ] }
  EMailAddress;

  @UI: { fieldGroup:     [ { position: 90, qualifier: 'Contact_FG' } ],
         identification: [ { position: 90 } ] }
  WebAddress;

  @UI.hidden: true
  LocalLastChangedAt;
}

来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译

← 定义层次结构的行为 · 最终确定业务服务 →