结果
代码
REPORT ZGUO_TEST.
*调用数据表
TABLES: SPFLI.
*定义、申明变量
TYPE-POOLS: SLIS.
DATA:GT_FIELDCAT TYPE LVC_T_FCAT,GS_FIELDCAT TYPE LVC_S_FCAT,ITEM TYPE I,
LS_LAYOUT TYPE LVC_S_LAYO,
GT_EVENT TYPE SLIS_T_EVENT WITH HEADER LINE.
DATA:LT_EDIT TYPE LVC_T_STYL, LS_STYLE TYPE LVC_S_STYL.
DATA:GV_GRID TYPE REF TO CL_GUI_ALV_GRID,LS_STABLE TYPE LVC_S_STBL.
DATA:COLOR TYPE SLIS_T_SPECIALCOL_ALV WITH HEADER LINE.
TYPES:BEGIN OF SSPFLI .
INCLUDE TYPE SPFLI.
TYPES:AMATNR TYPE C LENGTH 5,
BING TYPE C LENGTH 5,
COLOR(4) TYPE C,
CELLSTYLES TYPE LVC_T_STYL,
ITEM TYPE I,"隐藏/显示 ID
END OF SSPFLI.
DATA:T_SPFLI TYPE TABLE OF SSPFLI WITH HEADER LINE,
T_SPFLI1 TYPE TABLE OF SPFLI WITH HEADER LINE.
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_BUTTON_CLICK
FOR EVENT BUTTON_CLICK OF CL_GUI_ALV_GRID
IMPORTING ES_COL_ID ES_ROW_NO.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_BUTTON_CLICK.
PERFORM BUTTON_CLICK USING ES_COL_ID ES_ROW_NO .
ENDMETHOD. "HANDLE_BUTTON_CLICK
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
DATA GT_EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER .
INITIALIZATION.
START-OF-SELECTION.
PERFORM FRM_LOAD_DATA.
*&---------------------------------------------------------------------*
*& Form FRM_LOAD_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM FRM_LOAD_DATA.
PERFORM FRM_FIELD.
CLEAR T_SPFLI.REFRESH T_SPFLI.
SELECT * INTO CORRESPONDING FIELDS OF TABLE T_SPFLI FROM SPFLI.
LS_LAYOUT-INFO_FNAME = 'COLOR'.
LS_LAYOUT-STYLEFNAME = 'CELLSTYLES'.
GT_EVENT-NAME = 'CALLER_EXIT'. "slis_ev_caller_exit_at_start事件
GT_EVENT-FORM = 'FM_BUTTON'. "FROM 事件
APPEND GT_EVENT .
SORT T_SPFLI BY CARRID.
DELETE ADJACENT DUPLICATES FROM T_SPFLI COMPARING CARRID.
CLEAR:LT_EDIT,ITEM.REFRESH LT_EDIT.
LOOP AT T_SPFLI.
ITEM = ITEM + 1.
T_SPFLI-BING = '>'.
T_SPFLI-ITEM = ITEM.
CLEAR T_SPFLI-CITYTO.
LS_STYLE-FIELDNAME = 'AMATNR' .
LS_STYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_BUTTON .
INSERT LS_STYLE INTO TABLE LT_EDIT .
T_SPFLI-CELLSTYLES = LT_EDIT .
T_SPFLI-AMATNR = '+'.
MODIFY T_SPFLI TRANSPORTING CELLSTYLES BING AMATNR CITYTO ITEM.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT_LVC = LS_LAYOUT
IT_FIELDCAT_LVC = GT_FIELDCAT
IT_EVENTS = GT_EVENT[]
TABLES
T_OUTTAB = T_SPFLI[]
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM. "FRM_LOAD_DATA
*显示栏位
FORM FRM_FIELD.
REFRESH: GT_FIELDCAT.
CLEAR: GT_FIELDCAT.
*显示数据表格ALV的栏位
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'CARRID'.
GS_FIELDCAT-COLTEXT = 'Airline'.
GS_FIELDCAT-OUTPUTLEN = 8.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'CONNID'.
GS_FIELDCAT-COLTEXT = 'Connection Number'.
GS_FIELDCAT-OUTPUTLEN = 8.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'COUNTRYFR'.
GS_FIELDCAT-COLTEXT = 'Country Key'.
GS_FIELDCAT-OUTPUTLEN = 6.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'CITYFROM'.
GS_FIELDCAT-COLTEXT = 'Departure city'.
GS_FIELDCAT-OUTPUTLEN = 12.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'AIRPFROM'.
GS_FIELDCAT-COLTEXT = 'Departure airport'.
GS_FIELDCAT-OUTPUTLEN = 12.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'AMATNR'.
GS_FIELDCAT-COLTEXT = '隐藏/显示'.
GS_FIELDCAT-OUTPUTLEN = 9.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'BING'.
GS_FIELDCAT-COLTEXT = ''.
GS_FIELDCAT-OUTPUTLEN = 5.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = 'CITYTO'.
GS_FIELDCAT-COLTEXT = 'Arrival city'.
GS_FIELDCAT-OUTPUTLEN = 15.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
ENDFORM. "FRM_FIELD
*&---------------------------------------------------------------------*
*& Form FM_BUTTON
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->E_GRID text
*----------------------------------------------------------------------*
FORM FM_BUTTON USING E_GRID TYPE SLIS_DATA_CALLER_EXIT.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
E_GRID = GV_GRID.
* CALL METHOD ref_grid->check_changed_data.
* 设置enter事件
CALL METHOD GV_GRID->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED"MC_EVT_ENTER
EXCEPTIONS
ERROR = 1
OTHERS = 2.
CREATE OBJECT GT_EVENT_RECEIVER.
SET HANDLER GT_EVENT_RECEIVER->HANDLE_BUTTON_CLICK FOR GV_GRID.
ENDFORM. "FM_BUTTON
*&---------------------------------------------------------------------*
*& Form BUTTON_CLICK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_COL_ID text
* -->P_ROW_NO text
*----------------------------------------------------------------------*
FORM BUTTON_CLICK USING P_COL_ID
P_ROW_NO TYPE LVC_S_ROID.
DATA:STRTABIX TYPE I,STRTABIX1 TYPE I.
IF P_COL_ID = 'AMATNR'.
CLEAR:T_SPFLI .CLEAR LT_EDIT.REFRESH LT_EDIT.
READ TABLE T_SPFLI INDEX P_ROW_NO-ROW_ID.
IF SY-SUBRC = 0 AND T_SPFLI-AMATNR = '+'."点击按钮 ,显示隐藏数据
STRTABIX = P_ROW_NO-ROW_ID.
CLEAR T_SPFLI1.REFRESH T_SPFLI1.
SELECT * INTO CORRESPONDING FIELDS OF TABLE T_SPFLI1 FROM SPFLI WHERE CARRID = T_SPFLI-CARRID .
LOOP AT T_SPFLI1 .
STRTABIX = STRTABIX + 1.
STRTABIX1 = STRTABIX1 + 1.
CLEAR T_SPFLI.
T_SPFLI-BING = '>>'.
IF STRTABIX1 = 1."渐变颜色
T_SPFLI-COLOR = 'C300'.
ELSE.
T_SPFLI-COLOR = 'C700'.
CLEAR STRTABIX1.
ENDIF.
T_SPFLI-CARRID = T_SPFLI1-CARRID .
T_SPFLI-CITYTO = T_SPFLI1-CITYTO.
INSERT T_SPFLI INTO T_SPFLI INDEX STRTABIX.
ENDLOOP.
LS_STYLE-FIELDNAME = 'AMATNR' .
LS_STYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_BUTTON .
INSERT LS_STYLE INTO TABLE LT_EDIT .
T_SPFLI-CELLSTYLES = LT_EDIT .
T_SPFLI-AMATNR = '-'.
T_SPFLI-BING = '>'.
MODIFY T_SPFLI INDEX P_ROW_NO-ROW_ID TRANSPORTING CELLSTYLES AMATNR BING.
ELSEIF SY-SUBRC = 0 AND T_SPFLI-AMATNR = '-'."隐藏数据
DELETE T_SPFLI WHERE CARRID = T_SPFLI-CARRID AND ITEM = 0.
CLEAR LT_EDIT.REFRESH LT_EDIT.
LS_STYLE-FIELDNAME = 'AMATNR' .
LS_STYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_BUTTON .
INSERT LS_STYLE INTO TABLE LT_EDIT .
T_SPFLI-CELLSTYLES = LT_EDIT .
T_SPFLI-AMATNR = '+'.
T_SPFLI-BING = '>'.
MODIFY T_SPFLI INDEX P_ROW_NO-ROW_ID TRANSPORTING CELLSTYLES AMATNR BING.
ENDIF.
ENDIF.
LS_STABLE-ROW = 'X'." 基于行的稳定刷新
LS_STABLE-COL = 'X'." 基于列稳定刷新
CALL METHOD GV_GRID->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = LS_STABLE.
ENDFORM. "BUTTON_CLICK