ABAP:ALV 报表

1、可编辑的ALV表格

宏定义

DATA:gt_fieldcat TYPE lvc_t_fcat WITH HEADER LINE.

 DEFINE fill_alv.
    CLEAR GT_FIELDCAT.
    GT_FIELDCAT-FIELDNAME  = &1.   "变量1  字段名
    GT_FIELDCAT-COLTEXT    = &2.   "变量2  字段描述
    GT_FIELDCAT-OUTPUTLEN  = &3.   "变量3  字段长度
    GT_FIELDCAT-NO_ZERO    = &4.   "变量4  不显示0
    GT_FIELDCAT-EMPHASIZE  = &5.   "变量5  自适应大小
    GT_FIELDCAT-EDIT       = &6.   "变量6  可编辑
    GT_FIELDCAT-F4AVAILABL = &7.   "变量7  f4帮助

    APPEND GT_FIELDCAT.            "放入内表GT_FIELDCAT

  END-OF-DEFINITION.

调用宏定义赋值

 "宏定义名称——字段编码——字段描述——字段长度——不显示0——自适应大小——可编辑——f4帮助    
  fill_alv 'VKORG'  '销售组织'      '3' ''  ''  '' ''.
  fill_alv 'VTEXT1' '销售组织描述'  '20' ''  '' 'X' ''.
  fill_alv 'VTWEG'  '分销渠道'      '4' ''  ''  '' ''.
  fill_alv 'VTEXT2' '分销渠道描述'  '20' '' ''  'X' ''.
  fill_alv 'SPART'  '产品组'        '2' '' ''   ''   ''.
  fill_alv 'VTEXT3' '产品组描述'    '20' 'X' '' 'X'  ''.
  fill_alv 'VKBUR'  '销售部门'      '4'  ''  '' ''  ''.
  fill_alv 'VTEXT4' '销售部门描述'  '20' 'X' '' 'X'   ''.
  fill_alv 'VKGRP'  '销售组'        '3'  '' '' ''   ''.
  fill_alv 'VTEXT5' '销售组描述'    '20' 'X' '' 'X' ''.
  fill_alv 'B_DATE' '日期'          '8'  '' '' 'X' 'X'.
  fill_alv 'B_TIME' '时间'          '6'  '' '' 'X' 'X'.

控制栏定义

DATA: gs_layout   TYPE lvc_s_layo.


gs_layout-zebra      = 'X'.    "变换行颜色
gs_layout-cwidth_opt = 'X'.    "控制单元格大小
gs_layout-box_fname  = 'ZSEL'. "定义边框选中栏,这个名称,需要在定义内表时,定义同名的变量

2、F4触发按钮设置

" "设置FM_ALV 的EVENTS 事件: CALLER_EXIT.
FORM  fm04_set_fm_alv_event .
  DATA: ls_events TYPE slis_alv_event.

  ls_events-name = 'CALLER_EXIT' .
  ls_events-form =  'FRM_CALLER_EXIT'.
  APPEND ls_events TO gt_events.

ENDFORM.

FORM frm_caller_exit USING e_grid TYPE slis_data_caller_exit.
  DATA:
        lo_event_receiver TYPE REF TO lcl_event_receiver.

  DATA:  "定义的变量为类的方法:REGISTER_F4_FOR_FIELDS 用参数
    lt_f4 TYPE lvc_t_f4 WITH HEADER LINE.


*f4 帮助 宏定义
  DEFINE f4_help.

    lt_f4-fieldname = &1.   "指定字段'&1'有搜索帮助
    lt_f4-register  = 'X'.
    lt_f4-getbefore = 'X'.
    lt_f4-chngeafter  = 'X'.
    APPEND lt_f4.
    CLEAR lt_f4.
  END-OF-DEFINITION.

  IF go_grid IS INITIAL.
    CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
      IMPORTING
        e_grid = go_grid.
  ENDIF.
* 设置事件-回车
  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 3.
**指定OOALV 中F4字段

  "排序表必须按照字母顺序进行添加
  f4_help 'AUART'.
  f4_help 'B_DATE'.
  f4_help 'B_TIME'.
  f4_help 'E_DATE'.
  f4_help 'E_TIME'.

  CALL METHOD go_grid->register_f4_for_fields
    EXPORTING
      it_f4 = lt_f4[].
**** 注册F4 搜索帮助处理方法
  CREATE OBJECT lo_event_receiver.
  SET HANDLER lo_event_receiver->handle_f4 FOR go_grid.
ENDFORM.

