写点什么

ABAP-OOALV 实现

  • 2022 年 7 月 18 日
  • 本文字数:11829 字

    阅读完需:约 39 分钟

相关类

  • CL_GUI_ALV_GRID

  • CL_GUI_CUSTOM_CONTAINER

  • CL_GUI_DOCKING_CONTAINER

  • CL_GUI_SPLITTER_CONTAINER


控制区域、容器、Grid 关系

先在屏幕上绘制一个用户自定义控件区域,然后该用户以自定义控件区域为基础来创建 CL_GUI_CUSTOM_CONTAINER 容器实例,最后以此容器实例来创建 CL_GUI_ALV_GRID 实例


CL_GUI_ALV_GRID 重要方法

SET_TABLE_FOR_FIRST_DISPLAY

  • ALV 展示方法

  • 参数:

  • IS_VARIANT

  • 确定用于显示输出表的布局。如果使用此参数,则必须至少填充类型结构的“REPORT”字段。

    I_SAVE

  • 决定用户是否可以保存变式:

    'X' 只能保存全局变式; 

    'U' 只能保存特定变式; 

    'A' 都可以保存; 

    SPACE 不可以将布局保存为变式

    I_DEFAULT

  • 此参数确定是否允许用户定义默认布局:

    "X":允许默认布局(默认设置)

    空格:不允许默认布局

    如果允许默认布局,并且存在这样的布局,并且 IS_VARIANT 中未指定其他布局,则在调用此方法时会自动加载默认布局。

  • IS_LAYOUT

  • 确定网格控件的属性。布局结构与用于保存筛选器、排序和列属性的布局无关

  • 字段:

  • CWIDTH_OPT:最优化宽度 SPACE, 'X'

  • GRID_TITLE:标题,在网格和工具条之间 最长 70 个字符

  • NO_TOOLBAR:隐藏工具条 SPACE, 'X'

  • NO_VGRIDLN:隐藏垂直线 SPACE, 'X'

  • SEL_MODE:选择模式 SPACE, 'A', 'B', 'C', 'D'

  • CTAB_FNAME:带有复杂单元格颜色编码的字段名称 最长 30 个字符

  • INFO_FNAME:带有简单行彩色代码的字段名称 最长 30 个字符

  • ZEBRA:可选行颜色,如果设置了,出现了间隔色带 SPACE, 'X'

  • STYLEFNAME:字段格式

  • IT_OUTTAB

  • 要显示的数据的输出表

    IT_FIELDCATALOG

  • 确定输出表的结构和要显示的数据的格式

  • 字段:

  • COL_POS:输出列 列的位置,第几列,例如 1,2,…

  • FIELDNAME:字段名称

  • CURRENCY/CFIELDNAME:货币单位/参考的当前单位的字段名称

  • QUANTITY/QFIELDNAME:计量单位/参考计量单位的字段名称

  • DO_SUM:总计列值 'X',合计

  • FIX_COLUMN:固定列

  • EMPHASIZE:列的颜色

  • NO_OUT:列没有输出'X' ,隐藏此列

  • OUTPUTLEN:列的字符宽度

  • DATATYPE:ABAP 字典中的数据类型

  • INTTYPE:ABAP 数据类型(C,D,N,...)

  • HOTSPOT:单击敏感'X',下面出现下划线,响应单击

  • SCRTEXT_L/M/S:字段标签长/中/短

  • REF_TABLE/REF_FIELD:参考表/参考字段

  • EDIT:是否可编辑

  • IT_SORT

  • 具有最初要排序的列的排序属性的表

  • IT_FILTER

  • 具有最初要为其设置筛选器的列的筛选器属性的表

REFRESH_TABLE_DISPLAY

  • IS_STABLE:刷新的稳定性,就是滚动条保持不动

    I_SOFT_REFRESH:软刷新,如果设置了这个参数,临时给 ALV 创建的合计、排序、数据过滤都将保持不变。这个是非常有意义的,例如:当你没有修改数据内表里的数据,但因布局修改了想刷新 ALV 时可使用

  • SET_FRONTED_FIELDCATALOG:如果 fieldcat 格式有修改,需要刷新格式设置,则调用这个方法


