SAP ALV模板
效果图:ALV显示自动优化,更新后数据自动回写内表,日期栏位输入帮助,获取长文本数据
代码如下:
REPORT Z_ALV_MODULE.
TABLES:VEPO,EKKO,LIPS,LIKP. *ALV结构 TYPES: BEGIN OF TY_ALV, BOX TYPE C, ZFMK TYPE C, BOLNR TYPE LIKP-BOLNR, VBELN TYPE LIKP-VBELN, POSNR TYPE LIPS-POSNR, MATNR TYPE LIPS-MATNR, VGBEL TYPE LIPS-VGBEL, VGPOS TYPE LIPS-VGPOS, ERDAT TYPE LIKP-ERDAT, LFIMG TYPE LIPS-LFIMG, DAYS TYPE I, CODE TYPE STRING, DOWN TYPE C, DD_HANDLE TYPE INT4,"下拉列表 END OF TY_ALV. TYPES:BEGIN OF TY_BOLNR, ZBOLNR TYPE ZBOLNR, ZCLANO TYPE ZCLANO, END OF TY_BOLNR. *----------------------------------------------------------------------* * 定义ALV数据对象 *----------------------------------------------------------------------* DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV, W_FIELDCAT TYPE SLIS_FIELDCAT_ALV, W_LAYOUT TYPE SLIS_LAYOUT_ALV, GS_SETTINGS TYPE LVC_S_GLAY. DATA: GT_FIELDCAT TYPE LVC_T_FCAT, GW_FIELDCAT TYPE LVC_S_FCAT, GW_LAYOUT TYPE LVC_S_LAYO, GT_DDVAL TYPE LVC_T_DROP, GW_DDVAL TYPE LVC_S_DROP, GT_EVENTS TYPE SLIS_T_EVENT, GW_EVENTS TYPE SLIS_ALV_EVENT. DATA: GW_ALV TYPE TY_ALV, GT_ALV TYPE STANDARD TABLE OF TY_ALV, ZNAME TYPE THEAD-TDNAME. DATA: GT_BOLNR TYPE TABLE OF TY_BOLNR, WA_BOLNR TYPE TY_BOLNR. DATA: STBL TYPE LVC_S_STBL, G_GRID TYPE REF TO CL_GUI_ALV_GRID. *----------------------------------------------------------------------* * CLASS lcl_event_handler DEFINITION *----------------------------------------------------------------------* * 类定义 *----------------------------------------------------------------------* CLASS LCL_EVENT_HANDLER DEFINITION."类定义 PUBLIC SECTION. METHODS: * 第一屏alv数据改变 HANDLE_DATA_CHANGED FOR EVENT DATA_CHANGED_FINISHED OF CL_GUI_ALV_GRID IMPORTING E_MODIFIED ET_GOOD_CELLS. ENDCLASS. "lcl_event_handler DEFINITION *----------------------------------------------------------------------* * CLASS lcl_event_handler IMPLEMENTATION *----------------------------------------------------------------------* * 类实现 *----------------------------------------------------------------------* CLASS LCL_EVENT_HANDLER IMPLEMENTATION."类实现 METHOD HANDLE_DATA_CHANGED. PERFORM HANDLE_DATA_CHANGED . IF E_MODIFIED = 'X'. STBL-ROW = 'X'." 基于行的稳定刷新 STBL-COL = 'X'." 基于列稳定刷新 CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY EXPORTING IS_STABLE = STBL. ENDIF. ENDMETHOD. ENDCLASS. "lcl_event_handler IMPLEMENTATION *&---------------------------------------------------------------------* *& 选择屏幕定义 *&---------------------------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. SELECT-OPTIONS: S_BOLNR FOR LIKP-BOLNR. "提单 SELECTION-SCREEN SKIP. PARAMETERS:P_DP RADIOBUTTON GROUP G1 DEFAULT 'X', P_ED RADIOBUTTON GROUP G1 . SELECTION-SCREEN END OF BLOCK B1. *&---------------------------------------------------------------------* *& 执行查询 *&---------------------------------------------------------------------* START-OF-SELECTION. PERFORM FRM_GET_DATA. PERFORM FRM_ALV_DISPLAY. *&---------------------------------------------------------------------* *& Form FRM_GET_DATA *&---------------------------------------------------------------------* * 获取数据源 *----------------------------------------------------------------------* FORM FRM_GET_DATA . SELECT DISTINCT L~BOLNR, L~VBELN, L~ERDAT, P~POSNR, P~MATNR, P~VGBEL, P~VGPOS, P~LFIMG INTO CORRESPONDING FIELDS OF TABLE @GT_ALV FROM LIKP AS L LEFT JOIN LIPS AS P ON P~VBELN = L~VBELN WHERE L~BOLNR IN @S_BOLNR. CLEAR:GW_ALV. LOOP AT GT_ALV INTO GW_ALV. CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS' EXPORTING i_date_from = GW_ALV-ERDAT i_date_to = sy-datum " IMPORTING e_days = GW_ALV-DAYS."天数 CLEAR:ZNAME. ZNAME = GW_ALV-VBELN. * PERFORM FRM_READ_TXT USING 'F01' ZNAME 'EKKO' SY-LANGU CHANGING TNM. * IF TNM IS NOT INITIAL. * GW_ALV-CODE = TNM+0(3). * ENDIF. GW_ALV-CODE = 'XXXYMMZMOBQ'. MODIFY GT_ALV FROM GW_ALV. ENDLOOP. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_ALV_DISPLAY *&---------------------------------------------------------------------* * 显示ALV报表 *----------------------------------------------------------------------* FORM FRM_ALV_DISPLAY . PERFORM FRM_FILL_FIELDCAT. PERFORM FRM_ALV_LAYOUT. PERFORM FRM_ALV_INIT_SETTINGS. PERFORM CREATE_DROPDOWN_VALUES. PERFORM FRM_SET_ALV_EVENTS CHANGING GT_EVENTS. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING I_CALLBACK_PROGRAM = SY-REPID I_CALLBACK_PF_STATUS_SET = 'PF_STATUS_SET' I_CALLBACK_USER_COMMAND = 'FRM_USER_COMMAND' I_GRID_SETTINGS = GS_SETTINGS IS_LAYOUT_LVC = GW_LAYOUT IT_FIELDCAT_LVC = GT_FIELDCAT IT_EVENTS = GT_EVENTS I_SAVE = 'A' TABLES T_OUTTAB = GT_ALV EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_FILL_FIELDCAT *&---------------------------------------------------------------------* * 填充报表结构 *----------------------------------------------------------------------* FORM FRM_FILL_FIELDCAT . PERFORM FM_APPEND_FIELDCAT USING 'ZFMK' '提单标识'. PERFORM FM_APPEND_FIELDCAT USING 'BOLNR' '提单号码'. PERFORM FM_APPEND_FIELDCAT USING 'VBELN' '交货单号'. PERFORM FM_APPEND_FIELDCAT USING 'POSNR' '交货项次'. PERFORM FM_APPEND_FIELDCAT USING 'MATNR' '物料号码'. PERFORM FM_APPEND_FIELDCAT USING 'VGBEL' '采购订单'. PERFORM FM_APPEND_FIELDCAT USING 'VGPOS' '订单项次'. PERFORM FM_APPEND_FIELDCAT USING 'ERDAT' '提单日期'. PERFORM FM_APPEND_FIELDCAT USING 'LFIMG' '提单重量'. PERFORM FM_APPEND_FIELDCAT USING 'DAYS' '保存天数'. PERFORM FM_APPEND_FIELDCAT USING 'CODE' '厂商编码'. PERFORM FM_APPEND_FIELDCAT USING 'DOWN' '是否自提'. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_ALV_LAYOUT *&---------------------------------------------------------------------* * 显示报表结构 *----------------------------------------------------------------------* FORM FRM_ALV_LAYOUT . GW_LAYOUT-CWIDTH_OPT = 'X'. GW_LAYOUT-ZEBRA = 'X'. GW_LAYOUT-SEL_MODE = 'D'. GW_LAYOUT-BOX_FNAME = 'BOX'. ENDFORM. *&---------------------------------------------------------------------* *& Form FM_APPEND_FIELDCAT_PRO *&---------------------------------------------------------------------* * 定义性能填充模式 *----------------------------------------------------------------------* FORM FM_APPEND_FIELDCAT USING VALUE(FILED_NAME) VALUE(FILED_TEXT). GW_FIELDCAT-FIELDNAME = FILED_NAME. GW_FIELDCAT-COLTEXT = FILED_TEXT. GW_FIELDCAT-SELTEXT = FILED_TEXT. GW_FIELDCAT-TOOLTIP = FILED_TEXT. IF GW_FIELDCAT-FIELDNAME = 'MATNR'. GW_FIELDCAT-NO_ZERO = 'X'. ELSE. GW_FIELDCAT-NO_ZERO = ''. ENDIF. IF GW_FIELDCAT-FIELDNAME = 'ZFMK'. GW_FIELDCAT-CHECKBOX = 'X'. ELSE. GW_FIELDCAT-CHECKBOX = ''. ENDIF. * IF GW_FIELDCAT-FIELDNAME = 'ZTYPE'."选择对应表数据 * GW_FIELDCAT-F4AVAILABL = 'X'."输入帮助 * GW_FIELDCAT-REF_FIELD = 'SDABW'. * GW_FIELDCAT-REF_TABLE = 'TVSAKT'. * ENDIF. IF GW_FIELDCAT-FIELDNAME = 'ERDAT'."选择日期 GW_FIELDCAT-DATATYPE = 'DATS'. GW_FIELDCAT-INTTYPE = 'D'. GW_FIELDCAT-REF_FIELD = 'LFDAT'. GW_FIELDCAT-REF_TABLE = 'LIKP'. ENDIF. IF GW_FIELDCAT-FIELDNAME = 'DOWN' . GW_FIELDCAT-DRDN_FIELD = 'DD_HANDLE'." ELSE. GW_FIELDCAT-DRDN_FIELD = ''. ENDIF. IF GW_FIELDCAT-FIELDNAME = 'LFIMG'. GW_FIELDCAT-DATATYPE = 'P'."设置数据类型 ENDIF. IF P_ED = 'X'. CASE GW_FIELDCAT-FIELDNAME. WHEN 'ERDAT' OR 'LFIMG' OR 'CODE' OR 'DOWN' OR 'ZFMK'. GW_FIELDCAT-EDIT = 'X'. WHEN OTHERS. GW_FIELDCAT-EDIT = SPACE. ENDCASE. ENDIF. APPEND GW_FIELDCAT TO GT_FIELDCAT. CLEAR GW_FIELDCAT. ENDFORM. *&---------------------------------------------------------------------* *& FORM SET_STATUS GUI状态设置 *&---------------------------------------------------------------------* FORM pf_status_set USING rt_extab TYPE slis_t_extab. SET PF-STATUS 'STANDARD' EXCLUDING rt_extab."显示所有ALV功能按钮 ENDFORM. *&---------------------------------------------------------------------* *& FORM USER_COMMAND 用户自定义按钮 *&---------------------------------------------------------------------* FORM FRM_USER_COMMAND USING r_ucomm LIKE sy-ucomm rs_selfield TYPE slis_selfield. CASE r_ucomm. WHEN 'ZSAVE'. PERFORM frm_save. ENDCASE. rs_selfield-refresh = 'X'. ENDFORM. *&---------------------------------------------------------------------* *& Form 获取长文本 *&---------------------------------------------------------------------* FORM FRM_READ_TXT USING FU_ID TYPE THEAD-TDID FU_NAME TYPE THEAD-TDNAME FU_OBJECT TYPE THEAD-TDOBJECT FU_LANGU TYPE SY-LANGU CHANGING FU_TEXT. DATA: LT_TEXT TYPE TABLE OF TLINE, LT_LINES TYPE TABLE OF TLINE, LW_TEXT LIKE LINE OF LT_TEXT. CALL FUNCTION 'READ_TEXT' EXPORTING CLIENT = SY-MANDT ID = FU_ID LANGUAGE = FU_LANGU NAME = FU_NAME OBJECT = FU_OBJECT ARCHIVE_HANDLE = 0 LOCAL_CAT = ' ' TABLES LINES = LT_TEXT EXCEPTIONS ID = 1 LANGUAGE = 2 NAME = 3 NOT_FOUND = 4 OBJECT = 5 REFERENCE_CHECK = 6 WRONG_ACCESS_TO_ARCHIVE = 7 OTHERS = 8. "将特殊字符正常输出 CALL FUNCTION 'CONVERT_ITF_TO_STREAM_TEXT' EXPORTING LANGUAGE = FU_LANGU * LF = ' ' * IMPORTING * STREAM_LINES = TABLES ITF_TEXT = LT_TEXT TEXT_STREAM = LT_LINES. IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO "不提示 错误信息 * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. *文本拼接 LOOP AT LT_LINES INTO LW_TEXT. CONCATENATE FU_TEXT LW_TEXT INTO FU_TEXT. CLEAR: LW_TEXT. ENDLOOP. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_ALV_INIT_SETTINGS *&---------------------------------------------------------------------* FORM FRM_ALV_INIT_SETTINGS. GS_SETTINGS-EDT_CLL_CB = 'X'."ALV 控制: 退出可编辑单元格时回调 ENDFORM. FORM CREATE_DROPDOWN_VALUES."设置下拉列表值 DATA LV_TABIX TYPE I. CLEAR:GW_ALV. LOOP AT GT_ALV INTO GW_ALV. LV_TABIX = SY-TABIX. GW_DDVAL-HANDLE = LV_TABIX. GW_DDVAL-VALUE = 'Y'. APPEND GW_DDVAL TO GT_DDVAL. CLEAR GW_DDVAL. GW_DDVAL-HANDLE = LV_TABIX. GW_DDVAL-VALUE = 'N'. APPEND GW_DDVAL TO GT_DDVAL. GW_ALV-DD_HANDLE = LV_TABIX. MODIFY GT_ALV FROM GW_ALV. CLEAR GW_ALV. ENDLOOP. ENDFORM. *&---------------------------------------------------------------------* *& Form SET_ALV_EVENTS *&---------------------------------------------------------------------* FORM FRM_SET_ALV_EVENTS CHANGING PT_EVENTS TYPE SLIS_T_EVENT. DATA: LS_EVENTS TYPE SLIS_ALV_EVENT. LS_EVENTS-NAME = 'CALLER_EXIT'. LS_EVENTS-FORM = 'FRM_REGISTER_EVENTS'. APPEND LS_EVENTS TO PT_EVENTS. ENDFORM. *&---------------------------------------------------------------------* *& Form register_events *&---------------------------------------------------------------------* * 注册回车事件 *---------------------------------------------------------------------- FORM FRM_REGISTER_EVENTS USING E_GRID TYPE SLIS_DATA_CALLER_EXIT. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING E_GRID = G_GRID. CALL METHOD g_grid->SET_DROP_DOWN_TABLE EXPORTING IT_DROP_DOWN = GT_DDVAL. *--------------------------------------------------------------------* *回车更新内表 ,可在对应事件中自定义更新内表动作 DATA: GR_EVENT_HANDLER TYPE REF TO LCL_EVENT_HANDLER. CREATE OBJECT GR_EVENT_HANDLER. * CALL METHOD G_GRID->REGISTER_EDIT_EVENT EXPORTING I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER EXCEPTIONS ERROR = 1 OTHERS = 2. SET HANDLER GR_EVENT_HANDLER->HANDLE_DATA_CHANGED FOR G_GRID. *--------------------------------------------------------------------* IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. "register_events FORM HANDLE_DATA_CHANGED . "刷新ALV,优化显示内容,可自定义业务逻辑
" LOOP AT GT_ALV INTO GW_ALV WHERE ZCLANO IS INITIAL.
" GW_ALV-ACMAT = TM_AMT2.
" MODIFY GT_ALV FROM GW_ALV.
" ENDLOOP.
DATA: ls_stbl TYPE lvc_s_stbl, LS_LAYOUT TYPE LVC_S_LAYO. CALL METHOD G_GRID->GET_FRONTEND_LAYOUT IMPORTING es_layout = LS_LAYOUT. LS_LAYOUT-CWIDTH_OPT = 'X'. CALL METHOD G_GRID->SET_FRONTEND_LAYOUT EXPORTING IS_LAYOUT = LS_LAYOUT. ENDFORM. *&---------------------------------------------------------------------* *& Form FRM_SAVE *&---------------------------------------------------------------------* FORM FRM_SAVE . *--------------------------------------------------------------------* *ALV更新后自动回写对应内表,需设置 edt_cll_cb CALL METHOD G_GRID->CHECK_CHANGED_DATA . CALL FUNCTION 'POPUP_CONTINUE_YES_NO' EXPORTING DEFAULTOPTION = 'Y' TEXTLINE1 = '数据已更新' * TEXTLINE2 = ' ' TITEL = 'Infomation' * START_COLUMN = 25 * START_ROW = 6 * IMPORTING * ANSWER = . * "锁定 号码范围 * CALL FUNCTION 'NUMBER_RANGE_ENQUEUE' * EXPORTING * object = 'ZCLA' * EXCEPTIONS * foreign_lock = 1 * object_not_found = 2 * system_failure = 3 * OTHERS = 4. * IF sy-subrc <> 0. * MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno * WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. * ENDIF. * * CALL FUNCTION 'NUMBER_GET_NEXT' * EXPORTING * nr_range_nr = '01' * object = 'ZCLA' * toyear = sy-datum+0(4) * IMPORTING * number = lv_nr * EXCEPTIONS * interval_not_found = 1 * number_range_not_intern = 2 * object_not_found = 3 * quantity_is_0 = 4 * quantity_is_not_1 = 5 * interval_overflow = 6 * buffer_overflow = 7 * OTHERS = 8. * IF sy-subrc <> 0. * MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno * WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. * ENDIF. * CONCATENATE 'MC' sy-datum+2(2) lv_nr INTO WA_BOLNR-ZCLANO. * CLEAR:WA_BOLNR. * "解锁 * CALL FUNCTION 'NUMBER_RANGE_DEQUEUE' * EXPORTING * object = 'ZCLA' * EXCEPTIONS * object_not_found = 1 * OTHERS = 2. * IF sy-subrc <> 0. ** Implement suitable error handling here * ENDIF. ENDFORM.