F4 help code

*&---------------------------------------------------------------------*
*& Report  Z_TEST_F4_01
*&
*&---------------------------------------------------------------------*
*& Author: Zengjw_2004@hotmail.com
*& Date: Jan-27-2010
*&
*&---------------------------------------------------------------------*

REPORT  z_test_f4_01.

*----------------------------------------------------------------------*
* First way
*----------------------------------------------------------------------*
*TYPES: BEGIN OF y_value_tab ,
*        matnr TYPE  mara-matnr,
*        maktx  TYPE makt-maktx,
*       END OF y_value_tab.
*
*
*DATA: ts_value_tab  TYPE STANDARD TABLE OF y_value_tab,
*      e_value_tab   TYPE y_value_tab ,
*      w_dynpprog    TYPE sy-repid,
*      w_dynpnr      TYPE sy-dynnr
*      .
*
*PARAMETERS: p_matnr TYPE mara-matnr.
*
*INITIALIZATION.
*
*  MOVE sy-repid TO   w_dynpprog.
*
*  MOVE sy-dynnr TO   w_dynpnr.
*
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_matnr.
*  PERFORM f_f4.
*
*START-OF-SELECTION.
*
**&---------------------------------------------------------------------*
**&      Form  f_f4
**&---------------------------------------------------------------------*
**       text
**----------------------------------------------------------------------*
*FORM f_f4.
*
*  SELECT
*    a~matnr
*    maktx
*  UP TO 10 ROWS
*  FROM mara AS a INNER JOIN makt AS b
*  ON a~matnr EQ b~matnr
*  INTO TABLE
*  ts_value_tab.
*
*  IF  sy-subrc EQ 0.
*
*    SORT ts_value_tab BY matnr.
*
*  ENDIF.
*
*  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
*    EXPORTING
**   DDIC_STRUCTURE         = ' '
*      retfield               = 'MATNR'
**   PVALKEY                = ' '
*     dynpprog               = w_dynpprog
*     dynpnr                 = w_dynpnr
*     dynprofield            = 'P_MANTR'
**   STEPL                  = 0
**   WINDOW_TITLE           =
**   VALUE                  = ' '
*     value_org              = 'S'
**   MULTIPLE_CHOICE        = ' '
**   DISPLAY                = ' '
**   CALLBACK_PROGRAM       = ' '
**   CALLBACK_FORM          = ' '
**   MARK_TAB               =
** IMPORTING
**   USER_RESET             =
*    TABLES
*      value_tab              = ts_value_tab
**   FIELD_TAB              =
**    RETURN_TAB            =
**   DYNPFLD_MAPPING        =
** EXCEPTIONS
**   PARAMETER_ERROR        = 1
**   NO_VALUES_FOUND        = 2
**   OTHERS                 = 3
*            .
*  IF sy-subrc <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
**         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*  ENDIF.
*
*ENDFORM.                                                    "f_f4
*
*END-OF-SELECTION.
*
*  WRITE: 'The selected values:', p_matnr.

*----------------------------------------------------------
* The second way
*----------------------------------------------------------

TYPES: BEGIN OF y_value_tab ,
        matnr TYPE  mara-matnr,
        maktx  TYPE makt-maktx,
       END OF y_value_tab.

DATA: ts_value_tab  TYPE STANDARD TABLE OF y_value_tab,
      e_value_tab   TYPE y_value_tab
      .

DATA:
      is_return_tab      TYPE STANDARD TABLE OF ddshretval,
      e_return_tab       TYPE ddshretval
      .

PARAMETERS: p_matnr TYPE mara-matnr.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_matnr.


  CONSTANTS: c_matnr  TYPE dfies-fieldname  VALUE 'MATNR'.

  SELECT
    a~matnr
    maktx
  UP TO 10 ROWS
  FROM mara AS a INNER JOIN makt AS b
  ON a~matnr EQ b~matnr
  INTO TABLE
  ts_value_tab.

  IF  sy-subrc EQ 0.

    SORT ts_value_tab BY matnr.

  ENDIF.

  free: is_return_tab.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
*   DDIC_STRUCTURE    = ''
      retfield        = c_matnr
      value_org       = 'S'
    TABLES
      value_tab       = ts_value_tab