EVENTS

TOOLBAR 事件

  • 定义事件类

CLASS lcl_event_receiver DEFINITION.	PUBLIC SECTION.  	CLASS-METHODS:			handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid "自定义工具栏                     IMPORTING e_object e_interactive.ENDCLASS.
复制代码
  • 实现 handle_toolbar 方法

CLASS lcl_event_receiver IMPLEMENTATION.  METHOD handle_toolbar.    DATA: ls_toolbar  TYPE stb_button.    MOVE '' TO ls_toolbar-function.    MOVE '' TO ls_toolbar-icon.    MOVE '3' TO ls_toolbar-butn_type.    APPEND ls_toolbar TO e_object->mt_toolbar.
CLEAR ls_toolbar. MOVE 'SELA' TO ls_toolbar-function. MOVE icon_select_all TO ls_toolbar-icon. MOVE '全选' TO ls_toolbar-quickinfo. MOVE ''(112) TO ls_toolbar-text. APPEND ls_toolbar TO e_object->mt_toolbar.
CLEAR ls_toolbar. MOVE 'DSEL' TO ls_toolbar-function. MOVE icon_deselect_all TO ls_toolbar-icon. MOVE '取消全选' TO ls_toolbar-quickinfo. MOVE ''(112) TO ls_toolbar-text. APPEND ls_toolbar TO e_object->mt_toolbar. CLEAR ls_toolbar.
MOVE 'B_LIST' TO ls_toolbar-function. MOVE 1 TO ls_toolbar-butn_type. MOVE icon_calculation TO ls_toolbar-icon. MOVE '自定义下拉菜单按钮'(202) TO ls_toolbar-quickinfo. MOVE '下拉菜单按钮' TO ls_toolbar-text. "MOVE '' TO ls_toolbar-disabled. APPEND ls_toolbar TO e_object->mt_toolbar. ENDMETHOD. "handle_toolbarENDCLASS.
复制代码



MENU_BUTTON 事件

  • 定义事件类

CLASS lcl_event_receiver DEFINITION.	PUBLIC SECTION.  	CLASS-METHODS:			handle_menu_button        "自定义菜单                    FOR EVENT menu_button OF cl_gui_alv_grid        IMPORTING e_object e_ucomm.ENDCLASS.
复制代码
  • 实现 handle_menu_button 方法

CLASS lcl_event_receiver IMPLEMENTATION.  METHOD handle_menu_button .  "自定义菜单实现方法    IF e_ucomm = 'B_LIST'."给下拉菜单按钮增加选项,可以多次调用该方法以增加多行      CALL METHOD e_object->add_function        EXPORTING          icon  = ICON_OKAY          fcode = 'B_SUM' "字菜单按钮的FunCode          text  = '显示ALV总行数'.      CALL METHOD e_object->add_function        EXPORTING          icon  = ICON_CANCEL          fcode = 'C_SUM' "字菜单按钮的FunCode          text  = '测试'.    ENDIF.  ENDMETHOD.                    "handle_menu_buttonENDCLASS.
复制代码



USER_COMMAND 事件

  • 定义方法

CLASS lcl_event_receiver DEFINITION.	PUBLIC SECTION.  	CLASS-METHODS:			 handle_user_command       "自定义按钮事件                    FOR EVENT user_command OF cl_gui_alv_grid       IMPORTING e_ucomm.ENDCLASS.
复制代码
  • 实现 handle_user_command 方法

