SAP Fiori Elements如何基于domain fixed value创建下拉菜单

Several days ago I wrote a blog How to build a drop down list using Smart template + CDS view which introduces how to create a drop down list based on values from a backend table.

For example, the status list in above screenshot comes from the three entries in database table ZSTATUS_FIXEDVAL.

And now a new requirement is to use the fixed value defined in an ABAP domain instead.

Here below are steps to achieve it.

(1) Create an ABAP domain named ZORDER_STATUS_DOMAIN containing status list as displayed in previous screenshot.
Then create an ABAP data element based on this domain.
Create one header database table as root,

and another database table as item.

(2) create two CDS views based on the two database tables accordingly:

@AbapCatalog.sqlViewName: 'zorheader'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'header view'
define view Z_I_Order_Header as select from zorder_header
association [0..*] to Z_I_Order_Item as _Item
on $projection.object_id = _Item.parent_id
{
  key zorder_header.object_id,
  zorder_header.description,
  zorder_header.order_status,
  zorder_header.order_status_text,
  @ObjectModel.association.type: #TO_COMPOSITION_CHILD
  _Item
}
@AbapCatalog.sqlViewName: 'zorITem'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'order item detail'
define view Z_I_Order_Item as select from zorder_item {
  @UI.lineItem : [{position:10, label : 'Parent ID'}]
  key zorder_item.parent_id,
  @UI.lineItem : [{position:20, label : 'Item Number'}]
  key zorder_item.item_id,
  @UI.lineItem : [{position:30, label : 'Item Description'}]
  zorder_item.item_text,
  zorder_item.item_type
}

Then the consumption CDS view:

@AbapCatalog.sqlViewName: 'zjorderview'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Jerry order consumption view'
@Search.searchable: true
@OData.publish: false
@ObjectModel: {
   compositionRoot: true,
   type: #CONSUMPTION,
   transactionalProcessingDelegated: true,
   createEnabled,
   deleteEnabled,
   updateEnabled
}
@UI.headerInfo:{
  typeName:       'Jerry Service Order',
  typeNamePlural: 'Jerry Service Orders',
  title: { value: 'object_id' },
  description: { value: 'description' }
}
define view Z_C_Order as select from Z_I_Order_Header {
  @UI.lineItem : [{position:10, label : 'Service Order ID'}]
  @UI.identification: [ { position: 10,  label : 'Service Order ID' } ]
  @Search:{ defaultSearchElement: true, ranking: #HIGH, fuzzinessThreshold: 0.8 }
  @UI.selectionField: [ { position: 20, label : 'Service Order ID' } ]
  key Z_I_Order_Header.object_id,
  @UI.lineItem : [{position:20, label : 'Service Order Description'}]
  @UI.identification: [ { position: 20,  label : 'Service Order Description' } ]
  @Search:{ defaultSearchElement: true, ranking: #HIGH, fuzzinessThreshold: 0.8 }
  @UI.selectionField: [ { position: 10, label : 'Description' } ]
  Z_I_Order_Header.description,
  @UI.identification: [ { position: 30,  label : 'Order Status' } ]
  Z_I_Order_Header.order_status,
  Z_I_Order_Header.order_status_text,
  @ObjectModel.association.type: [#TO_COMPOSITION_CHILD]
  Z_I_Order_Header._Item
}

Now all are done except remaining work in ABAP side.

(3) Create a new project via tcode SEGW and include the activated consumption CDS view created in step2. If you don’t know how to do it, refer to this blog Enable CRM Service Order application with edit functionality.

Generate runtime artifacts. Now both DPC and MPC classes are generated.

3.1 Redefine DEFINE method of your MPC_EXT class:

super->define( ).
    zcl_fis_shlp_annotation=>create(
      io_odata_model = model
      io_vocan_model = vocab_anno_model
      iv_namespace = 'sap'
      iv_entitytype = 'Z_C_OrderType'
      iv_property = 'order_status'
      iv_search_help = space
      iv_search_supported = abap_false
      iv_search_help_field = space
      iv_valuelist_entityset = 'OrderStatusEntitySet'
      iv_valuelist_property = 'Code' ##NO_TEXT
      )->add_display_parameter( iv_valuelist_property  = 'Text' ).
    data(lo_txt_property) = model->get_entity_type( 'Z_C_OrderType' )->get_property( 'order_status' ).
    lo_txt_property->set_value_list( /iwbep/if_mgw_odata_property=>gcs_value_list_type_property-fixed_values ).
    data(lo_text_anno) = lo_txt_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
    lo_text_anno->add( iv_key = 'text' iv_value = 'order_status_text').
    lo_txt_property = model->get_entity_type( 'OrderStatus' )->get_property( 'Code' ).
    lo_txt_property->set_value_list( /iwbep/if_mgw_odata_property=>gcs_value_list_type_property-fixed_values ).
    lo_text_anno = lo_txt_property->/iwbep/if_mgw_odata_annotatabl~create_annotation( 'sap' ).
    lo_text_anno->add( iv_key = 'text' iv_value = 'Text').

The code above will generate the highlighted annotations which could be found in metadata:

3.2 Since in 3.1, the entity set OrderStatusEntitySet and entity type OrderStatus do not exist in SEGW project yet, so now we have to create them manually.
Just create them from context menu by clicking Entity Types and Entity Sets in SEGW:

Create properties for entity type:

And assign entity type to newly created entity set. After that regenerate runtime artifacts.

3.3 Since now framework knows it should display the value of order_status_text to render the drop down list, so we are responsible to provide framework with correct content.
Redefine method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_ENTITYSET of your DPC_EXT:

METHOD /iwbep/if_mgw_appl_srv_runtime~get_entityset.
    CASE iv_entity_name.
      WHEN 'OrderStatus'.
        get_Status_list( IMPORTING er_entityset = er_entityset ).
      WHEN OTHERS.
        TRY.
            CALL METHOD super->/iwbep/if_mgw_appl_srv_runtime~get_entityset
              EXPORTING
                iv_entity_name           = iv_entity_name
                iv_entity_set_name       = iv_entity_set_name
                iv_source_name           = iv_source_name
                it_filter_select_options = it_filter_select_options
                it_order                 = it_order
                is_paging                = is_paging
                it_navigation_path       = it_navigation_path
                it_key_tab               = it_key_tab
                iv_filter_string         = iv_filter_string
                iv_search_string         = iv_search_string
                io_tech_request_context  = io_tech_request_context
              IMPORTING
                er_entityset             = er_entityset
                es_response_context      = es_response_context.
          CATCH /iwbep/cx_mgw_busi_exception .
          CATCH /iwbep/cx_mgw_tech_exception .
        ENDTRY.
        IF iv_entity_name = 'Z_C_OrderType'.
          fill_status_text( CHANGING cr_entityset = er_entityset ).
        ENDIF.
    ENDCASE.
  ENDMETHOD.

The method fill_status_text is responsible for filling the field order_status_text.

Note: if you comment out the line 40 and 46,

The annotation for order_status_text will become “standard”, instead of previous “fixed-values”.

In this case, this field will be rendered as F4 value help instead:


要获取更多Jerry的原创文章,请关注公众号"汪子熙":

posted @ 2020-09-04 14:25  JerryWang_汪子熙  阅读(225)  评论(0编辑  收藏  举报