**-------------------------------------------------------
*&---------------------------------------------------------------------*
*& 包含               ZRAY_FM_ALV_F4_FM02
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form f4_help_plane
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> E_FIELDNAME
*&      --> ES_ROW_NO
*&      --> ER_EVENT_DATA
*&---------------------------------------------------------------------*
FORM f4_help_plane  USING    p_e_fieldname TYPE lvc_fname   "输入的: 列名称
                             p_es_row_no   TYPE lvc_s_roid   "输入的: 行号
                             p_er_event_data TYPE REF TO cl_alv_event_data.

  DATA: lt_return_tab TYPE STANDARD TABLE OF ddshretval,
        ls_return_tab TYPE ddshretval.
  DATA:l_date TYPE sy-datum.
  DATA: l_time TYPE sy-uzeit.

*  gt_alv = gt_in.
  IF p_e_fieldname = 'AUART'      .
    CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
      EXPORTING
        retfield         = 'AUART'      "搜索帮助表:gt_auart 里的字段:AUART
        dynpprog         = sy-repid     " 当前程序
        dynpnr           = sy-dynnr     "当前屏幕号
        value_org        = 'S'           "默认为C但是此处不用S不行
        callback_program = sy-repid     "回诘程序,也为当前程序
      TABLES
        value_tab        = gt_auart     "F4 搜索帮助表 (说明:函数对这个表参照类型并未做规定,但返回表的类型是定死的为:DDSHRETVAL
        return_tab       = lt_return_tab   "选择后,返回表值:已经选的值。准备用来回写到ALV表对应字段上
      EXCEPTIONS
        parameter_error  = 1
        no_values_found  = 2
        OTHERS           = 3.
    IF sy-subrc = 0.
***  将选定的值填入ALV字段中: 这步回填,是在FM_ALV层次上操作。
      CLEAR: gs_alv.    " 先清空一下工作区,下步好用
      READ TABLE gt_in INTO gs_alv INDEX p_es_row_no-row_id.  "根据传入的参数,当前行号,读取选择操作对应的字段行记录
      IF sy-subrc = 0.                                      "如果读取成功,说明有这条记录
        READ TABLE lt_return_tab INTO ls_return_tab INDEX 1 . "从返回值表中,读取选择定后值记录
        IF ls_return_tab-fieldval IS NOT INITIAL .                                    " 如果读取成功
          gs_alv-auart = ls_return_tab-fieldval.
        ENDIF.
        MODIFY gt_in FROM gs_alv INDEX p_es_row_no-row_id TRANSPORTING auart. "注意工作区内唯一发生改变的是PLANETYPE 字段
      ENDIF.
      "X表示事件已经处理,不会去调取系统标准搜索帮助
      p_er_event_data->m_event_handled = 'X'.
    ENDIF.
  ELSEIF  p_e_fieldname = 'B_TIME' OR  p_e_fieldname = 'B_DATE' .

    IF  p_e_fieldname = 'B_DATE' .

      CALL FUNCTION 'F4_DATE'
        EXPORTING
          date_for_first_month         = sy-datum
        IMPORTING
          select_date                  = l_date     "用户选择后返回的日期
        EXCEPTIONS
          calendar_buffer_not_loadable = 1
          date_after_range             = 2
          date_before_range            = 3
          date_invalid                 = 4
          factory_calendar_not_found   = 5
          holiday_calendar_not_found   = 6
          parameter_conflict           = 7
          OTHERS                       = 8.

      IF sy-subrc = 0.
***  将选定的值填入ALV字段中: 这步回填,是在FM_ALV层次上操作。
        CLEAR: gs_alv.    " 先清空一下工作区,下步好用
        READ TABLE gt_in INTO gs_alv INDEX p_es_row_no-row_id.  "根据传入的参数,当前行号,读取选择操作对应的字段行记录
        IF sy-subrc = 0.                                      "如果读取成功,说明有这条记录
          IF p_e_fieldname = 'B_DATE'.
            gs_alv-b_date = l_date .
            MODIFY gt_in FROM gs_alv INDEX p_es_row_no-row_id TRANSPORTING b_date. "注意工作区内唯一发生改变的是B_DATE 字段
          ENDIF.

        ENDIF.
        "X表示事件已经处理,不会去调取系统标准搜索帮助
        p_er_event_data->m_event_handled = 'X'.
      ENDIF.
    ENDIF.

    IF p_e_fieldname = 'B_TIME'.

      CALL FUNCTION 'F4_CLOCK'
        EXPORTING
          start_time    = sy-uzeit
          display       = ' '
        IMPORTING
          selected_time = l_time.
      IF sy-subrc = 0.
***  将选定的值填入ALV字段中: 这步回填,是在FM_ALV层次上操作。
        CLEAR: gs_alv.    " 先清空一下工作区,下步好用
        READ TABLE gt_in INTO gs_alv INDEX p_es_row_no-row_id.  "根据传入的参数,当前行号,读取选择操作对应的字段行记录
        IF sy-subrc = 0.                                      "如果读取成功,说明有这条记录
          IF p_e_fieldname = 'B_TIME'.
            gs_alv-b_time = l_time.
            MODIFY gt_in FROM gs_alv INDEX p_es_row_no-row_id TRANSPORTING b_time. "注意工作区内唯一发生改变的是B_TIME 字段
          ENDIF.
        ENDIF.
        "X表示事件已经处理,不会去调取系统标准搜索帮助
        p_er_event_data->m_event_handled = 'X'.
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.

