ABAP ALV 单元格按钮


前言

因为动态单元格可编辑的原因,小小了解了一下类型:LVC_T_STYL(ALV 控制: 单元格的类型表)和CL_GUI_ALV_GRID,发现了ALV单元格也可以改变为BUTTEN按钮,做了一个小例子程序,可能应用的不是很广泛,我们也可以用HOTSPOT(热键-单击敏感)或双击事件等方法来实现相同的效果,希望能帮助到大家。

一、介绍

具体方法如下:

1.提前给想要变成按钮的单元格赋值图标代码;

2.在ALV数据输出表定义field_style参考表类型LVC_T_STYL( 单元格的类型表)主要是放入控制单元格的状态的一些参数;

3.把想编辑处理的字段名称放入LVC_T_STYL的fieldname中,把类CL_GUI_ALV_GRID=>MC_STYLE_BUTTON方法(单元格按钮),放入LVC_T_STYL的style中;

4.Layout的stylefname 为 ALV数据表添加的field_style表类型字段;

5.触发单元格按钮事件为:CL_GUI_ALV_GRID的BUTTON_CLICK。

二、实现步骤

1.代码展示

代码如下(示例):

*&---------------------------------------------------------------------*
*& 程 序 名:YTEST_001
*& 程序描述:ABAP ALV行单元格按钮
*& 创 建 者:爱吃鱼香肉丝的abaper
*& 创建日期:2023/08/04
*&---------------------------------------------------------------------*
*& 版本     修改者(公司)     日期         修改描述
*& 1.0.0    XXXX           YYYYMMDD      创建程序
*&---------------------------------------------------------------------*
REPORT ytest_001.

*&---------------------------------------------------------------------*
* TYPES
*&---------------------------------------------------------------------*
TABLES:sflight.
TYPES:
  BEGIN OF ty_alv,
    carrid      TYPE sflight-carrid,     " 航线代码
    fldate      TYPE sflight-fldate,     " 航班日期
    price       TYPE sflight-price,      " 航空运费
    planetype   TYPE sflight-planetype,  " 飞机类型
    paymentsum  TYPE sflight-paymentsum, " 当前预定总数
    buten       TYPE char8,              " 我是按钮啊
*   单元格可编辑、按钮
    field_style TYPE  lvc_t_styl,        "控制字段可编辑、按钮的参数
  END OF ty_alv,
  tt_alv TYPE STANDARD TABLE OF ty_alv.

*&---------------------------------------------------------------------*
* CONSTANTS
*&---------------------------------------------------------------------*
CONSTANTS:
  BEGIN OF c_ucomm,
    action01 TYPE sy-ucomm VALUE '&ZALL',text01 TYPE text VALUE '全选    ',
    action02 TYPE sy-ucomm VALUE '&ZSAL',text02 TYPE text VALUE '取消全选',
  END OF c_ucomm,

  BEGIN OF c_fcat,
    name01 TYPE fieldname VALUE 'CARRID     ',text01 TYPE fieldtext VALUE '航线代码',
    name02 TYPE fieldname VALUE 'FLDATE     ',text02 TYPE fieldtext VALUE '航班日期',
    name03 TYPE fieldname VALUE 'PRICE      ',text03 TYPE fieldtext VALUE '航空运费',
    name04 TYPE fieldname VALUE 'PLANETYPE  ',text04 TYPE fieldtext VALUE '飞机类型',
    name05 TYPE fieldname VALUE 'PAYMENTSUM ',text05 TYPE fieldtext VALUE '当前预定总数',
    name06 TYPE fieldname VALUE 'BUTEN      ',text06 TYPE fieldtext VALUE '我是按钮啊',
  END OF c_fcat.

*&---------------------------------------------------------------------*
* DEFIEN ALV
*&---------------------------------------------------------------------*
DATA:go_container TYPE REF TO cl_gui_docking_container,
     go_grid      TYPE REF TO cl_gui_alv_grid,
     gt_exclude   TYPE ui_functions,
     gt_fieldcat  TYPE lvc_t_fcat,
     gs_layout    TYPE lvc_s_layo,
     gs_variant   TYPE disvariant,
     gv_save,
     ok_code      TYPE sy-ucomm,
     save_code    TYPE sy-ucomm.

