首页 > 其他分享 >ABAP ALV 单元格按钮

ABAP ALV 单元格按钮

时间:2023-08-07 14:24:15浏览次数:45  
标签:&--------------------------------------------------------------------- text 

本文转自: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.

 

效果:

 

标签:&---------------------------------------------------------------------,text,
From: https://www.cnblogs.com/seven1314pp/p/17611309.html

相关文章

  • excel 合并单元格内容数据写入
    youcanusethiscodetoopen(test.xlsx)fileandmodifyA1cellandthensaveitwithanewnameimportopenpyxlxfile=openpyxl.load_workbook('test.xlsx')sheet=xfile.get_sheet_by_name('Sheet1')sheet['A1']='hel......
  • 使用Activate和Select方法选中单元格的异同
    尽管使用Activate方法和Select方法都能选中指定的单元格区域,但这两种方法并不完全相同。例如,选中A1:F5单元格区域后,再分别用两种方法选中B5单元格,我们可得: 选中单元格区域后,再使用Activate方法激活该区域里的一个单元格,该区域依然呈选中状态,只改变活动单元格为激活的单元格。如......
  • 7-3 ALV 报表之REUSE_ALV
    7-3ALV报表之REUSE_ALV【ZPGM_REUSE_ALV_DEMO】1ALV:ABAPLISTVIEWER、SAPLISTVIEWER早期、SAP社が提供されたレポート開発技術:WRITE2三つの段階:​ 1FUNCTION;​ 2CL_GUI_ALV_GRID;​ 3CL_SALVに関するクラス;3ALV構造:1LAYOUT:表示属性;​ 2FIELDCATOG:項目属性;​ 3VARIANT;​ ......
  • 关于union和合并单元格的区别
    选择与单元格A1中的值相等的所有单元格SubselectSameCells()DimgoalRangeAsRange,indexCellAsRangeSetgoalRange=Range("A1")ForEachindexCellInRange("A1:B5")IfindexCell.Value=Range("A1").ValueThen......
  • 本地启动 ABAP Platform Trial 的 Docker 命令行程序
    Docker是一个开源的容器化平台,用于轻松地构建、发布和运行应用程序。DockerDesktop是适用于Windows和Mac的Docker桌面应用程序,它允许用户在本地运行和管理容器化应用程序。在本文中,我们将详细介绍以下两个命令行并解释每个参数的含义:dockerpullsapse/abap-platform-trial:1909......
  • itextpdf5单元格中的段落没有行间距
    关于对表格中的段落没有行间距的解决方式:通过观察和推测的结论:itextpdf对一些属性,只会对最外层元素的属性进行接收处理,如行间距。例:Paragraphp=newParagraph(10,c);p.setLeading(p.getTotalLeading()+40);table.addCell(newParagraph(value+":",textFont));table.addCell(p......
  • 《Ext详解与实践》节选:自定义单元格的显示格式
    有时候,需要在Grid中显示一个链接,或根据数字的大小显示不同的颜色,又或者使用固定格式显示记录编号等,这就需要自定义单元格的显示格式。在Ext的Grid可以很容易的实现自定义单元格的显示格式。在定义Grid的ColumnModel的时候,在需要自定义显示格式的列中加入参数renderer,该参数指向一个......
  • Gin框架dgrijalva/jwt-go实例(JWT用户认证)
    1.什么是JWTJWT(JSONWebToken)是一个非常轻巧的规范,这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息,一个JWT由三部分组成,Header头部,Claims载荷,Signature签名JWT原理类似我们加盖公章或手写签名的的过程,合同上写了很多条款,不是随便一张纸随便写啥都可以的,......
  • 著名开源Linux图形驱动开发者与Valve签约
    据报道,Valve最近聘用了著名开源 Linux 图形驱动开发者AlyssaRosenzweig,以改进开源Linux图形驱动程序堆栈,增强Linux游戏生态系统。据报道,Valve最近聘用了著名开源Linux图形驱动开发者AlyssaRosenzweig,以改进开源Linux图形驱动程序堆栈,增强Linux游戏生态系......
  • 著名开源Linux图形驱动开发者与Valve签约
    据报道,Valve最近聘用了著名开源 Linux 图形驱动开发者AlyssaRosenzweig,以改进开源Linux图形驱动程序堆栈,增强Linux游戏生态系统。据报道,Valve最近聘用了著名开源Linux图形驱动开发者AlyssaRosenzweig,以改进开源Linux图形驱动程序堆栈,增强Linux游戏生态系......