ABAP ALV 单元格按钮

本文转自:https://blog.csdn.net/JZYDDFZJ/article/details/132101267

一、介绍
具体方法如下:

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.代码展示
代码如下(示例):

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
*&---------------------------------------------------------------------*
*& "Set the Button using the staus
*& "Copy the Standard status from the program SAPLKKBL status
*& "STANDARD using SE41, and use that here.
*& "Pf status
*&---------------------------------------------------------------------*
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=>dock_at_left.
    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 OTHERS.
      LEAVE TO SCREEN 0.
  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.

需要新建9000屏幕,代码如下

PROCESS BEFORE OUTPUT.
  MODULE STATUS_9000.
  MODULE alv_des.
*
PROCESS AFTER INPUT.
  MODULE USER_COMMAND_9000.

 

效果:

 

posted on 2023-08-07 14:09  seven1314pp  阅读(618)  评论(0编辑  收藏  举报