CLASS lcl_event_receiver IMPLEMENTATION.  METHOD handle_user_command.  "自定义按钮实现方法    DATA: sum TYPE i .    CASE e_ucomm.      WHEN 'SELA'.        LOOP AT it_9001 INTO wa_9001.          wa_9001-check = 'X'.          MODIFY it_9001 INDEX sy-tabix FROM wa_9001 TRANSPORTING check.        ENDLOOP.        CALL METHOD oo_grid->refresh_table_display"这句话一定要写,否则界面没刷新,不生效          EXPORTING            is_stable = wa_stbl.      WHEN 'DSEL'.        LOOP AT it_9001 INTO wa_9001.          wa_9001-check = ''.          MODIFY it_9001 INDEX sy-tabix FROM wa_9001 TRANSPORTING check.        ENDLOOP.        CALL METHOD oo_grid->refresh_table_display          EXPORTING            is_stable = wa_stbl.      WHEN 'B_SUM'.        DESCRIBE TABLE it_9001[] LINES sum.        MESSAGE i001(00) WITH '当前ALV表格中的数据总行数为:' sum.       WHEN 'C_SUM'.        MESSAGE i001(00) WITH '测试为:' sum.    ENDCASE.  ENDMETHOD.ENDCLASS.
复制代码



HOTSPOT(单点)事件

  • 定义方法

CLASS lcl_event_receiver DEFINITION.  PUBLIC SECTION.    CLASS-METHODS:      handle_hotspot_click1     "单击事件                    FOR EVENT hotspot_click OF cl_gui_alv_grid        IMPORTING e_row_id e_column_id.ENDCLASS.
复制代码
  • 实现 handle_hotspot_click1 方法

CLASS lcl_event_receiver IMPLEMENTATION.  METHOD handle_data_changed.  "数据变化实现方法    PERFORM handle_data_changed USING er_data_changed.  ENDMETHOD.
METHOD handle_hotspot_click1. "单击事件实现方法 READ TABLE it_9001 INTO wa_9001 INDEX e_row_id-index. CASE e_column_id-fieldname. WHEN 'VBELN_VF'. SET PARAMETER ID 'VF' FIELD wa_9001-vbeln_vf. CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN. WHEN 'VBELN_SO'. SET PARAMETER ID 'AUN' FIELD wa_9001-vbeln_so. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. ENDCASE. ENDMETHOD. "HANDLE_HOTSPOT_CLICK1ENDCLASS.
复制代码



DATA_CHANGE 事件

  • 定义方法

CLASS lcl_event_receiver DEFINITION.  PUBLIC SECTION.    CLASS-METHODS:      handle_data_changed       "数据改变触发                    FOR EVENT data_changed OF cl_gui_alv_grid        IMPORTING er_data_changed.ENDCLASS.
复制代码
  • 实现方法

CLASS lcl_event_receiver IMPLEMENTATION.  METHOD handle_data_changed.  "数据变化实现方法  	"PERFORM handle_data_changed USING er_data_changed.    DATA: x_change TYPE lvc_s_modi.    LOOP AT data_changed->mt_good_cells INTO x_change.    "结算数量发生变化      IF x_change-fieldname  = 'FKLMG_JS'.        READ TABLE it_9001 INTO wa_9001 INDEX x_change-row_id.        wa_9001-fklmg_js = x_change-value.        wa_9001-kbetr_tmp = wa_9001-kbetr_js * wa_9001-fklmg_js / wa_9001-kpein / '1.13'.        MODIFY it_9001 FROM wa_9001 INDEX x_change-row_id.      ENDIF.      "行备注内容发生变化      IF x_change-fieldname  = 'ZREMARK'.        READ TABLE it_9001 INTO wa_9001 INDEX x_change-row_id.        wa_9001-zremark = x_change-value.        MODIFY it_9001 FROM wa_9001 INDEX x_change-row_id.      ENDIF.      CALL METHOD oo_grid->refresh_table_display        EXCEPTIONS          finished = 1          OTHERS   = 2.    ENDLOOP.  ENDMETHOD.ENDCLASS.
复制代码



F4 搜索帮助事件

  • 如果希望 ALV 中某字段具有搜索帮助,第一种办法当然是对表中某字段的引用,设置 ref_table、ref_field,将自动触发该字段所带的搜索帮助。

  • 第二种办法就是利用自定义代码来实现 ALV 的搜索帮助,显然它的功能更强大、更灵活。针对在 OO ALV 中实现搜索帮助,其主要步骤有:

  • 定义方法

