【IT168 技术文章】
一、单元格定义为按钮:
1、ALV内表中首先在显示数据内表中插入类型为LVC_T_STYL的内表。
即增加一个字段CELLSTYLESTYPELVC_T_STYL。
2、将单元格设置为按钮:(eg:字段ZDISPLAY设置为按钮)
DATA:LV_STYLEROWTYPELVC_S_STYL.
LOOPATITAB_ALVDATAINTOWA_ALVDATA.
“ITAB_ALVDATA为ALV内表
LV_STYLEROW-FIELDNAME='ZDISPLAY'.
LV_STYLEROW-STYLE.=CL_GUI_ALV_GRID=>MC_STYLE_BUTTON.
APPENDLV_STYLEROWTOWA_ALVDATA-CELLSTYLES.
ENDLOOP.
3、在layoutstructure中指明显示数据内表中表示样式的字段。
GV_LAYOUT-STYLEFNAME='CELLSTYLES'.
二、按钮事件的触发调用
如果是用OO方式调用的ALV,那么很简单,只需要对对象注册事件即可,这类的资料也很多,但是对于CALLFUNCTION方式调用的ALV来说,我们可以用下列方法来处理。
1、建事件CLASS
添加BUTTON_CLICK事件
CLASSLCL_EVENT_RECEIVERDEFINITION.
PUBLICSECTION.
METHODS:
HANDLE_BUTTON_CLICK
FOREVENTBUTTON_CLICKOFCL_GUI_ALV_GRID
IMPORTINGES_COL_IDES_ROW_NO,
ENDCLASS.
CLASSLCL_EVENT_RECEIVERIMPLEMENTATION.
METHODHANDLE_BUTTON_CLICK.
PERFORMBUTTON_CLICKUSINGES_COL_IDES_ROW_NO.
ENDMETHOD.
ENDCLASS.
DATAGV_EVENT_RECEIVERTYPEREFTOLCL_EVENT_RECEIVER.
2、设置ALV的EVENT事件(caller_exit)
FORMFM_EVENT_BUILD.
DATALV_EVENTLIKELINEOFGV_EVENTS.
CALLFUNCTION'REUSE_ALV_EVENTS_GET'
EXPORTING
I_LIST_TYPE=0
IMPORTING
ET_EVENTS=GV_EVENTS
EXCEPTIONS
LIST_TYPE_WRONG=1
OTHERS=2
.
IFSY-SUBRC<>0.
*MESSAGEIDSY-MSGIDTYPESY-MSGTYNUMBERSY-MSGNO
*WITHSY-MSGV1SY-MSGV2SY-MSGV3SY-MSGV4.
ENDIF.
LV_EVENT-NAME='CALLER_EXIT'.
LV_EVENT-FORM='FM_BUTTON'.
APPENDLV_EVENTTOGV_EVENTS.
ENDFORM.
3、在上面caller_exit定义的FORM.:'FM_BUTTON'里通过函数‘GET_GLOBALS_FROM_SLVC_FULLSCR’得到ALV的GRID对象,再设置这个GRID的BUTTON_CLICK事件。
DATAGV_GRIDTYPEREFTOCL_GUI_ALV_GRID.
FORMFM_BUTTONUSINGE_GRIDTYPESLIS_DATA_CALLER_EXIT.
CALLFUNCTION'GET_GLOBALS_FROM_SLVC_FULLSCR'
*EXPORTING
*IR_SALV_FULLSCREEN_ADAPTER=
IMPORTING
*ET_EXCLUDING=
*E_REPID=
*E_CALLBACK_PROGRAM=
*E_CALLBACK_ROUTINE=
E_GRID=GV_GRID
*ET_FIELDCAT_LVC=
*ER_TRACE=
*E_FLG_NO_HTML=
*ES_LAYOUT_KKBLO=
*ES_SEL_HIDE=
*ET_EVENT_EXIT=
*ER_FORM_TOL=
*ER_FORM_EOL=
.
CREATEOBJECTGV_EVENT_RECEIVER.
SETHANDLERGV_EVENT_RECEIVER->HANDLE_BUTTON_CLICKFORGV_GRID.
ENDFORM.
4、在CLASS中定义的BUTTON_CLICK事件的FORM:BUTTON_CLICK中写需要单击按钮后完成的处理。
FORMBUTTON_CLICKUSINGP_COL_ID
P_ROW_NOTYPElvc_s_roid.
READTABLEITAB_ALVDATAINTOWA_ALVDATAINDEXP_ROW_NO-ROW_ID.
PERFORMFM_DISPLAY_COMMENT.
ENDFORM.
5、DISPLAYALV
PERFORMFM_EVENT_BUILD.
*CallALVFuncation
CALLFUNCTION'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM=SY-REPID
I_GRID_TITLE=GV_TITLE
I_DEFAULT='X'
I_SAVE='A'
IT_EVENTS=GV_EVENTS
IT_FIELDCAT_LVC=GV_FIELDCAT_ALV[]
IS_LAYOUT_LVC=GV_LAYOUT
I_CALLBACK_PF_STATUS_SET=GV_STATUS
I_CALLBACK_USER_COMMAND='USER_COMMAND'
TABLES
T_OUTTAB=ITAB_ALVDATA
EXCEPTIONS
PROGRAM_ERROR=1
OTHERS=2.