刷新ALV屏幕与刷新ALV输出内表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
刷新ALV屏幕与刷新ALV输出内表
前两天一网友问我如何将ALV屏幕上更改的内容更新到内表中,我直接给了一段代码。
这位可怜的老兄竟然没有找到地方,我很是受伤,没有再给予理会。
后来想想,感觉有点太过分了,决定在博客上详细说明一下:
首先找到如下代码:
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
i_structure_name = 'TYP_RESULT'
i_grid_title = g_title
i_grid_settings = g_setting
i_callback_user_command = 'SUB_USER_COMMAND'
i_callback_pf_status_set = 'SUB_SET_PF_STATUS'
i_callback_top_of_page = 'SUB_TOP_OF_PAGE'
i_save = g_save
is_layout = wa_layout
it_fieldcat = gt_fieldcat[] TABLES
t_outtab = gt_result EXCEPTIONS
program_error = 1
OTHERS = 2.
程序中标红的部分是传递的一个子程序的名称,这个子程序用来处理你在ALV状态栏上新增的功能操作。
此子程序代码如下:
*@--------------------------------------------------------------------*
*@ Form sub_user_command
*@--------------------------------------------------------------------*
* -->R_UCOMM 事务功能码
* -->RS_SELFIELD ALV相关的数据
*---------------------------------------------------------------------*
FORM sub_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE sl is_selfield.
CASE r_ucomm.
WHEN '&IC1'. "双击事件的功能码
WHEN 'CLEAR'.
PERFORM sub_ucomm_clear. "清空操作
ENDCASE.
* 刷新ALV报表
* CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
* IMPORTING
* e_grid = g_grid.
* CALL METHOD g_grid->check_changed_data.
* CALL METHOD g_grid->refresh_table_display.
rs_selfield-refresh = 'X'.
ENDFORM. "sub_user_command
以上子程序传递的参数是固定,不用理会。
程序中注释掉的部分是当输出的内表内容发生变化时,我们将变化的内容刷新显示。
如果ALV是可编辑的,编辑相应的单元格后,只要有标红的那一句参数设置,就可以实现内表内容的刷新。
DA TA : L_GRID TYPE REF TO CL_GUI_ALV_GRID.
FORM ALV_USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
CASE R_UCOMM.
WHEN 'UPDATE'.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = L_GRID.
CALL METHOD L_GRID->CHECK_CHANGED_DA TA.
ENDFORM.
没有测试过,请自行测试一下!
数据库没有更新
代码:
DATA: OK_CODE TYPE SY-UCOMM,
SAVE_OK TYPE SY-UCOMM.
TABLES SPFLI.
DATA LS_SPFLI TYPE SPFLI.
DATA LINEA TYPE I.
DATA WA_SPFLI TYPE TABLE OF SPFLI.
DATA WADEL_SPFLI TYPE TABLE OF SPFLI.
SELECT * INTO TABLE WA_SPFLI FROM SPFLI.
DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA WA_LAYOUT TYPE LVC_S_LAYO.
WA_LAYOUT-EDIT = 'X'.
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
TYPES: DEL_ROWS TYPE STANDARD TABLE OF SPFLI.
DATA: DDEL_ROWS TYPE STANDARD TABLE OF SPFLI.
METHODS:
HANDLE_DATA_CHANGED
FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
METHODS:
UPDATE_DELTA_TABLES
IMPORTING
PR_DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL. METHODS:
GET_DELETED_ROWS
EXPORTING
DELETED_ROWS TYPE DEL_ROWS.
ENDCLASS.
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_DATA_CHANGED.
CALL METHOD UPDATE_DELTA_TABLES( ER_DATA_CHANGED ). ENDMETHOD.
METHOD UPDATE_DELTA_TABLES.
DATA: L_DEL_ROW TYPE LVC_S_MOCE.
LOOP AT PR_DATA_CHANGED->MT_DELETED_ROWS INTO L_DEL_ROW. READ TABLE WA_SPFLI INTO LS_SPFLI INDEX L_DEL_ROW-ROW_ID.
IF SY-SUBRC NE 0.
MESSAGE E208(00) WITH '处理错误'.
ELSE.
APPEND LS_SPFLI TO DDEL_ROWS.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD GET_DELETED_ROWS.
DELETED_ROWS = ME->DDEL_ROWS.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
CALL SCREEN 100.
WRITE / '删除的内表记录'.
WRITE / '______________'.
CALL METHOD EVENT_RECEIVER->GET_DELETED_ROWS
IMPORTING
DELETED_ROWS = WADEL_SPFLI.
LOOP AT WADEL_SPFLI INTO SPFLI.
WRITE: / SPFLI-CARRID,SPFLI-CONNID,SPFLI-CITYFROM.
ENDLOOP.
WRITE: / '更新后的内表记录:'.
WRITE: / '__________________'.
LOOP AT WA_SPFLI INTO SPFLI.
WRITE : / SPFLI-CARRID,SPFLI-CONNID,SPFLI-CITYFROM.
ENDLOOP.
END-OF-SELECTION.
MODULE STATUS_0100 OUTPUT.
IF WA_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT WA_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = WA_CONTAINER.
CREATE OBJECT ALV_GRID
EXPORTING I_PARENT = WA_CUSTOM_CONTAINER.
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR ALV_GRID.
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'SPFLI'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = WA_SPFLI.
ENDIF.
ENDMODULE.
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'EXIT'.
DATA L_RET VALUE 'X'.
CALL METHOD ALV_GRID->CHECK_CHANGED_DATA IMPORTING
E_VALID = L_RET.
LEAVE TO SCREEN 0.
WHEN 'SAVE'.
MODIFY SPFLI FROM TABLE wa_spfli.
IF SY-SUBRC NE 0.
MESSAGE I004(YMESS) WITH '更新数据错误!'. EXIT.
ELSE.
MESSAGE I004(YMESS) WITH '更新数据OK!'.
ENDIF.
DELETE SPFLI FROM TABLE WADEL_SPFLI.
IF SY-SUBRC NE 0.
MESSAGE I004(YMESS) WITH '更新数据错误!'. EXIT.
ELSE.
MESSAGE I004(YMESS) WITH '更新数据OK!'.
ENDIF.
ENDCASE.
ENDMODULE.。