一、Function ALV
Function ALV 从名字中可以推出,是由 Function(函数)形式提供,因此开发者只需适当使用以有的功能即可。没有必要创建屏幕也没有必要一一写出代码,因此可认为是生产性比较高的方法。
例:
*function ALV GRID和LIST报表输出
DATA gt_itab TYPE TABLE OF sflight.
SELECT * FROM sflight INTO TABLE gt_itab.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "grid报表
*CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' "list报表
EXPORTING
i_structure_name = 'SFLIGHT'
TABLES
t_outtab = gt_itab.
二、详细了解FUNCTION ALV函数
1、reuse_alv_grid_display
(1)I_CALLBACK_PROGRAM参数
调用ALV函数返回的程序
(2)I_CALLBACK_PF_STATUS_SET参数
设置ALV程序内的STATUS
(3)I_CALLBACK_USER_COMMAND参数
实现ALV程序的按钮功能,FORM语句名
(4)I_STRUCTURE_NAME参数
设置ALV结构:结构体、表
(5)I_GRID_TITLE参数
ALV GRID的表头标题
(6)I_GRID_SETTINGS参数
设置ALV GRID(编辑时立即调用事件等)
(7)IS_LAYOUT参数
设置ALV布局
(8)IT_FIELDCAT参数
设置ALV的构成项目字段(字段目录)
(9)IT_EXCLUDING参数
删除ALV基本按钮的项目
(10)IT_SORT参数
显示ALV 时设置基本排序字段
(11)IT_FILTER参数
显示ALV 时设置过滤字段
(12)I_SAVE参数
保存ALV布局的选项设置成保存
(13)IS_VARIANT参数
ALV布局后调用的模式定义ALV事件
(14)T_OUTTAB参数
ALV上要输出的数据
三、练习
TYPE-POOLS:slis.
DATA:gt_fieldcat TYPE slis_t_fieldcat_alv,
gs_fieldcat TYPE slis_fieldcat_alv.
DATA:gt_events TYPE slis_t_event,
gs_event TYPE slis_alv_event.
DATA:gs_layout TYPE slis_layout_alv,
gs_glay TYPE lvc_s_glay.
*为了在内表中追加复选框定义结构体类型
TYPES BEGIN OF t_line.
INCLUDE STRUCTURE sflight.
TYPES checkbox TYPE c.
TYPES END OF t_line.
*定义参照结构体类型的内表
DATA gt_itab TYPE TABLE OF t_line WITH HEADER LINE.
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_itab.
MOVE 'PF_STATUS_SET' TO gs_event-name.
MOVE 'ALV_STATUS_SET' TO gs_event-form.
APPEND gs_event TO gt_events.
MOVE 'DATA_CHANGED' TO gs_event-name.
MOVE 'ALV_DATA_CHANGED' TO gs_event-form.
APPEND gs_event TO gt_events.
*字段目录中追加复选框字段。设置为复选框checkbox属性设置成X,编辑模式edit属性设置成X
CLEAR: gs_fieldcat,gt_fieldcat.
gs_fieldcat-col_pos = 1.
gs_fieldcat-fieldname = 'CHECKBOX'.
gs_fieldcat-checkbox = 'X'.
gs_fieldcat-edit = 'X'.
gs_fieldcat-seltext_m = 'Sel'.
APPEND gs_fieldcat TO gt_fieldcat.
*构造其他必要字段目录
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 2.
gs_fieldcat-fieldname = 'CARRID'.
gs_fieldcat-checkbox = 'Airline Code'.
APPEND gs_fieldcat TO gt_fieldcat.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 3.
gs_fieldcat-fieldname = 'CONNID'.
gs_fieldcat-checkbox = 'Connection Number'.
APPEND gs_fieldcat TO gt_fieldcat.
CLEAR gs_fieldcat.
gs_fieldcat-col_pos = 4.
gs_fieldcat-fieldname = 'FLDATE'.
gs_fieldcat-checkbox = '起飞日期'.
APPEND gs_fieldcat TO gt_fieldcat.
gs_glay-edt_cll_cb = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
i_grid_settings = gs_glay
it_fieldcat = gt_fieldcat
it_events = gt_events
is_layout = gs_layout
i_callback_user_command = 'ALV_USER_COMMAND'
TABLES
t_outtab = gt_itab.
FORM alv_user_command USING p_ucomm LIKE sy-ucomm
p_selfield TYPE slis_selfield.
CASE p_ucomm.
WHEN '&IC1'.
READ TABLE gt_itab INDEX p_selfield-tabindex.
gt_itab-checkbox = 'X'.
MODIFY gt_itab INDEX p_selfield-tabindex.
WHEN 'SAVE'.
MODIFY sflight FROM TABLE gt_itab.
IF sy-subrc EQ 0.
COMMIT WORK.
ELSE.
ROLLBACK WORK.
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDFORM.
FORM alv_status_set USING pt_extab TYPE slis_t_extab.
CLEAR:pt_extab[].
SET PF-STATUS 'STANDARD_FULLSCREEN'.LSLVC_FULLSCREENEVT
ENDFORM.
FORM alc_data_changed USING pcl_data TYPE REF TO cl_alv_changed_data_protocol.
DATA:l_name(20),
ls_cells TYPE lvc_s_modi.
FIELD-SYMBOLS <fs_value>.
LOOP AT pcl_data->mt_mod_cells INTO ls_cells.
CLEAR gt_itab.
READ TABLE gt_itab INDEX ls_cells-row_id.
CONCATENATE 'GT_ITAB-' ls_cells-fieldname INTO l_name.
ASSIGN (l_name) TO <fs_value>.
<fs_value> = ls_cells-value.
MODIFY gt_itab INDEX ls_cells-row_id.
ENDLOOP.
ENDFORM.