CLASS lcl_event_receiver DEFINITION.  PUBLIC SECTION.    CLASS-METHODS:      handle_onf4 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_onf4.  	DATA: ls_modi TYPE lvc_s_modi,          lt_ret_tab TYPE TABLE OF ddshretval WITH HEADER LINE.     FIELD-SYMBOLS <modtab> TYPE lvc_t_modi.    IF e_fieldname = 'FIELD_NAME'. "我们自定义搜索的字段名      READ TABLE gt_alv_data INDEX es_row_no-row_id.      CHECK sy-subrc = 0.**  这里可以添加代码以对lt_hitlist内表进行填充      CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'        EXPORTING          retfield        = 'HIT_FIELD'          value_org       = 'S'        TABLES          value_tab       = lt_hitlist          return_tab      = lt_ret_tab        EXCEPTIONS          parameter_error = 1          no_values_found = 2          OTHERS          = 3.      IF sy-subrc = 0.**  Update the value in ALV cell        READ TABLE lt_ret_tab INDEX 1.        IF sy-subrc = 0. " User didn't cancel          ls_modi-row_id = es_row_no-row_id.          ls_modi-fieldname = e_fieldname.          ls_modi-value = lt_ret_tab-fieldval.          ASSIGN er_event_data->m_data->* TO <modtab>.          APPEND ls_modi TO <modtab>.        ENDIF.      ENDIF.**  Inform ALV Grid that event 'onf4' has been processed      er_event_data->m_event_handled = 'X'.    ENDIF.  ENDMETHOD.ENDCLASS.
复制代码
  • 在 Method 的最后,记得加上 er_event_data->m_event_handled = 'X'. 通知系统搜索事件处理完毕,这样就不会调用系统标准的 Search Help。

  • 需要自定义搜索帮助的字段,设置其 field catalog 时:

ls_fieldcat-f4availabl = 'X'.
复制代码
  • 在创建 ALV 对象之后,要对需要自定义搜索帮助的字段进行注册。lvc_s_f4 中的字段 getbefore 和 changeafter 应该代表是否触发 data_changed 事件。

DATA: lt_f4 TYPE lvc_t_f4 WITH HEADER LINE.CLEAR lt_f4.lt_f4-fieldname = 'FIELD_NAME'.lt_f4-register = 'X'.lt_f4-chngeafter = 'X'. APPEND lt_f4.CALL METHOD mygrid->register_f4_for_fields  EXPORTING    it_f4 = lt_f4[].CREATE OBJECT go_evt_receiver.SET HANDLER go_evt_receiver->handle_onf4 FOR go_alv_grid.
复制代码



EVENTS 注册

"屏幕输出前MODULE status_9001 OUTPUT.
"设置GUI状态 SET PF-STATUS '9001_PF'. "设置界面标题 SET TITLEBAR '9001_BAR'.
IF oo_container IS INITIAL.* 建立一个容器控件作为ALV的容器 CREATE OBJECT oo_container EXPORTING container_name = oo_cont_on_9001.* 建立一个ALV的实例 CREATE OBJECT oo_grid EXPORTING i_parent = oo_container.
* 显示表格 CALL METHOD oo_grid->set_table_for_first_display EXPORTING is_layout = oo_layout CHANGING it_outtab = it_9001[] it_fieldcatalog = oo_field.
* 注册ALV的事件 DATA: lt_f4 TYPE lvc_t_f4 WITH HEADER LINE. CLEAR lt_f4. lt_f4-fieldname = 'FIELD_NAME'. lt_f4-register = 'X'. lt_f4-chngeafter = 'X'. APPEND lt_f4. CALL METHOD oo_grid->register_f4_for_fields EXPORTING it_f4 = lt_f4[]. CREATE OBJECT event_receiver.
SET HANDLER event_receiver->handle_toolbar FOR oo_grid. "注册工具栏 SET HANDLER event_receiver->handle_menu_button FOR oo_grid. "注册工具栏菜单 SET HANDLER event_receiver->handle_user_command FOR oo_grid. "注册用户自定义命令 SET HANDLER event_receiver->handle_data_changed FOR oo_grid. "单元格数据更改时触发 SET HANDLER event_receiver->handle_hotspot_click1 FOR oo_grid."单击事件 SET HANDLER event_receiver->handle_onf4 FOR go_alv_grid. "搜索帮助事件