类定义和实现

*---------------------------------------------------------------------------------------------
*----------Reresh internal table data after input 'enter' command.
*----------------------------Enter event Definition-------------------------------------------
DATA: stbl     TYPE lvc_s_stbl,
      g_grid   TYPE REF TO cl_gui_alv_grid,
      l_grid   TYPE  slis_data_caller_exit,
      selfield TYPE slis_selfield.
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       类定义
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION."类定义
  PUBLIC SECTION.
    METHODS:
      handle_data_changed FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING e_modified et_good_cells.
ENDCLASS. "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*       类实现
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION."类实现
  METHOD handle_data_changed.
*    PERFORM handle_data_changed ."具体处理数据事件
*    IF e_modified = 'X'.
    stbl-row = 'X'." 基于行的稳定刷新
    stbl-col = 'X'." 基于列稳定刷新
    CALL METHOD g_grid->refresh_table_display
      EXPORTING
        is_stable = stbl.
*    ENDIF.
  ENDMETHOD.                    "HANDLE_DATA_CHANGED
ENDCLASS. "lcl_event_handler IMPLEMENTATION


CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS: handle_f4
                FOR EVENT onf4 OF cl_gui_alv_grid
      IMPORTING e_fieldname    "字段名称
                es_row_no       "行号,注意这是个结构
                er_event_data .    "事件数据
ENDCLASS.

CLASS: lcl_event_receiver IMPLEMENTATION.
  METHOD handle_f4.
    DATA: ls_stable TYPE lvc_s_stbl.   "定义基于稳定的刷新 所需要参数。
    ls_stable-row = 'X'.    "行固定
    ls_stable-col = 'X'.   "列固定

    IF e_fieldname = 'AUART'.        "注意大写
      PERFORM f4_help_plane USING e_fieldname      "字段名称
                                   es_row_no       "行号,注意这是个结构
                                   er_event_data .    "事件数据
    ELSEIF e_fieldname = 'B_DATE'  .   "又是小写的问题,一定要注意
      PERFORM f4_help_plane USING e_fieldname    "字段名称
                                   es_row_no       "行号,注意这是个结构
                                   er_event_data .    "事件数据
    ELSEIF e_fieldname = 'B_TIME'  .   "又是小写的问题,一定要注意
      PERFORM f4_help_plane USING e_fieldname    "字段名称
                                   es_row_no       "行号,注意这是个结构
                                   er_event_data .    "事件数据

    ENDIF.
    CALL METHOD go_grid->refresh_table_display    " 使用类:CL_GUI_ALV_GRID的实例的方法,稳定刷新
      EXPORTING
        is_stable = ls_stable.
  ENDMETHOD.
ENDCLASS.

3、GUI状态设置

FORM set_pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STANDARD'.
ENDFORM.

4、自定义按钮

FORM user_command USING ucomm TYPE sy-ucomm
    rs_selfield TYPE slis_selfield.

  CASE ucomm.
    WHEN '&BACK' OR '&F03'.
      LEAVE TO SCREEN 0.
    WHEN '&ADD'.
      PERFORM add_new_line.

    WHEN '&SAVE'  .

      PERFORM save_data.
  ENDCASE.
  rs_selfield-refresh = 'X'.   "必须加这一行,否则修改后不能更改
ENDFORM.

5、调用ALV显示

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'SET_PF_STATUS'
      i_callback_user_command  = 'USER_COMMAND'
      is_layout_lvc            = gs_layout
*     i_grid_settings          = i_grid_settings
      it_fieldcat_lvc          = gt_fieldcat[]
      it_events                = gt_events[]
      i_save                   = 'A'
    TABLES
      "    t_outtab            = export_list_one
      t_outtab                 = pt_tab
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

6、调用

START-OF-SELECTION.
PERFORM fm03_create_f4_plane_table.
PERFORM fm04_set_fm_alv_event.     "设置FM_ALV 的EVENTS 事件: CALLER_EXIT.
PERFORM display_data TABLES gt_in.

END-OF-SELECTION.

参考博文:

ABAP-ALV-列:F4搜索帮助_abap中alv界面的搜索帮助设置-CSDN博客

选择画面中日期F4输入帮助函数汇总 | 摆渡SAP

https://www.cnblogs.com/seven1314pp/p/15715614.html

https://www.cnblogs.com/mingdashu/p/5795967.html

你可能感兴趣的:(SAP学习,前端,服务器)