*      field_tab       = is_field_tab
      return_tab      = is_return_tab
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3
      .

  IF sy-subrc = 0.

    CLEAR: e_return_tab.

    READ TABLE is_return_tab INDEX 1 INTO e_return_tab.

    p_matnr = e_return_tab-fieldval.

    CLEAR: e_return_tab.

    FREE: is_return_tab.

  ENDIF.


==========================================================================
Function module documentation

FU F4IF_INT_TABLE_VALUE_REQUEST
____________________________________________________
Short Text
F4 help also returning the value to be displayed in internal table

Functionality
This module implements the standard help at POV while passing the values to be displayed in a table. The entire dialogue behaves as for a standard F4 help. This module also supports input help control.

Note: Before you use this module at POV (Process on Value-Request), reconsider whether a search help could not carry out the same task. You can implement self-defined value selection in a search help exit. This has the advantage that you can attach the search help to a data element or a field of a structure. The F4 help is then automatically available for all users of the data element or structure.

The values to be displayed are passed in table VALUE_TAB. There are different ways to organize the data in VALUE_TAB. These are mainly defined with the parameter VALUE_ORG.

1. VALUE_ORG = 'S' (Structure)
This option should be used for new developments (despite other default values).
VALUE_TAB is an internal table with a (flat) structure. Every line in VALUE_TAB then corresponds to one line in the list to be displayed.
How the definition of the data struture is passed to the module is described below.
The data in VALUE_TAB is available in internal representation.
2. VALUE_ORG = 'C' (Column)
Each line of VALUE_TAB contains the contents of a single field. The contents of VALUE_TAB are copied to the hit list line by line. The description of the columns is passed in FIELD_TAB. When all fields of FIELD_TAB have been edited, the next line is processed.
In this case the values in VALUE_TAB must be passed in external representation. This means that for types whose external and internal representation differ, the values should be passed to VALUE_TAB with the ABAP command WRITE and not with MOVE (These are for example all the numeric types, date, fields with conversion exit, etc.). Use the extension LEFT-JUSTIFIED for numbers.
Caution: Do not copy any constants directly to VALUE_TAB. For example, if a date is defined as constant "01.01.1998", it can no loner be interpreted for other user-specific settings.
Definition of the data structure of VALUE_TAB.
Depending on how the internal table VALUE_TAB was defined, there are different ways to pass this structure to the module.

In the end, the data structure is always stored internally in table FIELD_TAB. In addition to passing the value directly in the call, there are different ways for the module to fill the table automatically or to enhance the data automatically.

1. The structure of internal table VALUE_TAB is defined in the ABAP Dictionary. For example the table is defined as follows:
DATA: VALUE_TAB LIKE <DDIC structure> OCCURS 0 [WITH HEADER LINE].
In this case it is sufficient to pass the name of the reference structure to parameter DDIC_STRUCTURE. Table FIELD_TAB should not be passed in this case because it is automatically filled with the information from the ABAP Dictionary in the function module.
Table FIELD_TAB only has to be defined instead of parameter DDIC_STRUCTURE if for example some of the fields of the structure should not be displayed or if the order of the columns or header texts should be changed.
If only fields TABNAME and FIELDNAME are filled in FIELD_TAB, the module gets the remaining information from the ABAP Dictionary. As a result you can easily choose individual fields and their order.
If you want to change text, you should fill FIELD_TAB initially using the module DDIF_FIELDINFO_GET. You can then change for example the text in FIELD_TAB.
2. Table VALUE_TAB can for example be defined as follows:
DATA: BEGIN OF VALUE_TAB OCCURS 0,
         FIELD1 LIKE <DDIC FIELD>,
         FIELD2 TYPE <Data element>,
         "etc.
      END OF VALUE_TAB.