*&---------------------------------------------------------------------*
* GLOBAL INTERNAL TABLES
*&---------------------------------------------------------------------*
DATA:
  gt_out TYPE tt_alv.

*&---------------------------------------------------------------------*
* GLOBAL MACROS
*&---------------------------------------------------------------------*
DEFINE macro_fcat.
  ls_fieldcat-fieldname = &1.
  ls_fieldcat-reptext   = &2.
  APPEND ls_fieldcat TO ct_fieldcat.
  CLEAR ls_fieldcat.
END-OF-DEFINITION.

*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS:
*     BUTTEN单元格
      handle_butten_click FOR EVENT button_click OF cl_gui_alv_grid
        IMPORTING
          es_col_id
          es_row_no.
ENDCLASS.
*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_HANDLER IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
* BUTTEN 单元格
  METHOD handle_butten_click.
    PERFORM frm_butten_click USING es_col_id es_row_no.
  ENDMETHOD.
ENDCLASS.

*&---------------------------------------------------------------------*
* SELECTION-SCREEN
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS:
  s_carrid FOR sflight-carrid.
SELECTION-SCREEN END OF BLOCK b01.

*&----------------------------------------------------------------------
*& INITIALIZATION.
*&----------------------------------------------------------------------
INITIALIZATION.
* 初始化
  PERFORM frm_initial_data.

*&----------------------------------------------------------------------
*& AT SELECTION-SCREEN OUTPUT
*&----------------------------------------------------------------------
AT SELECTION-SCREEN OUTPUT.

*&----------------------------------------------------------------------
*& AT SELECTION-SCREEN
*&----------------------------------------------------------------------
AT SELECTION-SCREEN.

*&----------------------------------------------------------------------
*& START-OF-SELECTION
*&----------------------------------------------------------------------
START-OF-SELECTION.
* 数据处理
  PERFORM frm_get_data.
  PERFORM frm_show_alv.

*&---------------------------------------------------------------------*
*& Form FRM_INITIAL_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_initial_data .

  CLEAR gt_out.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data ." 747-400   PLANETYPE

  DATA:ls_stylelin TYPE lvc_s_styl.

  SELECT
    carrid,
    fldate,
    price,
    planetype,
    paymentsum
    FROM sflight
    WHERE carrid IN @s_carrid
    INTO CORRESPONDING FIELDS OF TABLE @gt_out
    UP TO 20 ROWS.

  LOOP AT gt_out ASSIGNING FIELD-SYMBOL(<fs_out>).

*   赋值按钮图标
    IF <fs_out>-planetype = '747-400'.
      <fs_out>-buten = icon_checked.
    ELSE.
      <fs_out>-buten = icon_incomplete.
    ENDIF.

*   把单元格变为按钮
    IF <fs_out>-field_style IS INITIAL.
      ls_stylelin-fieldname = 'BUTEN'.                        " 需要编辑的列名
      ls_stylelin-style = cl_gui_alv_grid=>mc_style_button.   " 设置为BUTEN
      APPEND ls_stylelin TO <fs_out>-field_style.
      CLEAR ls_stylelin.
    ENDIF.

  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SHOW_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_show_alv .

  CALL SCREEN 9000.

ENDFORM.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.

  DATA:lv_title TYPE string.
  CLEAR lv_title.

  lv_title = 'ABAP ALV行单元格按钮'.

  SET PF-STATUS '9000'.               " 定义gui状态
  SET TITLEBAR '9000'  WITH lv_title. " 定义gui标题

ENDMODULE.
*&---------------------------------------------------------------------*
*& Module ALV_DES OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE alv_des OUTPUT.

  DATA:lt_sort TYPE lvc_t_sort.

  IF go_grid IS INITIAL .
    CREATE OBJECT go_container
      EXPORTING
        repid     = sy-repid
        dynnr     = '9000'
        extension = 2050
        side      = cl_gui_docking_container=>property_floating.
    CREATE OBJECT go_grid
      EXPORTING
        i_parent = go_container.