"调用此方法才能激活工具栏上增加的自定义按钮 CALL METHOD oo_grid->set_toolbar_interactive.
CALL METHOD oo_grid->register_edit_event "单元格数据更改时触发条件 EXPORTING* I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER. "回车时触发 i_event_id = cl_gui_alv_grid=>mc_evt_modified. "单元格失去焦点触发 ENDIF. CALL METHOD oo_grid->refresh_table_display.ENDMODULE.
复制代码



实现可编辑

实现列可编辑

  • 只要将 catalog 中的字段 edit 设置成‘X',整列就变成能输入的了,LVC_S_FCAT-EDIT

REPORT ztest_alv_lvc_edit.
TYPES:BEGIN OF gty_ekko, ebeln TYPE ekko-ebeln, verkf TYPE ekko-verkf, END OF gty_ekko.DATA:git_ekko TYPE STANDARD TABLE OF gty_ekko.DATA:git_fcat TYPE lvc_t_fcat, gwa_fcat LIKE LINE OF git_fcat, gwa_layout TYPE lvc_s_layo.DATA:l_pos TYPE i VALUE 1.
START-OF-SELECTION.
SELECT ebeln verkf INTO TABLE git_ekko FROM ekko UP TO 10 ROWS.
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-edit = 'X'. "设置列为可编辑状态 gwa_fcat-outputlen = '20'. APPEND gwa_fcat TO git_fcat.
gwa_layout-zebra = 'X'. gwa_layout-sel_mode = 'A'. gwa_layout-cwidth_opt = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid is_layout_lvc = gwa_layout it_fieldcat_lvc = git_fcat TABLES t_outtab = git_ekko[] EXCEPTIONS program_error = 1 OTHERS = 2. IF sy-subrc = 0.* BREAK-POINT. ENDIF.
复制代码



实现单元格可编辑

  • 单元格可编辑状态设置的主要思想:首先通过 EIDT 参数设置列为可编辑状态;其次对输出内表进行循环将不需要编辑的行设置为不可编辑状态,如此单元格的可编辑属性设置完毕。下面粘贴简要代码。