If all the fields have an explicit reference to a DDIC field (mit LIKE) or to a data element (with TYPE), the module itself can determine the structure of VALUE_TAB. In this case do not pass either DDIC_STRUCTURE or FIELD_TAB.
If you want to pass the field description with FIELD_TAB (for example because you do not want to display all the fields), you normally should not only fill TABNAME and FIELDNAME. The module then gets all the information from the ABAP Dictionary. This also includes the relative position of a field to the beginning of the Dictionary structure. However, this usually will not agree with the relative position of the field in VALUE_TAB.
In this case it is better to fill table FIELD_TAB using module DDIF_FIELDINFO_GET. The relative position of the fields in VALUE_TAB should then be corrected in FIELD_TAB-OFFSET. You can use for example the ABAP command DESCRIBE DISTANCE here.
3. The table has only one field that holds the values column by column. 'C' is passed as VALUE_ORG. The definition of VALUE_TAB could be for example:
DATA: VALUE_TAB(100) TYPE C OCCURS 0 [WITH HEADER LINE].
The definition of the columns should be passed in FIELD_TAB. Again you can limit yourself to TABNAME and FIELDNAME if the remaining information (type, length, headers) should be obtained from the ABAP Dictionary.
The relative position of the fields to the beginning of the structure (FIELD_TAB-OFFSET) are of no importance for this type of data transfer.
Even if you want to pass the entire information in FIELD_TAB yourself, you nevertheless should first fill FIELD_TAB with DDIF_FIELDINFO_GET from the ABAP Dictionary. You can then change individual fields, such as header text, before calling the module.
If you define parameter DDIC_STRUCTURE, FIELD_TAB is internally given the field description of the DDIC structure passed. This is normally meaningless for column-by-column data transfer.
The field names in FIELD_TAB must be unique. If you need fields with the same name from different structures, you should get the information with the function module DDIF_FIELDINFO_GET yourself and then change the field name.

Return the selection in the screen
If the screen information DYNPPROG, DYNPNR, DYNPROFIELD and possibly STEPL are also defined, the selected value is automatically returned in the screen field.

If possible, you should always give this information because the amodal control version of the F4 help can only be supported with this information. A user who switches on the F4 help control does not see a difference between the standard F4 help and a help implemented with this function module in this case.

If the user activates the ActiveX help, it is started amodally under the following conditions:
1. The screen information DYNPPROG, DYNPNR and DYNPROFIELD must be defined.
2. When the function module is called, the RETURN_TAB may not be defined. (In an amodal call, the function module returns before the user has selected a value).

Normally values are only returned in input fields. This behavior can be changed with parameter DISPLAY (see parameter documentation).

With Release 4.6C the module can automatically recognize an input field.

 

Notes
DYNPPROG and DYNNR cannot be passed as SY-REPID and SY-DYNNR because they are only analyzed after the function module has been called. Instead, first copy the SY fields to local variables and then pass them to the function module.
The value for STEPL is not within a step loop 0. Within a step loop, the current line can be defined at time POV with the function module DYNP_GET_STEPL.
If you do not want to return the selected value automatically, you should not define parameters DYNPPROG, DYNNR etc. A value is not returned automatically if MULTIPLE_SELECTION is selected either, but this could change in the future!
Calls that do not permit a value to be returned automatically can only be implemented modally. The input help control nevertheless can be used for the display, but no other entries can be made in the screen as long as the popup window is displayed. The value is returned in table RETURN_TAB. If the user terminates the F4 help, RETURN_TAB is returned empty (termination does not result in an exception).
Further information
Also read the documentation for the parameters, especially for VALUE and CALLBACK_FORM.

Parameters
DDIC_STRUCTURE
RETFIELD
PVALKEY
DYNPPROG
DYNPNR
DYNPROFIELD
STEPL
WINDOW_TITLE
VALUE
VALUE_ORG
MULTIPLE_CHOICE
DISPLAY
CALLBACK_PROGRAM
CALLBACK_FORM
MARK_TAB
USER_RESET
VALUE_TAB
FIELD_TAB
RETURN_TAB
DYNPFLD_MAPPING

Exceptions
PARAMETER_ERROR
NO_VALUES_FOUND

Function Group
SDHI
=========================================================================================================================================================

