一、Function ALV
实现方式 :调用标准函数
基本知识点:
- 类型池:SLIS
- Fieldcat TYPE slis_t_fieldcat_alv :列格式设置(表单),字段名称,列是否可修改,列宽度等
- Layout TYPE slis_layout_alv,全局格式设置(结构),整表字段是否可修改,是否以斑马纹输出,是否显示选择按钮字段等
- 函数:REUSE_ALV_FIELDCATALOG_MERGE ,根据内表结构返回FIELDCAT字段结构信息函数:REUSE_ALV_GRID_DISPLAY / REUSE_ALV_LIST_DISPLAY,使用GRID/LIST模式输出ALV报表
Fieldcat
字段 | 说明 |
KEY | 将该列设置为关键字(’X’/’’) |
ICON | 以图标显示(’X’/’’) |
CHECKBOX | 以复选框显示(’X’/’’) |
JUST | 定义对齐方式(’R’/’L’/’C’) |
IZERO | 增加前导零(’X’/’’) |
NO_SIGN | 不显示符号(’X’/’’) |
NO_ZERO | 不显示前导零(’X’/’’) |
COL_POS | 作为第几列输出(数字) |
DO_SUM | 对字段进行汇总(’X’/’’) |
SELTEXT_L/M/S | 字段名称描述长/中/短(字符串) |
HOTSPOT | 设置为热点(显示为有下划线)(’X’/’’) |
layout
字段 | 说明 |
EDIT | 是否为可编辑模式(’X’/’’) |
COLWIDTH_OPTIMIZE | 优化列宽,根据内容自动匹配宽度(’X’/’’) |
NO_VLINE | 不显示垂直格线(’X’/’’) |
NO_ULINE_HS | 不显示水平格线(’X’/’’) |
INFO_FIELDNAME | 设置标志颜色字段(内表中字段名称) |
KEY_HOTSPOT | 设置关键字段热点(’X’/’’) |
NO_COLNAME | 是否显示字段名(’X’/’’) |
ZEBRA | 以斑马纹输出,深浅色底纹相间(’X’/’’) |
ALV事件
在Type-pools : SLIS中可以查看到 IT_EVENTS 表中可输入内容,或使用函数:REUSE_ALV_EVENTS_GET可以查看
单击/双击
单击/双击的功能代码均为&IC1,但单击需要先对字段设置为热点(Hotspot)通过结构SLIS_SELFIELD(点击的当前行信息),进行字段和值的判断 (注意:单击双击使用同一功能代码,注意按钮功能顺序等判断条件
增加按钮
先增加按钮到应用工具栏,创建GUI_STATUS,增加一个FORM,传入参数参照SLIS_T_EXTAB 使用SET PF-STATUS语句设置按钮,向EVENT表中增加事件,事件名:PF_SET_STATUS,子程序名:设置按钮的子程序 。修改ALV调用函数,给IT_EVENTS传EVENT表,或者不向EVENT传值,而是在ALV的创建函数中,向I_CALLBACK_PF_STATUS_SET直接传子程序的名称
增加表头
创建子程序,对表头内表进行赋值,表头内表:SLIS_T_LIST_HEADER,1、使用函数创建表头REUSE_ALV_COMMENTARY_WRITE,2、修改ALV调用函数,向I_CALLBACK_TOP_OF_PAGE进行传值,传子程序名称,或在EVENT表中增加TOP_OF_PAGE事件
设置颜色
•颜色值的设置:C+X(颜色:1~7)X(是否加重:1/0)X(是否反色1/0),
"增加一个color字段设置行颜色
TYPES: BEGIN OF ty_student.
INCLUDE STRUCTURE ztint_student.
TYPES:icon TYPE char4,
box TYPE char1,
color TYPE char4,
END OF ty_student.
DATA gt_student TYPE STANDARD TABLE OF ty_student .
"取数时设置行颜色显示
FORM get_data .
SELECT * FROM ztint_student INTO CORRESPONDING FIELDS OF TABLE @gt_student.
LOOP AT gt_student ASSIGNING FIELD-SYMBOL(<fs_data>).
IF <fs_data>-sex = '男'.
<fs_data>-icon = icon_green_light.
ELSE.
<fs_data>-icon = icon_red_light.
<fs_data>-color = 'C310'.
ENDIF.
ENDLOOP.
ENDFORM.
“layout里设置INFO_FIELDNAM颜色字段
FORM build_layout .
gs_layout-colwidth_optimize = 'X'.
gs_layout-zebra = 'X'.
gs_layout-box_fieldname = 'BOX'.
gs_layout-confirmation_prompt = 'X'.
gs_layout-window_titlebar = '学生基本信息'.
gs_layout-INFO_FIELDNAME = 'COLOR'.
ENDFORM.
"增加一个color字段设置单元格颜色
TYPES: BEGIN OF ty_student.
INCLUDE STRUCTURE ztint_student.
TYPES:icon TYPE char4,
box TYPE char1,
color TYPE lvc_t_scol,
END OF ty_student.
DATA gt_student TYPE STANDARD TABLE OF ty_student .
"取数时设置行颜色显示
FORM get_data .
SELECT * FROM ztint_student INTO CORRESPONDING FIELDS OF TABLE @gt_student.
LOOP AT gt_student ASSIGNING FIELD-SYMBOL(<fs_data>).
IF <fs_data>-sex = '男'.
<fs_data>-icon = icon_green_light.
ELSE.
<fs_data>-icon = icon_red_light.
IF <fs_data>-school = '北京大学'.
<fs_data>-color = VALUE #( ( fname = 'NAME' color-col = 6 color-int = 0 color-inv = 0 ) ).
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.
“layout里设置coltab_fieldname颜色字段
FORM build_layout .
gs_layout-colwidth_optimize = 'X'.
gs_layout-zebra = 'X'.
gs_layout-box_fieldname = 'BOX'.
gs_layout-confirmation_prompt = 'X'.
gs_layout-window_titlebar = '学生基本信息'.
gs_layout-coltab_fieldname = 'COLOR'.
ENDFORM.
FORM display_data .
PERFORM build_fieldcat.
PERFORM build_layout.
PERFORM build_events.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_grid_title = 'GRIL报表输出'
it_events = gt_events
i_callback_program = sy-repid
i_callback_pf_status_set = 'FM_SET_STATUS'
i_callback_user_command = 'FM_USER_COMMAND'
i_callback_top_of_page = 'FRM_TOP_OF_PAGE' "表头设置
* I_GRID_SETTINGS =
is_layout = gs_layout
it_fieldcat = gt_fieldcat
TABLES
t_outtab = gt_student
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
FORM frm_top_of_page.
DATA lt_header TYPE slis_t_listheader.
lt_header = VALUE #( ( typ = 'H' info = '学生信息表单' )"HEADER
( typ = 'S' key = '当前日期' info = sy-datum )"SELECTION
( typ = 'A' info = '可查看学校信息' ) )."ACTION
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = lt_header.
ENDFORM.
动态文本表头
可以使用超链接等方式增加内容较为复杂的表头,增加子程序,传入参数参照CL_DD_DOCUMENT,使用方法实现增加表头内容,修改ALV调用函数,向I_CALLBACK_HTML_TOP_OF_PAGE传值子程序名称,可以使用事务代码SE24查看类CL_DD_DOCUMENT,以及定义的属性及方法.
TYPES: BEGIN OF ty_student.
include STRUCTURE ztint_student.
TYPES:icon TYPE char4,
box TYPE char1,
END OF ty_student.
DATA gt_student TYPE STANDARD TABLE OF ty_student .
DATA gs_student LIKE LINE OF gt_student.
DATA gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA gs_fieldcat TYPE slis_fieldcat_alv.
DATA gs_layout TYPE slis_layout_alv.
DATA gs_fieldcat TYPE slis_fieldcat_alv.
START-OF-SELECTION.
PERFORM get_data.
END-OF-SELECTION.
IF gt_student IS NOT INITIAL.
PERFORM display_data.
ELSE.
"MESSAGE '未查询到数据!' TYPE 'E'.
ENDIF.
*----------------------------------------------------------------------*
FORM get_data .
SELECT * FROM ztint_student INTO CORRESPONDING FIELDS OF TABLE @gt_student.
LOOP AT gt_student ASSIGNING FIELD-SYMBOL(<fs_data>).
IF <fs_data>-sex = '男'.
<fs_data>-icon = icon_green_light.
ELSE.
<fs_data>-icon = icon_red_light.
ENDIF.
ENDLOOP.
ENDFORM.
*----------------------------------------------------------------------*
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_grid_title = 'GRIL报表输出'
it_events = gt_events
i_callback_program = sy-repid
i_callback_pf_status_set = 'FM_SET_STATUS'
i_callback_user_command = 'FM_USER_COMMAND'
"i_callback_top_of_page = 'FRM_TOP_OF_PAGE'
i_callback_html_top_of_page = 'FRM_HTML_TOP_OF_PAGE'
* I_GRID_SETTINGS =
is_layout = gs_layout
it_fieldcat = gt_fieldcat
TABLES
t_outtab = gt_student
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*----------------------------------------------------------------------*
FORM build_fieldcat .
DEFINE add_field.
CLEAR:gs_fieldcat.
gs_fieldcat-fieldname = &1.
gs_fieldcat-seltext_l = &2.
gs_fieldcat-seltext_m = &2.
gs_fieldcat-edit = &3.
gs_fieldcat-reptext_ddic = ''.
gs_fieldcat-ddictxt = 'L'.
case &1.
when 'CODE'.
gs_fieldcat-KEY = 'X'.
gs_fieldcat-hotspot = 'X'.
WHEN 'WEIGH'.
gs_fieldcat-do_sum = 'X'.
WHEN 'ICON'.
gs_fieldcat-icon = 'X'.
ENDCASE.
APPEND gs_fieldcat to gt_fieldcat.
END-OF-DEFINITION.
add_field :'CODE' '学号' '',
'NAME' '姓名' '',
'SEX' '性别' '',
'SCHOOL' '学校' '',
'WEIGH' '重量' '',
'UNIT' '单位' '',
'ICON' '图标' ''.
ENDFORM.
*----------------------------------------------------------------------*
FORM build_layout .
gs_layout-colwidth_optimize = 'X'.
gs_layout-zebra = 'X'.
gs_layout-box_fieldname = 'BOX'.
gs_layout-confirmation_prompt = 'X'.
gs_layout-window_titlebar = '学生基本信息'.
ENDFORM.
FORM fm_set_status USING rt_extab TYPE slis_t_extab..
SET PF-STATUS 'PF_STATUS' EXCLUDING rt_extab.
ENDFORM.
*----------------------------------------------------------------------*
FORM fm_user_command USING r_ucome LIKE sy-ucomm
r_selfield TYPE slis_selfield.
CASE r_ucome.
WHEN 'EXIT' OR 'CANCEL'.
LEAVE PROGRAM.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN '&IC1'."单击
CASE r_selfield-fieldname.
WHEN 'CODE'.
data(lv_msg) = |点击了学号为{ r_selfield-value }的信息|.
SET PARAMETER ID 'LIF' FIELD r_selfield-value.
CALL TRANSACTION 'FK03'.
MESSAGE lv_msg type 'I'.
ENDCASE.
ENDCASE.
ENDFORM.
FORM frm_html_top_of_page USING pr_document TYPE REF TO cl_dd_document.
DATA lv_date TYPE sdydo_text_element.
CALL METHOD pr_document->add_text
EXPORTING
text = '学生信息表'
sap_style = cl_dd_document=>table_heading.
CALL METHOD pr_document->new_line.
CALL METHOD pr_document->add_text
EXPORTING
text = '日期'
sap_fontsize = cl_dd_document=>medium.
CALL METHOD pr_document->add_gap
EXPORTING
width = 3.
WRITE sy-datum TO lv_date USING EDIT MASK '____年__月_日'.
CALL METHOD pr_document->add_text
EXPORTING
text = lv_date
sap_fontsize = cl_dd_document=>medium.
CALL METHOD pr_document->new_line.
CALL METHOD pr_document->add_text
EXPORTING
text = '连接'
sap_fontsize = cl_dd_document=>medium.
CALL METHOD pr_document->add_gap
EXPORTING
width = 3.
CALL METHOD pr_document->add_link
EXPORTING
url = 'https://www.baidu.com/'
text = '百度'
name = 'baidu'.
ENDFORM.
双击进入供应商查看页面
除上述两个ALV输出函数外,Function ALV还有另一个调用函数REUSE_ALV_GRID_DISPLAY_LVC,与上基本相同,不同的是数据定义不使用SLIS类型池中的类型,而使用数据字典中的结构,部分传参没有。REUSE_ALV_LIST_DISPLAY,基本构造和GRID的调用函数相同,没有CALLBACK_TOP_OF_PAGE,可以在IT_EVENTS中增加TOP_OF_PAGE事件,可以使用WRITE语句实现表头,也可以使用函数实现表头设置。
使用的Function Modules :
• REUSE_ALV_VARIANT_DEFAULT_GET• REUSE_ALV_FIELDCATALOG_MERGE• REUSE_ALV_EVENTS_GET• REUSE_ALV_COMMENTARY_WRITE• REUSE_ALV_VARIANT_F4• REUSE_ALV_VARIANT_EXISTENCE• REUSE_ALV_HIERSEQ_LIST_DISPLAY : 该 FM 用来显示 Hierarchical-sequential interactive list ,很重要• REUSE_ALV_FIELDCATALOG_MERGE 自动生成fieldcat内表上传ALV标题栏的LOGO :T-CODE:OAER
复制程序功能:SE41
二、OO ALV
是指以面向对象的方式实现ALV的输出,通过界面调用,在界面的Customer Control控件中,输出ALV
实现步骤:
- ALV实现相关变量定义
- 创建本地类的声明及实现(EVENT HANDLER)
- 创建界面,并创建Customer Control容器
- 实例化container,关联Customer Control容器
- 将ALV植入container中
- ALV格式化(layout及fieldcat的赋值等)
- 注册相关事件
- 执行ALV显示(CALL METHOD GS_XXX->SET_TABLE_FOR_FIRST_DISPLAY)
主要使用的类:
DATA:gs_alv TYPE REF TO cl_gui_alv_grid. “用于表单输出 DATA:gs_con TYPE REF TO cl_gui_custom_container. “用于定义容器 DATA:gs_dyndoc_id TYPE REF TO cl_dd_document. “用于表头书写 DATA:gs_splitter TYPE REF TO cl_gui_splitter_container. “用于分割容器
主要使用的方法:
- 第一次输出表单:SET_TABLE_FOR_FIRST_DISPLAY
- 刷新表单内容:REFRESH_TABLE_DISPLAY,I_SOFT_REFRESH,’X’ :只刷新单元格(如果有合计不自动更新)
- 刷新fieldcat: SET_FRONTED_FIELDCATALOG,如果fieldcat格式有修改,需要刷新格式设置,则调用这个方法
基本实现步骤
- 创建REPORT程序,在TOP INCLUDE中增加基本定义
- 在END-OF-SELECTION中执行输出,如创建界面0100,调用界面0100
- 在0100界面中增加customer control控件,例名称为GS_CON
- 在0100界面的PBO部分增加module,用于ALV输出
- 设置fieldcat及layout等属性,通过调用方法执行ALV的初次输出或刷新
在使用ALV类之前,需要对其进行创建,并指定ALV输出时的容器,定义,参照类定义ALV及容器
DATA gs_alv TYPE REF TO cl_gui_alv_grid. ”alv输出
DATA gs_con TYPE REF TO cl_gui_custom_container., “ALV容器
在界面中增加Customer Control控件,例名称为:GC_CON,实例化容器对象,给CONSTRUCTOR构造方法传值,CONTAINER_NAME,界面中用户定义容器名称,实例化ALV对象,给CONSTRUCTOR构造方法传值,I_PARENT,实例化的容器对象
FORM create_alv .
CREATE OBJECT gs_con
EXPORTING
container_name = 'GC_CON'.
CREATE OBJECT gs_alv
EXPORTING
i_parent = gs_con.
ENDFORM.
设置Fieldcat
定义内表:LVC_T_FCAT 结构:LVC_S_FCAT ,执行ALV输出前,对fieldcat表单进行赋值,调用ALV 输出方法时,传值给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',下面出现下划线,响应单击 |
DECIMALS | 设置小数的位数 |
SCRTEXT_L/M/S | 字段标签长/中/短 |
DATA gt_fieldcat TYPE STANDARD TABLE OF lvc_s_fcat .
FORM build_fieldcat .
gt_fieldcat = VALUE #( ( fieldname = 'CODE' scrtext_l = '学号' outputlen = '10' fix_column = 'X' emphasize = 'C100' )
( fieldname = 'NAME' scrtext_l = '姓名' outputlen = '30' fix_column = 'X' )
( fieldname = 'SEX' scrtext_l = '姓别' outputlen = '30' fix_column = 'X' )
( fieldname = 'SCHOOL' scrtext_l = '学校' outputlen = '30' fix_column = 'X' hotspot = 'X' )
( fieldname = 'WEIGH' scrtext_l = '重量' outputlen = '30' fix_column = 'X' edit = 'X' decimals = 2 )
( fieldname = 'UNIT' scrtext_l = '单位' outputlen = '30' fix_column = 'X' ) ).
ENDFORM.
设置LAYOUT
定义:结构:LVC_S_LAYO,执行ALV输出前,对layout结构进行赋值,调用ALV输出方法时,传值给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 | 字段格式 |
ut•颜色值的设置:C+X(颜色:1~7)X(是否加重:1/0)X(是否反色1/0), 以列为单位:在FIELDCAT中给EMPHASIZE字段赋值,以行为单位:在内表中增加一个字段,类型为CHAR,至少长度为4,例COLOR给向输出内表获取数据时,给COLOR字段赋值,设置LAYOUT,给INFO_FNAME字段赋值颜色字段名称,以单元格为单位:在内表中增加一个字段,类型为LVC_T_SCOL ,例COLOR,在向输出内表取数据时,通过结构给COLOR字段内表赋值,设置LAYOUT,给CTAB_FNAME字段赋值颜色字段名称,例COLOR
DATA gs_layout TYPE lvc_s_layo.
FORM buil_layout .
gs_layout-col_opt = 'X'.
gs_layout-zebra = 'X'.
gs_layout-box_fname = 'BOX'.
gs_layout-ctab_fname = 'COLOR'.
gs_layout-stylefname = 'STYLE'.
"gs_layout-edit = 'X'.
ENDFORM.
可修改属性设置
可修以表单为单位,设置LAYOUT-EDIT,为X,可修改, 以列为单位,设置FIELDCAT-EDIT,为X,列可修改,以单元格为单位在内表中增加一个字段,类型为LVC_T_STYL ,例CELLSTL在需要设置修改属性的位置,通过结构给CELLSTL字段内表赋值FIELDNAME,需要设置属性的字段名 STYLE,可修改属性 不可修改:CL_GUI_ALV_GRID=>MC_STYLE_DISABLED 可修改:CL_GUI_ALV_GRID=>MC_STYLE_ENALBED,最后设置LAYOUT,给STYLEFNAME字段赋值修改属性的字段名,例CELLSTL,特别的,如果将某一列中部分字段设置为可修改状态,一般通过以下方式完成•在FIELDCAT中,设置该列属性为可修改•在表单赋值时,将不应为可修改状态的字段,设置为不可修改,不是将需要修改的单元格设置为可修改,防止可修改属性设置与静态属性设置冲突
ALV布局管理
ALV中,若想保存布局,要添加I_SAVE参数,I_SAVE定义为C数据类型,值如下:
为了进一步管理布局,则需要加入IS_VARIANT参数 ,其数据结构参考DISVARIANT,REPORT = SY-REPID “程序名称,VARIANT为布局保存格式,有三个有用函数供调用保存的格式布局分别为
REUSE_ALV_VARIANT_F4 显示布局格式选
当排序、筛选等也设置时,以布局管理参数设置优先,当用户设置布局参数默认值时,以布局参数传值优先
隐藏按钮
在OO ALV输出过程中,默认输出按钮栏,包含标准按钮,如果标准按钮中有部分不需要的按钮,通过隐藏实现
DATA gt_exclud TYPE ui_functions.
gt_exclud = VALUE #( ( gs_alv->mc_fc_loc_insert_row ) ( gs_alv->mc_fc_loc_delete_row ) ).
gs_alv->set_table_for_first_display(
EXPORTING is_layout = gs_layout
it_toolbar_excluding = gt_exclud
CHANGING it_outtab = gt_studet
it_fieldcatalog = gt_fieldcat[] ).
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
EVENT事件
首先先进行相关定义,创建本地类,可以在TOP中直接使用DEFINITION DEFERRED语句,先推迟声明一个类,续会对这个类进行实际的定义,参照类声明对象,然后创建本地类
CLASS lcl_app_compt DEFINITION DEFERRED."后续定义
DATA event_handel TYPE REF TO lcl_app_compt.”类接收器
“声明
CLASS lcl_app_compt DEFINITION.
PUBLIC SECTION.
METHODS handel_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object.
METHODS handel_user_conmmand FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
METHODS header_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING e_row_id e_column_id.
METHODS header_double_click FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column.
METHODS header_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed e_ucomm.
ENDCLASS.
”实现
CLASS lcl_app_compt IMPLEMENTATION.
METHOD handel_toolbar..
PERFORM frm_handel_toolbar USING e_object.
ENDMETHOD.
METHOD handel_user_conmmand .
PERFORM frm_handel_user_conmmand USING e_ucomm.
ENDMETHOD.
METHOD header_hotspot_click.
PERFORM frm_hotspot_click USING e_row_id e_column_id.
ENDMETHOD.
METHOD header_double_click.
PERFORM frm_double_click USING e_row e_column.
ENDMETHOD.
METHOD header_data_changed.
PERFORM frm_data_changed USING er_data_changed->MT_MOD_CELLS.
ENDMETHOD.
ENDCLASS.
MODULE init_alv OUTPUT.
IF gs_alv IS INITIAL.
PERFORM create_alv.
PERFORM get_data.
PERFORM buil_layout.
PERFORM build_fieldcat.
PERFORM set_handler.
PERFORM buil_alv.
ELSE.
PERFORM refresh_alv.
ENDIF.
ENDMODULE.
“注册事件
FORM set_handler .
CREATE OBJECT event_handel.
SET HANDLER event_handel->handel_toolbar FOR gs_alv.
SET HANDLER event_handel->handel_user_conmmand FOR gs_alv.
SET HANDLER event_handel->header_hotspot_click FOR gs_alv.
SET HANDLER event_handel->header_double_click FOR gs_alv.
SET HANDLER event_handel->header_data_changed FOR gs_alv.
"DATA CHANGGED 事件触发
gs_alv->register_edit_event( EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_enter ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_HANDEL_TOOLBAR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_E_OBJECT text
*----------------------------------------------------------------------*
FORM frm_handel_toolbar USING p_e_object TYPE REF TO cl_alv_event_toolbar_set.
DATA ls_button TYPE stb_button.
ls_button-butn_type = '3'.
APPEND ls_button TO p_e_object->mt_toolbar.
CLEAR: ls_button .
ls_button-function = 'CALLSF'.
ls_button-text = '调用SF'.
ls_button-icon = icon_failure.
APPEND ls_button TO p_e_object->mt_toolbar.
ls_button-butn_type = '3'.
APPEND ls_button TO p_e_object->mt_toolbar.
CLEAR: ls_button .
ls_button-function = 'EDIT'.
ls_button-text = '修改选中行'.
ls_button-icon = icon_change.
APPEND ls_button TO p_e_object->mt_toolbar.
ls_button-butn_type = '3'.
APPEND ls_button TO p_e_object->mt_toolbar.
CLEAR: ls_button .
ls_button-function = 'DOWNLOAD'.
ls_button-text = '下载表单'.
ls_button-icon = icon_export.
APPEND ls_button TO p_e_object->mt_toolbar.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_USER_CONMMAND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_E_UCOMM text
*----------------------------------------------------------------------*
FORM frm_handel_user_conmmand USING p_ucomm TYPE sy-ucomm.
CASE p_ucomm .
WHEN 'CALLSF'.
MESSAGE '点击了调用SF功能按钮' TYPE 'S'.
WHEN 'EDIT'.
PERFORM frm_change_data.
WHEN 'DOWNLOAD'.
PERFORM frm_download_file.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CHANGE_DATA 编辑按钮事件选中行体重单元可修改
* 这里我先再设置fieldcat时将体重列设置成了可修改状态,取数时,在style里
* 设置这一列不可修改,当选中行,点击了编辑按钮时,我将其选中行单元格重量stlye
* 属性删除,从而设置选中行单元体重单元格为可修改状态
*&---------------------------------------------------------------------*
FORM frm_change_data .
"获取选中行
gs_alv->get_selected_rows( IMPORTING et_index_rows = DATA(lt_index_rows)
et_row_no = DATA(lt_row_no) ).
DESCRIBE TABLE lt_index_rows LINES DATA(lv_lines).
IF lv_lines NE 1.
MESSAGE '只可以选择单行进行编辑操作' TYPE 'E'.
EXIT.
ENDIF.
"修改当前行状态
READ TABLE lt_index_rows INTO DATA(ls_temp) INDEX 1.
CHECK sy-subrc EQ 0.
READ TABLE gt_studet ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX ls_temp-index.
IF sy-subrc EQ 0.
READ TABLE <fs_data>-style TRANSPORTING NO FIELDS WITH KEY fieldname = 'WEIGH'.
IF sy-subrc EQ 0.
DELETE <fs_data>-style INDEX sy-tabix.
ENDIF.
ENDIF.
"刷新界面
PERFORM refresh_alv.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_FILE 下载
*&---------------------------------------------------------------------*
FORM frm_download_file .
DATA lv_filename TYPE string.
TYPES:BEGIN OF ty_filename ,
name TYPE char20,
END OF ty_filename.
DATA lt_filename TYPE STANDARD TABLE OF ty_filename WITH DEFAULT KEY.
DATA lt_student TYPE STANDARD TABLE OF ztint_student WITH DEFAULT KEY.
lt_filename = VALUE #( ( name = '集团' ) ( name = '学号' ) ( name = '姓名' ) ( name = '性别' ) ( name = '学校' ) ( name = '体重' ) ( name = '单位' ) ).
lt_student = CORRESPONDING #( gt_studet ).
"获取文件名
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = ' 学生信息表'
* DEF_PATH = ' '
mask = ',EXCEL,*.XLS. '
mode = 'S '
title = '整表单保存 '
IMPORTING
filename = lv_filename
* RC =
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0.
EXIT.
* Implement suitable error handling here
ENDIF.
"下载数据
CHECK lv_filename IS NOT INITIAL.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = lv_filename
* FILETYPE = 'ASC'
* APPEND = ' '
write_field_separator = 'X'
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* VIRUS_SCAN_PROFILE = '/SCET/GUI_DOWNLOAD'
* IMPORTING
* FILELENGTH =
TABLES
data_tab = lt_student[]
fieldnames = lt_filename[]
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_HOTSPOT_CLICK 单击事件
*&---------------------------------------------------------------------*
FORM frm_hotspot_click USING p_row_id TYPE lvc_s_row
p_column_i TYPE lvc_s_col.
IF p_column_i-fieldname EQ 'SCHOOL'.
READ TABLE gt_studet INTO DATA(gs_student) INDEX p_row_id-index.
MESSAGE '单击了弟' && p_row_id-index && '行,学校名称为' && gs_student-school TYPE 'I'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOUBLE_CLICK 双击事件
*&---------------------------------------------------------------------*
FORM frm_double_click USING p_row_id TYPE lvc_s_row
p_column_i TYPE lvc_s_col.
IF p_column_i-fieldname EQ 'NAME'.
READ TABLE gt_studet INTO DATA(gs_student) INDEX p_row_id-index.
MESSAGE '双击了弟' && p_row_id-index && '行,学校名称为' && gs_student-school TYPE 'I'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_CHANGED 编辑事件
*&---------------------------------------------------------------------*
FORM frm_data_changed USING pt_cell TYPE lvc_t_modi.
LOOP AT pt_cell INTO DATA(ls_cell).
READ TABLE gt_studet ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX ls_cell-row_id.
CASE ls_cell-fieldname.
WHEN 'SEX'.
<fs_data>-sex = ls_cell-value.
WHEN 'WEIGH'.
<fs_data>-weigh = ls_cell-value.
ENDCASE.
ENDLOOP.
ENDFORM.
这里值得注意的是,体重列我设置的是两位小数类型,当点击单元格修改回车后,数值会自动缩小100倍,比如输入100,回车后自动变为了1,此时需要在设置fieldcat属下时,当字段名为重量的时候新增一个属性decimals = 2,如下:
FORM build_fieldcat .
gt_fieldcat = VALUE #( ( fieldname = 'CODE' scrtext_l = '学号' outputlen = '10' fix_column = 'X' emphasize = 'C100' )
( fieldname = 'NAME' scrtext_l = '姓名' outputlen = '30' fix_column = 'X' )
( fieldname = 'SEX' scrtext_l = '姓别' outputlen = '30' fix_column = 'X' )
( fieldname = 'SCHOOL' scrtext_l = '学校' outputlen = '30' fix_column = 'X' hotspot = 'X' )
( fieldname = 'WEIGH' scrtext_l = '重量' outputlen = '30' fix_column = 'X' edit = 'X' decimals = 2 )
( fieldname = 'UNIT' scrtext_l = '单位' outputlen = '30' fix_column = 'X' ) ).
ENDFORM.
TOP_OF_PAGE
同上其他事件,先在本地类中新增方法处理之间,将表头内容写入到表头内容的类中,将表头内容与表头输出的类关联,将ALV显示容器分割成两部分,获取上半部分用于表头输出,获取下半部分用于表体输出,最在ALV输出后,将表头输出
DATA gs_con TYPE REF TO cl_gui_custom_container. “包含表头标题的容器
DATA gs_con_top TYPE REF TO cl_gui_container.”表头容器
DATA gs_con_grid TYPE REF TO cl_gui_container.“表体容器
DATA gs_html_control TYPE REF TO cl_gui_html_viewer.”表头输出类
DATA gs_spliter TYPE REF TO cl_gui_splitter_container.“分割容器
DATA gs_top TYPE REF TO cl_dd_document.”表头内容
*&---------------------------------------------------------------------*
*& 包含 YTEST_ZJJ09_CLS 本地类中新增处理事件方法
*&---------------------------------------------------------------------*
CLASS lcl_app_compt DEFINITION.
PUBLIC SECTION.
METHODS handel_top_of_page FOR EVENT top_of_page OF cl_gui_alv_grid
IMPORTING e_dyndoc_id.
ENDCLASS.
CLASS lcl_app_compt IMPLEMENTATION.
METHOD handel_top_of_page.
PERFORM frm_top_of_page USING e_dyndoc_id.
ENDMETHOD.
ENDCLASS.
*&---------------------------------------------------------------------*
*& Form CREATE_ALV 分割容器
*&---------------------------------------------------------------------*
FORM create_alv .
CREATE OBJECT gs_con
EXPORTING
container_name = 'GC_CON'.
"创建ALV_GRID
CREATE OBJECT gs_top
EXPORTING
style = 'ALV_GRID'.
"创建分割,将gs_con分成两部分
CREATE OBJECT gs_spliter
EXPORTING
parent = gs_con
rows = 2
columns = 1.
"将表头在top中输出
CREATE OBJECT gs_html_control
EXPORTING
parent = gs_con_top.
gs_con_top = gs_spliter->get_container( EXPORTING row = 1 column = 1 ).
gs_con_grid = gs_spliter->get_container( EXPORTING row = 2 column = 1 ).
gs_spliter->set_row_height( EXPORTING id = 1 height = 20 ). "设置行高
"将表体在GRID中输出
CREATE OBJECT gs_alv
EXPORTING
i_parent = gs_con_grid.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_HANDLER 注册事件
*&---------------------------------------------------------------------*
FORM set_handler .
CREATE OBJECT event_handel.
SET HANDLER event_handel->handel_top_of_page FOR gs_alv.
"DATA CHANGGED 事件触发
gs_alv->register_edit_event( EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_enter ).
ENDFORM.
*&---------------------------------------------------------------------*
*& frm_top_of_page 表头内容
*&---------------------------------------------------------------------*
FORM frm_top_of_page USING pr_document TYPE REF TO cl_dd_document.
DATA lv_date TYPE sdydo_text_element.
CALL METHOD gs_top->add_text
EXPORTING
text = '学生信息表'
sap_style = cl_dd_document=>table_heading.
CALL METHOD gs_top->new_line.
CALL METHOD gs_top->add_text
EXPORTING
text = '日期'
sap_fontsize = cl_dd_document=>medium.
CALL METHOD gs_top->add_gap
EXPORTING
width = 3.
WRITE sy-datum TO lv_date USING EDIT MASK '____年__月_日'.
CALL METHOD gs_top->add_text
EXPORTING
text = lv_date
sap_fontsize = cl_dd_document=>medium.
CALL METHOD gs_top->new_line.
CALL METHOD gs_top->add_text
EXPORTING
text = '连接'
sap_fontsize = cl_dd_document=>medium.
CALL METHOD gs_top->add_gap
EXPORTING
width = 3.
CALL METHOD gs_top->add_link
EXPORTING
url = 'https://www.baidu.com/'
text = '百度'
name = 'baidu'.
CALL METHOD gs_top->display_document
EXPORTING
reuse_control = 'X'
parent = gs_con_top
EXCEPTIONS
html_display_error = 1.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form BUIL_ALV 输出ALV后将表头输出
*&---------------------------------------------------------------------*
FORM buil_alv .
"gt_exclud = VALUE #( ( gs_alv->mc_fc_loc_insert_row ) ( gs_alv->mc_fc_loc_delete_row ) ).
gs_alv->set_table_for_first_display(
EXPORTING
* i_buffer_active =
* i_bypassing_buffer =
* i_consistency_check =
* i_structure_name =
* is_variant =
* i_save =
* i_default = 'X'
is_layout = gs_layout
* is_print =
* it_special_groups =
it_toolbar_excluding = gt_exclud
* it_hyperlink =
* it_alv_graphics =
* it_except_qinfo =
* ir_salv_adapter =
CHANGING
it_outtab = gt_studet
it_fieldcatalog = gt_fieldcat[]
* it_sort =
* it_filter =
* EXCEPTIONS
* invalid_parameter_combination = 1
* program_error = 2
* too_many_lines = 3
* others = 4
).
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
"输出表头
gs_alv->list_processing_events( EXPORTING i_event_name = 'TOP_OF_PAGE' i_dyndoc_id = gs_top ).
ENDFORM.
DATA_CHANGED
当数据修改时,可以触发这个事件,可以进行数据检查或修改等,
REPORT ytest_zjj09.
INCLUDE ytest_zjj09_top.
INCLUDE ytest_zjj09_cls.
INCLUDE ytest_zjj09_o01.
INCLUDE ytest_zjj09_i01.
INCLUDE ytest_zjj09_form.
START-OF-SELECTION.
PERFORM get_data.
END-OF-SELECTION.
CALL SCREEN 0100.
**************INCLUDE ytest_zjj09_top****************************************************
TYPES:BEGIN OF ty_student.
INCLUDE TYPE ztint_student.
TYPES: box TYPE char1,
color TYPE lvc_t_scol,
style TYPE lvc_t_styl,
END OF ty_student.
DATA gt_studet TYPE STANDARD TABLE OF ty_student WITH DEFAULT KEY.
DATA gs_studet LIKE LINE OF gt_studet.
DATA gs_alv TYPE REF TO cl_gui_alv_grid.
DATA gs_con TYPE REF TO cl_gui_custom_container.
DATA gs_con_top TYPE REF TO cl_gui_container.
DATA gs_con_grid TYPE REF TO cl_gui_container.
DATA gs_html_control TYPE REF TO cl_gui_html_viewer.
DATA gs_spliter TYPE REF TO cl_gui_splitter_container.
DATA gs_top TYPE REF TO cl_dd_document.
DATA gt_fieldcat TYPE STANDARD TABLE OF lvc_s_fcat .
DATA gs_layout TYPE lvc_s_layo.
DATA gt_exclud TYPE ui_functions.
DATA gs_fieldcat TYPE lvc_s_fcat.
DATA ok_code TYPE sy-ucomm.
CLASS lcl_app_compt DEFINITION DEFERRED.
DATA event_handel TYPE REF TO lcl_app_compt.
SELECT-OPTIONS s_code FOR ztint_student-code.
**************INCLUDE ytest_zjj09_top***************************************************
**************INCLUDE ytest_zjj09_cls****************************************************
CLASS lcl_app_compt DEFINITION.
PUBLIC SECTION.
METHODS handel_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object.
METHODS handel_user_conmmand FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
METHODS header_hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING e_row_id e_column_id.
METHODS header_double_click FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column.
METHODS header_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed e_ucomm.
METHODS handel_top_of_page FOR EVENT top_of_page OF cl_gui_alv_grid
IMPORTING e_dyndoc_id.
ENDCLASS.
CLASS lcl_app_compt IMPLEMENTATION.
METHOD handel_toolbar..
PERFORM frm_handel_toolbar USING e_object.
ENDMETHOD.
METHOD handel_user_conmmand .
PERFORM frm_handel_user_conmmand USING e_ucomm.
ENDMETHOD.
METHOD header_hotspot_click.
PERFORM frm_hotspot_click USING e_row_id e_column_id.
ENDMETHOD.
METHOD header_double_click.
PERFORM frm_double_click USING e_row e_column.
ENDMETHOD.
METHOD header_data_changed.
PERFORM frm_data_changed USING er_data_changed->mt_mod_cells.
ENDMETHOD.
METHOD handel_top_of_page.
PERFORM frm_top_of_page USING e_dyndoc_id.
ENDMETHOD.
ENDCLASS.
**************INCLUDE ytest_zjj09_cls***************************************************
**************0100 pai及pbo调用*********************************************************
PROCESS BEFORE OUTPUT.
MODULE status_0100.
MODULE init_alv.
*
PROCESS AFTER INPUT.
MODULE exit AT EXIT-COMMAND.
MODULE user_command_0100.
MODULE status_0100 OUTPUT.
SET PF-STATUS '0100'.
SET TITLEBAR '0100'.
ENDMODULE.
**************0100 pai及pbo调用*********************************************************
**************INCLUDE ytest_zjj09_o01***************************************************
MODULE init_alv OUTPUT.
IF gs_alv IS INITIAL.
PERFORM create_alv.
PERFORM buil_layout.
PERFORM build_fieldcat.
PERFORM set_handler.
PERFORM buil_alv.
ELSE.
PERFORM refresh_alv.
ENDIF.
ENDMODULE.
**************INCLUDE ytest_zjj09_o01***************************************************
**************INCLUDE ytest_zjj09_i01***************************************************
MODULE exit INPUT.
LEAVE PROGRAM.
ENDMODULE.
MODULE user_command_0100 INPUT.
CASE ok_code.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'SAVE'.
"这样在点击保存按钮的时候,可以触发data changed事件
gs_alv->check_changed_data( ).
ENDCASE.
ENDMODULE.
**************INCLUDE ytest_zjj09_i01***************************************************
**************INCLUDE ytest_zjj09_form *************************************************
**************get_data********************
FORM get_data .
DATA ls_style TYPE lvc_s_styl.
SELECT * FROM ztint_student INTO CORRESPONDING FIELDS OF TABLE @gt_studet WHERE code IN @s_code.
LOOP AT gt_studet ASSIGNING FIELD-SYMBOL(<fs_data>).
<fs_data>-style = VALUE #( ( fieldname = 'WEIGH' style = cl_gui_alv_grid=>mc_style_disabled ) )."设置体重列为可修改状态
IF <fs_data>-school EQ '北京大学'.
<fs_data>-color = VALUE #( ( fname = 'NAME' color-col = 6 color-int = 0 color-inv = 0 ) ).”设置学校名称为北京大学的行的姓名颜色
ENDIF.
ENDLOOP.
ENDFORM.
**************create_alv********************
FORM create_alv .
CREATE OBJECT gs_con
EXPORTING
container_name = 'GC_CON'.
"创建ALV_GRID
CREATE OBJECT gs_top
EXPORTING
style = 'ALV_GRID'.
"创建分割,将gs_con分成两部分
CREATE OBJECT gs_spliter
EXPORTING
parent = gs_con
rows = 2
columns = 1.
"将表头在top中输出
CREATE OBJECT gs_html_control
EXPORTING
parent = gs_con_top.
gs_con_top = gs_spliter->get_container( EXPORTING row = 1 column = 1 ).
gs_con_grid = gs_spliter->get_container( EXPORTING row = 2 column = 1 ).
gs_spliter->set_row_height( EXPORTING id = 1 height = 20 ). "设置表头行高
"将表体在GRID中输出
CREATE OBJECT gs_alv
EXPORTING
i_parent = gs_con_grid.
ENDFORM.
**************buil_layout********************
FORM buil_layout .
gs_layout-col_opt = 'X'.
gs_layout-zebra = 'X'.
gs_layout-box_fname = 'BOX'.
gs_layout-ctab_fname = 'COLOR'.
gs_layout-stylefname = 'STYLE'.
"gs_layout-edit = 'X'.
ENDFORM.
**************build_fieldcat********************
FORM build_fieldcat .
gt_fieldcat = VALUE #( ( fieldname = 'CODE' scrtext_l = '学号' outputlen = '10' fix_column = 'X' emphasize = 'C100' )
( fieldname = 'NAME' scrtext_l = '姓名' outputlen = '30' fix_column = 'X' )
( fieldname = 'SEX' scrtext_l = '姓别' outputlen = '30' fix_column = 'X' )
( fieldname = 'SCHOOL' scrtext_l = '学校' outputlen = '30' fix_column = 'X' hotspot = 'X' )
( fieldname = 'WEIGH' scrtext_l = '重量' outputlen = '30' fix_column = 'X' edit = 'X' decimals = 2 )
( fieldname = 'UNIT' scrtext_l = '单位' outputlen = '30' fix_column = 'X' ) ).
ENDFORM.
**************set_handler********************
FORM set_handler .
CREATE OBJECT event_handel.
SET HANDLER event_handel->handel_toolbar FOR gs_alv.
SET HANDLER event_handel->handel_user_conmmand FOR gs_alv.
SET HANDLER event_handel->header_hotspot_click FOR gs_alv.
SET HANDLER event_handel->header_double_click FOR gs_alv.
SET HANDLER event_handel->header_data_changed FOR gs_alv.
SET HANDLER event_handel->handel_top_of_page FOR gs_alv.
"按enter键时触发DATA CHANGGED 事件
gs_alv->register_edit_event( EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_enter ).
ENDFORM.
**************buil_alv********************
FORM buil_alv .
"gt_exclud = VALUE #( ( gs_alv->mc_fc_loc_insert_row ) ( gs_alv->mc_fc_loc_delete_row ) ).
gs_alv->set_table_for_first_display(
EXPORTING
* i_buffer_active =
* i_bypassing_buffer =
* i_consistency_check =
* i_structure_name =
* is_variant =
* i_save =
* i_default = 'X'
is_layout = gs_layout
* is_print =
* it_special_groups =
it_toolbar_excluding = gt_exclud
* it_hyperlink =
* it_alv_graphics =
* it_except_qinfo =
* ir_salv_adapter =
CHANGING
it_outtab = gt_studet
it_fieldcatalog = gt_fieldcat[]
* it_sort =
* it_filter =
* EXCEPTIONS
* invalid_parameter_combination = 1
* program_error = 2
* too_many_lines = 3
* others = 4
).
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
"输出表头
gs_alv->list_processing_events( EXPORTING i_event_name = 'TOP_OF_PAGE' i_dyndoc_id = gs_top ).
ENDFORM.
**************refresh_alv ********************
FORM refresh_alv .
DATA gs_stbl TYPE lvc_s_stbl.
gs_stbl-col = 'X'.
gs_stbl-row = 'X'.
gs_alv->refresh_table_display(
EXPORTING
is_stable = gs_stbl
* i_soft_refresh =
EXCEPTIONS
finished = 1
OTHERS = 2 ).
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_HANDEL_TOOLBAR
*&---------------------------------------------------------------------*
FORM frm_handel_toolbar USING p_e_object TYPE REF TO cl_alv_event_toolbar_set.
DATA ls_button TYPE stb_button.
ls_button-butn_type = '3'.
APPEND ls_button TO p_e_object->mt_toolbar.
CLEAR: ls_button .
ls_button-function = 'CALLSF'.
ls_button-text = '调用SF'.
ls_button-icon = icon_failure.
APPEND ls_button TO p_e_object->mt_toolbar.
ls_button-butn_type = '3'.
APPEND ls_button TO p_e_object->mt_toolbar.
CLEAR: ls_button .
ls_button-function = 'EDIT'.
ls_button-text = '修改选中行'.
ls_button-icon = icon_change.
APPEND ls_button TO p_e_object->mt_toolbar.
ls_button-butn_type = '3'.
APPEND ls_button TO p_e_object->mt_toolbar.
CLEAR: ls_button .
ls_button-function = 'DOWNLOAD'.
ls_button-text = '下载表单'.
ls_button-icon = icon_export.
APPEND ls_button TO p_e_object->mt_toolbar.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_USER_CONMMAND
*&---------------------------------------------------------------------*
FORM frm_handel_user_conmmand USING p_ucomm TYPE sy-ucomm.
CASE p_ucomm .
WHEN 'CALLSF'.
MESSAGE '点击了调用SF功能按钮' TYPE 'S'.
WHEN 'EDIT'.
PERFORM frm_change_data.
WHEN 'DOWNLOAD'.
PERFORM frm_download_file.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_CHANGE_DATA
*&---------------------------------------------------------------------*
FORM frm_change_data .
"获取选中行
gs_alv->get_selected_rows( IMPORTING et_index_rows = DATA(lt_index_rows)
et_row_no = DATA(lt_row_no) ).
DESCRIBE TABLE lt_index_rows LINES DATA(lv_lines).
IF lv_lines NE 1.
MESSAGE '只可以选择单行进行编辑操作' TYPE 'E'.
EXIT.
ENDIF.
"修改当前行状态
READ TABLE lt_index_rows INTO DATA(ls_temp) INDEX 1.
CHECK sy-subrc EQ 0.
READ TABLE gt_studet ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX ls_temp-index.
IF sy-subrc EQ 0.
READ TABLE <fs_data>-style TRANSPORTING NO FIELDS WITH KEY fieldname = 'WEIGH'.
IF sy-subrc EQ 0.
DELETE <fs_data>-style INDEX sy-tabix.
ENDIF.
ENDIF.
"刷新界面
PERFORM refresh_alv.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD_FILE
*&---------------------------------------------------------------------*
FORM frm_download_file .
DATA lv_filename TYPE string.
TYPES:BEGIN OF ty_filename ,
name TYPE char20,
END OF ty_filename.
DATA lt_filename TYPE STANDARD TABLE OF ty_filename WITH DEFAULT KEY.
DATA lt_student TYPE STANDARD TABLE OF ztint_student WITH DEFAULT KEY.
lt_filename = VALUE #( ( name = '集团' ) ( name = '学号' ) ( name = '姓名' ) ( name = '性别' ) ( name = '学校' ) ( name = '体重' ) ( name = '单位' ) ).
lt_student = CORRESPONDING #( gt_studet ).
"获取文件名
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = ' 学生信息表'
* DEF_PATH = ' '
mask = ',EXCEL,*.XLS. '
mode = 'S '
title = '整表单保存 '
IMPORTING
filename = lv_filename
* RC =
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
IF sy-subrc <> 0.
EXIT.
* Implement suitable error handling here
ENDIF.
"下载数据
CHECK lv_filename IS NOT INITIAL.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
filename = lv_filename
* FILETYPE = 'ASC'
* APPEND = ' '
write_field_separator = 'X'
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* WRITE_LF_AFTER_LAST_LINE = ABAP_TRUE
* SHOW_TRANSFER_STATUS = ABAP_TRUE
* VIRUS_SCAN_PROFILE = '/SCET/GUI_DOWNLOAD'
* IMPORTING
* FILELENGTH =
TABLES
data_tab = lt_student[]
fieldnames = lt_filename[]
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_HOTSPOT_CLICK
*&---------------------------------------------------------------------*
FORM frm_hotspot_click USING p_row_id TYPE lvc_s_row
p_column_i TYPE lvc_s_col.
IF p_column_i-fieldname EQ 'SCHOOL'.
READ TABLE gt_studet INTO DATA(gs_student) INDEX p_row_id-index.
MESSAGE '单击了弟' && p_row_id-index && '行,学校名称为' && gs_student-school TYPE 'I'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOUBLE_CLICK
*&---------------------------------------------------------------------*
FORM frm_double_click USING p_row_id TYPE lvc_s_row
p_column_i TYPE lvc_s_col.
IF p_column_i-fieldname EQ 'NAME'.
READ TABLE gt_studet INTO DATA(gs_student) INDEX p_row_id-index.
MESSAGE '双击了弟' && p_row_id-index && '行,学校名称为' && gs_student-school TYPE 'I'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_CHANGED
*&---------------------------------------------------------------------*
FORM frm_data_changed USING pt_cell TYPE lvc_t_modi.
LOOP AT pt_cell INTO DATA(ls_cell).
READ TABLE gt_studet ASSIGNING FIELD-SYMBOL(<fs_data>) INDEX ls_cell-row_id.
CASE ls_cell-fieldname.
WHEN 'SEX'.
<fs_data>-sex = ls_cell-value.
WHEN 'WEIGH'.
<fs_data>-weigh = ls_cell-value.
ENDCASE.
ENDLOOP.
ENDFORM.
FORM frm_top_of_page USING pr_document TYPE REF TO cl_dd_document.
DATA lv_date TYPE sdydo_text_element.
CALL METHOD gs_top->add_text
EXPORTING
text = '学生信息表'
sap_style = cl_dd_document=>table_heading.
CALL METHOD gs_top->new_line.
CALL METHOD gs_top->add_text
EXPORTING
text = '日期'
sap_fontsize = cl_dd_document=>medium.
CALL METHOD gs_top->add_gap
EXPORTING
width = 3.
WRITE sy-datum TO lv_date USING EDIT MASK '____年__月_日'.
CALL METHOD gs_top->add_text
EXPORTING
text = lv_date
sap_fontsize = cl_dd_document=>medium.
CALL METHOD gs_top->new_line.
CALL METHOD gs_top->add_text
EXPORTING
text = '连接'
sap_fontsize = cl_dd_document=>medium.
CALL METHOD gs_top->add_gap
EXPORTING
width = 3.
CALL METHOD gs_top->add_link
EXPORTING
url = 'https://www.baidu.com/'
text = '百度'
name = 'baidu'.
CALL METHOD gs_top->display_document
EXPORTING
reuse_control = 'X'
parent = gs_con_top
EXCEPTIONS
html_display_error = 1.
ENDFORM.
如下为报表输出效果:
单击学校名称
双击姓名
选中第二行重量单元格变为可修改
OO ALV 自定义F4搜索帮助
实现步骤:
- 在本地类lcl_app_compt中定义方法handel_onf4用于实现类CL_GUI_ALV_GRID的ONF4事实现类,实现方法.
CLASS lcl_app_compt DEFINITION. PUBLIC SECTION. METHODS handel_onf4 FOR EVENT onf4 OF cl_gui_alv_grid IMPORTING e_fieldname e_fieldvalue es_row_no er_event_data et_bad_cells e_display. ENDCLASS. CLASS lcl_app_compt IMPLEMENTATION. METHOD handel_onf4. PERFORM frm_onf4 USING e_fieldname e_fieldvalue es_row_no er_event_data et_bad_cells e_display. ENDMETHOD. ENDCLASS. *******frm_onf4************************************ FORM frm_onf4 USING p_fieldname TYPE lvc_fname p_fieldvalue TYPE lvc_value ps_row_no TYPE lvc_s_roid pr_event_data TYPE REF TO cl_alv_event_data pt_bad_cells TYPE lvc_t_modi p_display TYPE char01. FIELD-SYMBOLS:<ft_modi> TYPE lvc_t_modi. TYPES:BEGIN OF ty_sex, name TYPE char2, END OF ty_sex. DATA lt_sexf4 TYPE STANDARD TABLE OF ty_sex. DATA: ls_modi TYPE lvc_s_modi. DATA lt_ret_tab TYPE TABLE OF ddshretval. DATA ls_ret_tab TYPE ddshretval. lt_sexf4 = VALUE #( ( name = '女生' ) ( name = '男生' ) ). IF p_fieldname EQ 'SEX'. CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING * DDIC_STRUCTURE = ' ' retfield = 'SEX' * PVALKEY = ' ' * DYNPPROG = ' ' * DYNPNR = ' ' * DYNPROFIELD = ' ' * STEPL = 0 * WINDOW_TITLE = * VALUE = ' ' value_org = 'S' * MULTIPLE_CHOICE = ' ' * DISPLAY = ' ' * CALLBACK_PROGRAM = ' ' * CALLBACK_FORM = ' ' * CALLBACK_METHOD = * MARK_TAB = * IMPORTING * USER_RESET = TABLES value_tab = lt_sexf4 * field_tab = return_tab = lt_ret_tab. * DYNPFLD_MAPPING = * EXCEPTIONS * PARAMETER_ERROR = 1 * NO_VALUES_FOUND = 2 * OTHERS = 3 IF sy-subrc <> 0. * Implement suitable error handling here ELSE. READ TABLE lt_ret_tab INTO ls_ret_tab INDEX 1. IF sy-subrc = 0. ls_modi-row_id = ps_row_no-row_id. ls_modi-fieldname = p_fieldname. ls_modi-value = ls_ret_tab-fieldval. ASSIGN pr_event_data->m_data->* TO <ft_modi>. APPEND ls_modi TO <ft_modi>. ENDIF. pr_event_data->m_event_handled = 'X'. ENDIF. ENDIF. ENDFORM.
2、FIELDCAT中字段属性设置可搜索帮助f4availabl = 'X'
FORM build_fieldcat . gt_fieldcat = VALUE #( ( fieldname = 'CODE' scrtext_l = '学号' outputlen = '10' fix_column = 'X' emphasize = 'C100' ) ( fieldname = 'NAME' scrtext_l = '姓名' outputlen = '30' fix_column = 'X' ) ( fieldname = 'SEX' scrtext_l = '姓别' outputlen = '30' fix_column = 'X' f4availabl = 'X' ) "F4帮助属性设置 ( fieldname = 'SCHOOL' scrtext_l = '学校' outputlen = '30' fix_column = 'X' hotspot = 'X' ) ( fieldname = 'WEIGH' scrtext_l = '重量' outputlen = '30' fix_column = 'X' edit = 'X' decimals = 2 ) ( fieldname = 'UNIT' scrtext_l = '单位' outputlen = '30' fix_column = 'X' ) ). ENDFORM.
3、注册F4搜索帮助事件handel_onf4 及register_f4_for_fields
FORM set_handler . DATA lt_f4 TYPE lvc_t_f4 WITH HEADER LINE. CREATE OBJECT event_handel. SET HANDLER event_handel->handel_toolbar FOR gs_alv. SET HANDLER event_handel->handel_user_conmmand FOR gs_alv. SET HANDLER event_handel->header_hotspot_click FOR gs_alv. SET HANDLER event_handel->header_double_click FOR gs_alv. SET HANDLER event_handel->header_data_changed FOR gs_alv. SET HANDLER event_handel->handel_top_of_page FOR gs_alv. SET HANDLER event_handel->handel_onf4 FOR gs_alv. "按enter键时触发DATA CHANGGED 事件 gs_alv->register_edit_event( EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_enter ). lt_f4[] = VALUE #( ( fieldname = 'SEX'register = 'X' chngeafter = 'X') ). CALL METHOD gs_alv->register_f4_for_fields EXPORTING it_f4 = lt_f4[]. ENDFORM.