TYPE-POOLS: SLIS.*- FieldcatalogDATA: IT_FIELDCAT TYPE LVC_T_FCAT.DATA: X_FIELDCAT TYPE LVC_S_FCAT.DATA: X_LAYOUT TYPE LVC_S_LAYO.
"第1步:用操作具体单元的是否可编辑的内表和工作区
DATA: LS_EDIT TYPE LVC_S_STYL, LT_EDIT TYPE LVC_T_STYL."第2步:在内表定义添加字段,用于控制具体行的具体单元是否可编辑DATA: BEGIN OF IT_VBAP OCCURS 0, VBELN LIKE VBAP-VBELN, POSNR LIKE VBAP-POSNR, STYLE TYPE LVC_T_STYL, "FOR DISABLEEND OF IT_VBAP.DATA: LS_OUTTAB LIKE LINE OF IT_VBAP.SELECT VBELN POSNR UP TO 100 ROWS INTO CORRESPONDING FIELDS OF TABLE IT_VBAPFROM VBAP.
DATA:L_POS TYPE I VALUE 1.CLEAR: L_POS.L_POS = L_POS + 1.X_FIELDCAT-SELTEXT = 'VBELN'.X_FIELDCAT-FIELDNAME = 'VBELN'.X_FIELDCAT-TABNAME = 'ITAB'.X_FIELDCAT-COL_POS = L_POS.X_FIELDCAT-EDIT = 'X'.X_FIELDCAT-OUTPUTLEN = '10'.x_fieldcat-ref_field = 'VBELN'.x_fieldcat-ref_table = 'VBAK'.APPEND X_FIELDCAT TO IT_FIELDCAT.
CLEAR X_FIELDCAT.L_POS = L_POS + 1.X_FIELDCAT-SELTEXT = 'POSNR'.X_FIELDCAT-FIELDNAME = 'POSNR'.X_FIELDCAT-TABNAME = 'ITAB'.X_FIELDCAT-COL_POS = L_POS.X_FIELDCAT-EDIT = 'X'.X_FIELDCAT-OUTPUTLEN = '5'.APPEND X_FIELDCAT TO IT_FIELDCAT.CLEAR X_FIELDCAT.L_POS = L_POS + 1.
"第3步:设置第六行两个单元都不能输入SY-TABIX = 6.LS_EDIT-FIELDNAME = 'VBELN'.LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.LS_EDIT-STYLE2 = SPACE.LS_EDIT-STYLE3 = SPACE.LS_EDIT-STYLE4 = SPACE.LS_EDIT-MAXLEN = 10.INSERT LS_EDIT INTO TABLE LT_EDIT.
LS_EDIT-FIELDNAME = 'POSNR'.LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.LS_EDIT-STYLE2 = SPACE.LS_EDIT-STYLE3 = SPACE.LS_EDIT-STYLE4 = SPACE.LS_EDIT-MAXLEN = 6.INSERT LS_EDIT INTO TABLE LT_EDIT.
INSERT LINES OF LT_EDIT INTO TABLE LS_OUTTAB-STYLE.
"第4步:将控制数据写到内表MODIFY IT_VBAP INDEX SY-TABIX FROM LS_OUTTAB TRANSPORTING STYLE ."设置第10行只有项目不能输入clear LS_OUTTAB.refresh LT_EDIT.LS_EDIT-FIELDNAME = 'POSNR'.LS_EDIT-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.LS_EDIT-STYLE2 = SPACE.LS_EDIT-STYLE3 = SPACE.LS_EDIT-STYLE4 = SPACE.LS_EDIT-MAXLEN = 6.INSERT LS_EDIT INTO TABLE LT_EDIT.INSERT LINES OF LT_EDIT INTO TABLE LS_OUTTAB-STYLE.SY-TABIX = 10.
"将控制数据写到内表MODIFY IT_VBAP INDEX SY-TABIX FROM LS_OUTTAB TRANSPORTING STYLE .
"第5步:设置控制字段X_LAYOUT-STYLEFNAME = 'STYLE'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING I_CALLBACK_PROGRAM = SY-REPID IS_LAYOUT_LVC = X_LAYOUT IT_FIELDCAT_LVC = IT_FIELDCAT TABLES T_OUTTAB = IT_VBAP[] EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2.IF SY-SUBRC NE 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.ENDIF.
复制代码



ALV 的颜色设定(单元格,行,列的颜色设定)

在 ABAP 开发过程中,有的时候,我们需要在 ALV 网格上绘上一些颜色.可以给特定的行,某个特定的列,某个特定的单元格绘制颜色.

如果某列被设置为关键列,这列的颜色将被自动绘制,而不需要我们额外的指定.,ALV 的颜色是四位的如 C710,第一位是固定死的 C,第二位是颜色代码,可能值为 1-7,代表颜色如下:

1:Gray-blue —>headers

2:Light gray —>List bodies

3:yellow —>totals

4:Blue-green—>Key columns

5:green —>Positive threshold value

6:red —>Negative threshold value

7:orange —>Control levels

后两位是 1 和 0 的组合,10 则代表颜色用在背景,01 代表颜色用在前景。

列颜色设定

  • 列的颜色很简单,在构成 ALV 字段的 FIELDCAT 内表中有一个字段是 EMPHASIZE,将一个 char 型 4 位的颜色代码分配到 FIELDCAT 内表这个字段即可。

LW_FCAT-EMPHASIZE = 'C710'. "设置字段的颜色 
复制代码



行颜色设定

  • 为某行设置颜色,是有点复杂的,我们需要在要显示的数据内表中增加一个字段,这个字段不需要在字段目录中存在.同样,这个字段也是 4 位的 CHAR 型,符合颜色编码的定义.,然后在布局设定中指定该列为颜色列,gs_layout-info_fname = ‘COLOR’.或(info_fieldname)

