如何动态改变ALV状态栏(Status)中的按钮文本和图标(Icon)

经常会在ALV的状态栏上加入自定义的按钮(button),有时还需要动态更改自定义按钮上的文本和图标,如下:开始是修改按钮,点击后变成显示按钮,按钮上的显示的内容可以自动切换。

下面介绍具体实现方法:

1,现在程序中定义一个全局变量,类型是 smp_dyntxt

1

DATA:g_text TYPE smp_dyntxt.

2,在创建状态(status)中的按钮时选择Dynamic Text选项

然后选择1中定义的全局变量

创建完如下:

REPORT ztest_alv_lvc_button.

DATA:go_grid TYPE REF TO cl_gui_alv_grid.
DATA:g_text TYPE smp_dyntxt.
DATA:g_display TYPE c.

TYPES:BEGIN OF gty_ekko,
        ebeln TYPE ekko-ebeln,
        verkf TYPE ekko-verkf,
        sel   TYPE c,  "用来标识行选择的字段
      END OF gty_ekko.
DATA:git_ekko TYPE STANDARD TABLE OF gty_ekko,
     gwa_ekko TYPE gty_ekko.

DATA:git_fcat   TYPE lvc_t_fcat,
     gwa_fcat   LIKE LINE OF git_fcat,
     gwa_layout TYPE lvc_s_layo.

CONSTANTS: gco_callback_user_command TYPE slis_formname  VALUE 'FRM_USER_COMMAND',
           gco_callback_status       TYPE slis_formname  VALUE 'FRM_USER_STATUS'.

START-OF-SELECTION.

  SELECT ebeln
         verkf
    INTO CORRESPONDING FIELDS OF TABLE git_ekko
    FROM ekko
   UP TO 10 ROWS.

  PERFORM frm_set_catalog.

  gwa_layout-zebra = 'X'.
  gwa_layout-box_fname = 'SEL'.  "指定行选择字段
  gwa_layout-cwidth_opt = 'X'.

* 初始化状态栏中的button
  g_text-icon_id   = icon_change .
  g_text-text      = 'Change' .
  g_text-icon_text = 'Change'.

  g_display = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      is_layout_lvc            = gwa_layout
      it_fieldcat_lvc          = git_fcat
      i_callback_pf_status_set = gco_callback_status
      i_callback_user_command  = gco_callback_user_command
    TABLES
      t_outtab                 = git_ekko[]
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc = 0.

  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_PF_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_user_status USING i_it_extab TYPE slis_t_extab.
  SET PF-STATUS 'S0001' .
ENDFORM.                    " FRM_SET_PF_STATUS

FORM frm_user_command USING i_ucomm       TYPE sy-ucomm
                            i_wa_selfield TYPE slis_selfield.
  CASE i_ucomm.
    WHEN 'CHANGE'.  "click Change button in application toolbar
      IF g_display = 'X'.
        g_text-icon_id   = icon_display .
        g_text-text      = 'Display' .
        g_text-icon_text = 'Display'.
        CLEAR:g_display.
      ELSE.
        g_text-icon_id   = icon_change .
        g_text-text      = 'Change' .
        g_text-icon_text = 'Change'.
        g_display        = 'X'.
      ENDIF.
    WHEN '&IC1'.  "Double click
*     if click on PO field, jump to me23n
      IF i_wa_selfield-fieldname = 'EBELN'.
        SET PARAMETER ID 'BES' FIELD i_wa_selfield-value.
        CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.
      ENDIF.
    WHEN OTHERS.

  ENDCASE.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_CATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_set_catalog .

  DATA:l_pos TYPE i VALUE 1.
  CLEAR: l_pos.
  l_pos = l_pos + 1.
  gwa_fcat-coltext   = 'PO'.
  gwa_fcat-scrtext_l = 'PO'.
  gwa_fcat-scrtext_m = 'PO'.
  gwa_fcat-scrtext_s = 'PO'.
  gwa_fcat-fieldname = 'EBELN'.
  gwa_fcat-col_pos = l_pos.
  gwa_fcat-outputlen = '10'.
  APPEND gwa_fcat TO git_fcat.
  l_pos = l_pos + 1.
  gwa_fcat-coltext   = 'PO item'.
  gwa_fcat-scrtext_l = 'PO item'.
  gwa_fcat-scrtext_m = 'PO item'.
  gwa_fcat-scrtext_s = 'PO item'.
  gwa_fcat-fieldname = 'VERKF'.
  gwa_fcat-col_pos = l_pos.
  gwa_fcat-outputlen = '20'.
  APPEND gwa_fcat TO git_fcat.