*   获取字段目录
    PERFORM prepare_field_catalog CHANGING gt_fieldcat .

*   获取样式
    PERFORM prepare_layout CHANGING gs_layout .

*   响应事件
    PERFORM eventload.

*   锚定ALV变式标记
    CLEAR gs_variant.
    gs_variant = sy-repid.
    gv_save = 'A'.
    gs_variant-report = |{ sy-repid }{ sy-dynnr }|.

*-----显示ALV-------------------------------------------
    PERFORM frm_alv_display USING gt_out.
  ELSE .
*----------------------------刷新ALV----------------------
    " 刷新ALV
    PERFORM frm_refresh_alv.
  ENDIF .

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Form  PREPARE_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PS_LAYOUT  text
*----------------------------------------------------------------------*
FORM prepare_layout
  CHANGING ps_layout TYPE lvc_s_layo.

  ps_layout-cwidth_opt      = 'X'.               " 优化行宽度
  ps_layout-zebra           = 'X'.               " 斑马纹
  ps_layout-sel_mode        = 'D'.               " 选择方式
  ps_layout-box_fname       = 'SEL'.             " 指定行选择字段
  ps_layout-stylefname      = 'FIELD_STYLE'.     " 单元格BUTEN

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ALV_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> GT_OUT
*&---------------------------------------------------------------------*
FORM frm_alv_display
  USING pt_table.

  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout            = gs_layout
      it_toolbar_excluding = gt_exclude
      is_variant           = gs_variant
      i_save               = gv_save
    CHANGING
      it_outtab            = pt_table
      it_fieldcatalog      = gt_fieldcat.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_REFRESH_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_refresh_alv .

  DATA:ls_stable TYPE lvc_s_stbl.
  "刷新ALV
  ls_stable-row = 'X'.
  ls_stable-col = 'X'.
  CALL METHOD go_grid->refresh_table_display
    EXPORTING
      is_stable = ls_stable.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
  save_code = ok_code.
  CLEAR ok_code.
  CASE save_code.
*   定义返回按钮
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
    WHEN 'EXIT' OR 'EXIT2'.
      LEAVE PROGRAM.
    WHEN 'SAVE'.
    WHEN OTHERS.
      CALL METHOD go_grid->set_function_code
        CHANGING
          c_ucomm = save_code.
  ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Form PREPARE_FIELD_CATALOG
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- GT_FIELDCAT
*&---------------------------------------------------------------------*
FORM prepare_field_catalog
  CHANGING ct_fieldcat TYPE lvc_t_fcat.

  DATA:
    ls_fieldcat TYPE lvc_s_fcat.

  macro_fcat:
    c_fcat-name01 c_fcat-text01,
    c_fcat-name02 c_fcat-text02,
    c_fcat-name03 c_fcat-text03,
    c_fcat-name04 c_fcat-text04,
    c_fcat-name05 c_fcat-text05,
    c_fcat-name06 c_fcat-text06.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_BUTTEN_CLICK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> ES_COL_ID
*&      --> ES_ROW_NO
*&---------------------------------------------------------------------*
FORM frm_butten_click
  USING ps_col_id TYPE lvc_s_col
        ps_row_no TYPE lvc_s_roid.

  IF ps_col_id-fieldname = 'BUTEN'.
    READ TABLE gt_out INTO DATA(ls_out) INDEX ps_row_no-row_id.
    IF sy-subrc = 0.
      IF ls_out-buten = icon_checked.
        MESSAGE |选中的为第{ ps_row_no-row_id }行!此数据审批成功!| TYPE 'I'.
      ELSE.
        MESSAGE |选中的为第{ ps_row_no-row_id }行!此数据驳回成功!| TYPE 'I'.
      ENDIF.
    ENDIF.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form EVENTLOAD
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM eventload .

  DATA: gr_event_handler TYPE REF TO lcl_event_handler .  "事件响应
  CREATE OBJECT gr_event_handler.
  SET HANDLER gr_event_handler->handle_butten_click FOR go_grid.

ENDFORM.

2.效果如下

效果:

总结

ALV单元格动态可编辑的一个扩展,希望能帮助需要的人!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值