FORM p_data .	LOOP AT gt_mara.		IF gt_mara-matnr = '100-100'.			gt_mara-color = 'C310'.		ENDIF.		MODIFY gt_mara.	ENDLOOP.ENDFORM.  gs_layout-info_fname = 'COLOR'
复制代码



单元格颜色设定

  • 设置单元格和设置行的颜色,本质上没有什么大的区别,但是定位单元格需要 2 个参数.我们需要在数据内表中插入一个表类型的字段,这样我们的数据内表就变成了 DEEP 结构了,不过 ALV 是可以处理的.不需要担心.

  • 插入的这个表类型的类型为”LVC_T_SCOL”.里面有 3 个参数:

  • FNAME 告诉我们你需要设置的是哪个字段,如果为空,然后直接在 COLOR 中设置颜色,就是整行设置为这个颜色.如果具体到某个单元格,必须指定是哪个字段.

  • COLOR 字段是用来设置颜色的.

  • NOKEYCOL 字段比较关键了.设置为关键列的一些字段,我们的颜色设置可能被覆盖.通过这个字段的设置,可以避免被关键列的颜色被覆盖.

  • 同样,ALV 在布局中有个字段”CTAB_FNAME”告诉我们,数据内表中,哪个字段是用来设置单元格的颜色的,类似于设置行颜色的 INTO_FNAME.

FORM p_data .	LOOP AT gt_mara.		IF gt_mara-matnr = '100-100'.			gt_mara-color = 'C310'."设置行的颜色		IF gt_mara-matnr = '100-401'.    	CLEAR gs_cellcolor.			gs_cellcolor-fname = 'MTART'.			gs_cellcolor-color-col = '6'.			APPEND gs_cellcolor TO gt_mara-cellclor."设置单元格的颜色			gs_cellcolor-fname = 'ERSDA'.			gs_cellcolor-color-col = '7'.			APPEND gs_cellcolor TO gt_mara-cellclor.		ENDIF.		MODIFY gt_mara.	ENDLOOP.ENDFORM.gs_layout-ctab_fname = 'COLOR'.
复制代码



下拉框设置

  • 设置下拉框数据

DATA: gs_dropdowm TYPE lvc_s_drop,      gt_dropdowm TYPE lvc_t_drop.gs_dropdowm-handle = '2'.gs_dropdowm-value  = 'drop-value1'.APPEND gs_dropdowm TO gt_dropdowm.
CLEAR: gs_dropdowm.gs_dropdowm-handle = '2'.gs_dropdowm-value = 'drop-value2'.APPEND gs_dropdowm TO gt_dropdowm.
复制代码
  • 设置 FieldCatelog 属性

gs_fieldcat-drdn_hndl = 2.
复制代码
  • 调用 ALV 显示方法之前设置下拉框

CALL METHOD g_grid01->set_drop_down_table  EXPORTING    it_drop_down = gt_dropdowm. "添加下拉框方法到ALV中顯示
" 调用ALV 显示方法CALL METHOD g_grid01->set_table_for_first_display EXPORTING* i_structure_name = 'SPFLI' " 参照表结构字段显示 is_layout = ls_layout " 布局 CHANGING it_fieldcatalog = gt_fieldcat " 显示字段 it_outtab = gt_data[] EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 3 others = 4.
复制代码



CL_GUI_DOCKING_CONTAINER

  • 使用 CL_GUI_DOCKING_CONTAINER 不需要提前在屏幕中绘制自定义控制区域

DATA: GS_DOCK TYPE REF TO CL_GUI_DOCKING_CONTAINER. "屏幕容器对象FORM FRM_SET_GRID .
"实例化屏幕容器 CREATE OBJECT GS_DOCK EXPORTING* PARENT = REPID = SY-REPID "当前程序 DYNNR = '0100' "屏幕编号 SIDE = CL_GUI_DOCKING_CONTAINER=>DOCK_AT_LEFT "容器吸附左侧 EXTENSION = 1300 "ALV的宽度* STYLE =* LIFETIME = lifetime_default* CAPTION = METRIC = 0* RATIO = 100 "ALV的比率,优先级高于上面的EXTENSION* NO_AUTODEF_PROGID_DYNNR =* NAME = EXCEPTIONS CNTL_ERROR = 1 CNTL_SYSTEM_ERROR = 2 CREATE_ERROR = 3 LIFETIME_ERROR = 4 LIFETIME_DYNPRO_DYNPRO_LINK = 5 OTHERS = 6. IF SY-SUBRC <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.