<think>我们被要求提供关于ABAP视图代码的解答。在ABAP中,视图(View)是数据库表的虚拟表示,它不存储数据,而是提供一种方式来组合和呈现来自一个或多个表的数据。ABAP中有多种类型的视图,如数据库视图(Database View)、维护视图(Maintenance View)、投影视图(Projection View)和帮助视图(Help View)。这里我们将重点介绍数据库视图的创建和使用。 ### 数据库视图(Database View)的创建 数据库视图是通过SE11事务码(ABAP字典工具)创建的,并且可以在ABAP程序中使用。视图可以连接多个表,选择字段,并定义条件。 #### 创建步骤(在SE11中): 1. 输入视图名称(以`Z`或`Y`开头)。 2. 选择“视图”类型为“数据库视图”。 3. 添加参与的表,并指定连接条件。 4. 选择需要包含在视图中的字段。 5. 可以设置选择条件(Selection Conditions)。 6. 激活视图。 #### 在ABAP程序中使用视图 一旦视图被激活,就可以像普通数据库表一样在ABAP程序中使用`SELECT`语句进行查询。 #### 示例代码:创建一个简单的数据库视图并查询 假设我们有两个表:`SFLIGHT`(航班表)和`SCARR`(航空公司表)。我们创建一个视图`Z_FLIGHT_CARRIER`,它连接这两个表,并包含航班号、航线、航空公司名称和货币。 ##### 视图定义(在SE11中创建): - 视图名:`Z_FLIGHT_CARRIER` - 基表:`SFLIGHT`和`SCARR` - 连接条件:`SFLIGHT~CARRID = SCARR~CARRID` - 字段选择: - `SFLIGHT`:`CARRID`, `CONNID`, `FLDATE`, `PRICE`, `CURRENCY` - `SCARR`:`CARRNAME` ##### ABAP程序中使用该视图: ```abap REPORT z_view_demo. DATA: gt_flights TYPE TABLE OF z_flight_carrier, gs_flight TYPE z_flight_carrier. * 从视图中选择数据 SELECT carrid, connid, fldate, carrname, price, currency FROM z_flight_carrier INTO TABLE gt_flights WHERE carrid = 'LH' " 航空公司代码为LH AND fldate BETWEEN '20230101' AND '20231231'. * 输出数据 LOOP AT gt_flights INTO gs_flight. WRITE: / gs_flight-carrid, gs_flight-connid, gs_flight-fldate, gs_flight-carrname, gs_flight-price, gs_flight-currency. ENDLOOP. ``` #### 代码解释: 1. **视图定义**:在SE11中创建视图时,我们选择了两个表,并指定了连接条件,然后选择了需要的字段。视图激活后,在ABAP字典中就可以像表一样使用。 2. **ABAP程序**: - 声明了一个内表`gt_flights`,其结构为视图`Z_FLIGHT_CARRIER`的结构。 - 使用`SELECT`语句从视图中读取数据,条件为航空公司代码为'LH'(汉莎航空)且航班日期在2023年内。 - 循环内表并输出每条记录的字段。 #### 注意事项: - 数据库视图在数据库层实现,因此查询时会转换为底层数据库的SQL语句(如OpenSQL转换为本地SQL)。 - 视图只能用于读取数据,不能用于更新(除非是单表视图且满足更新条件)。 - 在视图中可以定义参数化条件(使用参数`:`),但在ABAP中查询时不能直接使用参数化视图,而是使用选择条件。 ### 其他类型的视图 1. **维护视图(Maintenance View)**:用于通过SM30维护数据,可以基于多个表,但要求这些表通过外键关联。 2. **投影视图(Projection View)**:用于屏蔽表中某些字段,只暴露部分字段。只能基于一个表。 3. **帮助视图(Help View)**:用于为输入帮助(F4)提供数据源。 ### 视图的维护 - 可以通过SE11修改视图定义,然后重新激活。 - 在ABAP程序中,视图的使用和表类似,但要注意性能,特别是当视图连接多个大表时。 §§相关问题§§: 1. ABAP中数据库视图和表连接(JOIN)在性能上有何区别? 2. 如何在ABAP中创建和维护维护视图(Maintenance View)? 3. ABAP投影视图(Projection View)的主要用途是什么? 4. 在ABAP中,如何通过视图实现数据的增删改查?</think>### ABAP 视图(View)代码详解 在 ABAP 中,视图(View)是数据库表的虚拟表示,它提供了一种灵活的数据访问方式而无需物理存储数据。ABAP 支持多种视图类型,每种都有特定用途: #### 视图类型对比 | 视图类型 | 事务码 | 基表数量 | 主要用途 | 是否可写 | |----------|--------|----------|----------|----------| | 数据库视图 | SE11 | 多个 | 复杂数据关联 | 只读 | | 投影视图 | SE11 | 单个 | 字段屏蔽 | 只读 | | 维护视图 | SE11 | 多个 | 数据维护界面 | 可写 | | 帮助视图 | SE11 | 多个 | 搜索帮助 | 只读 | | CDS 视图 | Eclipse | 多个 | 现代数据建模 | 只读 | #### 1. 数据库视图(Database View)示例 用于连接多个相关表的数据: ```abap " SE11 创建视图 ZV_FLIGHT_DETAILS VIEW ZV_FLIGHT_DETAILS AS SELECT FROM sflight JOIN scarr ON sflight~carrid = scarr~carrid JOIN spfli ON sflight~carrid = spfli~carrid AND sflight~connid = spfli~connid { // 选择字段 sflight.carrid, sflight.connid, sflight.fldate, scarr.carrname, spfli.cityfrom, spfli.cityto, sflight.price, sflight.currency } WHERE sflight~planetype = '747' // 筛选条件 ``` **程序中使用:** ```abap REPORT z_flight_report. DATA: gt_flights TYPE TABLE OF zv_flight_details, gs_flight TYPE zv_flight_details. START-OF-SELECTION. SELECT * FROM zv_flight_details INTO TABLE gt_flights WHERE cityfrom = 'FRANKFURT' AND fldate >= '20230101'. LOOP AT gt_flights INTO gs_flight. WRITE: / gs_flight-carrid, gs_flight-connid, gs_flight-carrname, gs_flight-cityfrom, '->', gs_flight-cityto, gs_flight-price, gs_flight-currency. ENDLOOP. ``` #### 2. 维护视图(Maintenance View)示例 用于创建可维护的数据界面: ```abap " SE11 创建维护视图 ZV_CUSTOMER_MAINT VIEW ZV_CUSTOMER_MAINT WITH HEADER LINE BASED ON { kna1, // 客户主数据 knb1 // 公司代码数据 } RELATION kna1~kunnr = knb1~kunnr FIELDS ( kna1.kunnr AS Customer, kna1.name1 AS Name, kna1.ort01 AS City, knb1.bukrs AS Company, knb1.akont AS ReconAccount ) MAINTENANCE STATUS zcust_mnt // 维护状态 MAINTENANCE SCREEN 1000. // 维护屏幕 ``` **生成维护程序:** ```abap " 在 SE11 中生成维护程序 FUNCTION-POOL z_customer_maintenance. MODULE status_0100 OUTPUT. SET PF-STATUS 'MAINTAIN'. SET TITLEBAR 'CUST_MNT'. ENDMODULE. MODULE user_command_0100 INPUT. CASE sy-ucomm. WHEN 'SAVE'. CALL METHOD cust_mnt->save( ). WHEN 'BACK'. LEAVE TO SCREEN 0. ENDCASE. ENDMODULE. ``` #### 3. CDS 视图(Core Data Services)示例 现代 ABAP 开发推荐的数据建模方式: ```abap // CDS 视图 ZCDS_FLIGHT_ANALYSIS @AbapCatalog.sqlViewName: 'ZCDSFLTANL' @AccessControl.authorizationCheck: #NOT_REQUIRED define view ZCDS_FLIGHT_ANALYSIS as select from sflight association [1..1] to scarr as _carrier on $projection.carrid = _carrier.carrid { key carrid, key connid, key fldate, price, currency, seatsocc, seatsmax, // 计算字段 cast( seatsocc as abap.fltp ) / seatsmax as occupancyRate, // 关联字段 _carrier.carrname } where planetype = '747'; ``` **ABAP 程序中使用 CDS 视图:** ```abap REPORT z_cds_flight_report. DATA: lt_results TYPE TABLE OF zcds_flight_analysis. START-OF-SELECTION. SELECT * FROM zcds_flight_analysis INTO TABLE @lt_results WHERE carrid = 'LH' ORDER BY fldate DESCENDING. cl_demo_output=>display( lt_results ). ``` #### 4. 投影视图(Projection View)示例 用于简化表结构: ```abap " SE11 创建投影视图 ZV_PROJ_CUSTOMER VIEW ZV_PROJ_CUSTOMER AS PROJECTION FROM kna1 { kunnr, name1, ort01, land1, pstlz } ``` #### 关键注意事项 1. **性能优化**: - 避免在视图的WHERE子句中使用复杂函数 - 限制连接表的数量(通常不超过5个) - 使用索引字段作为连接条件 2. **更新限制**: - 只有维护视图支持数据修改 - 数据库视图/投影视图是只读的 - 更新需要基础表有适当的维护视图 3. **权限控制**: ```abap @EndUserText.label: 'Flight Data' @AccessControl.authorizationCheck: #CHECK define view ZCDS_FLIGHT_AUTH as select from sflight { carrid, connid, fldate, price } where planetype = '747' ``` 4. **调试技巧**: - 使用`EXPLAIN`查看SQL执行计划 - 激活运行时检查工具(ST05) - 使用`CL_DEMO_OUTPUT`快速测试CDS视图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值