ENDFORM.

在 ABAP FUNCTION ALV 添加状态栏下拉框按钮,可以使用函数模块 `REUSE_ALV_HIERSEQ_LIST_DISPLAY`。该函数模块可以在状态栏添加下拉框按钮,用于切换不同的层次结构。 以下是添加下拉框按钮的代码示例: ``` REPORT ztest. DATA: it_list TYPE STANDARD TABLE OF sflight, wa_list TYPE sflight. DATA: it_fieldcat TYPE slis_t_fieldcat_alv. FIELD-SYMBOLS: <fs_fieldcat> TYPE slis_fieldcat_alv. * 初始化 ALV 数据 SELECT * FROM sflight INTO TABLE it_list. * 初始化字段目录 CLEAR: it_fieldcat. wa_fieldcat-fieldname = 'CARRID'. wa_fieldcat-ref_table = 'SCARR'. wa_fieldcat-ref_field = 'CARRID'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'CONNID'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'FLDATE'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'SEATSMAX'. APPEND wa_fieldcat TO it_fieldcat. wa_fieldcat-fieldname = 'SEATSOCC'. APPEND wa_fieldcat TO it_fieldcat. CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_pf_status_set = 'PF_STATUS' is_layout = VALUE #( ( hierarchy_seq = 'CARRID' ) ) TABLES t_fieldcat = it_fieldcat t_outtab = it_list. * 定义层次结构下拉框的值集合 DATA: it_hier TYPE STANDARD TABLE OF lvc_s_layo WITH HEADER LINE. it_hier-fieldname = 'HIERARCHY_SEQ'. it_hier-seltext_l = '层次结构'. it_hier-selopt = 'O'. it_hier-no_out = 'X'. APPEND it_hier. it_hier-fieldname = 'CARRID'. it_hier-seltext_l = '按航空公司'. it_hier-selopt = 'C'. it_hier-no_out = ' '. APPEND it_hier. it_hier-fieldname = 'FLDATE'. it_hier-seltext_l = '按航班日期'. it_hier-selopt = 'D'. it_hier-no_out = ' '. APPEND it_hier. it_hier-fieldname = 'SEATSMAX'. it_hier-seltext_l = '按最大座位数'. it_hier-selopt = 'S'. it_hier-no_out = ' '. APPEND it_hier. it_hier-fieldname = 'SEATSOCC'. it_hier-seltext_l = '按已占用座位数'. it_hier-selopt = 'O'. it_hier-no_out = ' '. APPEND it_hier. * 设置层次结构下拉框 SET PF-STATUS 'PF_STATUS' EXCLUDING it_hier. * PF-STATUS 事件处理程序 MODULE pf_status OUTPUT. SET PF-STATUS 'PF_STATUS'. SET TITLEBAR 'TITLE'. ENDMODULE. * AT SELECTION-SCREEN 事件处理程序 AT SELECTION-SCREEN ON HIERARCHY_SEQ. LOOP AT it_hier ASSIGNING FIELD-SYMBOL(<fs_hier>). IF <fs_hier>-fieldname = 'HIERARCHY_SEQ'. CONTINUE. ENDIF. IF <fs_hier>-selopt = 'X'. CLEAR: it_list. SELECT * FROM sflight INTO TABLE it_list ORDER BY carrid. ELSE. CLEAR: it_list. SELECT * FROM sflight INTO TABLE it_list ORDER BY ( <fs_hier>-fieldname ). ENDIF. CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY' EXPORTING i_callback_program = sy-repid i_callback_pf_status_set = 'PF_STATUS' is_layout = VALUE #( ( hierarchy_seq = <fs_hier>-fieldname ) ) TABLES t_fieldcat = it_fieldcat t_hierarchy = it_list. ENDLOOP. ``` 在代码,使用了 `REUSE_ALV_HIERSEQ_LIST_DISPLAY` 函数模块来显示 ALV 报表,并在 `is_layout` 参数指定默认的层次结构。在 `pf_status` 事件处理程序设置状态栏的标题,并在 `AT SELECTION-SCREEN` 事件处理程序根据用户选择的层次结构重新查询数据并刷新 ALV 报表。 通过在 `it_hier` 定义层次结构下拉框的值集合,并在 `SET PF-STATUS` 语句排除该值集合,即可在状态栏添加下拉框按钮。在 `AT SELECTION-SCREEN` 事件处理程序,根据用户选择的层次结构重新查询数据,并使用 `REUSE_ALV_HIERSEQ_LIST_DISPLAY` 函数模块刷新 ALV 报表显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值