** CREATE OBJECT GS_CON* EXPORTING** PARENT =* CONTAINER_NAME = 'GC_CON'** STYLE =** LIFETIME = lifetime_default** REPID = SY-REPID** DYNNR = '0100'** NO_AUTODEF_PROGID_DYNNR =* EXCEPTIONS* CNTL_ERROR = 1* CNTL_SYSTEM_ERROR = 2* CREATE_ERROR = 3* LIFETIME_ERROR = 4* LIFETIME_DYNPRO_DYNPRO_LINK = 5* OTHERS = 6.* IF SY-SUBRC <> 0.** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.* ENDIF.

"实例化alv展示对象 CREATE OBJECT GS_ALV EXPORTING* I_SHELLSTYLE = 0* I_LIFETIME = I_PARENT = GS_DOCK* I_APPL_EVENTS = space* I_PARENTDBG =* I_APPLOGPARENT =* I_GRAPHICSPARENT =* I_NAME =* I_FCAT_COMPLETE = SPACE EXCEPTIONS ERROR_CNTL_CREATE = 1 ERROR_CNTL_INIT = 2 ERROR_CNTL_LINK = 3 ERROR_DP_CREATE = 4 OTHERS = 5. IF SY-SUBRC <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.ENDFORM.
复制代码



CL_GUI_SPLITTER_CONTAINER

  • 拆分 CL_GUI_CONTAINER 可以通过使用 CL_GUI_SPLITTER_CONTAINER 对象的 GET_CONTAINER 方法获取多个容器对象来实现。 

DATA: G_SPLITTER_CONTAINER_1 TYPE REF TO CL_GUI_SPLITTER_CONTAINER.DATA: MYCONTAINER_1 TYPE REF TO CL_GUI_CONTAINER.DATA: MYCONTAINER_2 TYPE REF TO CL_GUI_CONTAINER.  CREATE OBJECT G_SPLITTER_CONTAINER_1    EXPORTING      PARENT  = CL_GUI_CONTAINER=>SCREEN0      ROWS    = 2         "该方法中传入实参ROWS和COLUMNS的值决定你每行跟每列的容器数目      COLUMNS = 1.    CALL METHOD G_SPLITTER_CONTAINER_1->GET_CONTAINER    EXPORTING      ROW       = 1       "该方法中传入实参ROW和COLUMNS决定容器在屏幕中所处的相对位置      COLUMN    = 1    RECEIVING      CONTAINER = MYCONTAINER_1.    CALL METHOD G_SPLITTER_CONTAINER_1->GET_CONTAINER    EXPORTING      ROW      = 2      COLUMN   = 1    RECEIVING      CONTAINER = MYCONTAINER_2.
复制代码
  • 在容器中放 ALV 控件就实现了分屏显示多个 ALV 控件

DATA: G_GUI_ALV_1 TYPE REF TO CL_GUI_ALV_GRID.DATA: G_GUI_ALV_2 TYPE REF TO CL_GUI_ALV_GRID.CREATE OBJECT G_GUI_ALV_1  EXPORTING    I_PARENT = MYCONTAINER_1.  CREATE OBJECT G_GUI_ALV_2  EXPORTING    I_PARENT = MYCONTAINER_2.*—–省略给ALV展示所需参数赋值……
CALL METHOD G_GUI_ALV_1->SET_TABLE_FOR_FIRST_DISPLAY EXPORTING IS_LAYOUT = GTH_LAYOUT_UP CHANGING IT_OUTTAB = GT_MAT IT_FIELDCATALOG = GTD_FIELDCAT_UP.
复制代码


用户头像

还未添加个人签名 2022.06.08 加入

还未添加个人简介

评论

发布
暂无评论
ABAP-OOALV实现_SAP_桥下本有油菜花_InfoQ写作社区