根据结构(表)名创建LT_ALV_CAT,后续更改显示字段,直接改结构(表)就可以了。
ZPPR0102
*&---------------------------------------------------------------------* *& Report ZPPR0102 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ZPPR0102. INCLUDE ZPPR0102_HEAD. INCLUDE ZPPR0102_SCREEN. INCLUDE ZPPR0102_FORM. START-OF-SELECTION. PERFORM FRM_CHECK_AUTH. IF LV_CHECKF EQ 'X'. MESSAGE LV_MSG TYPE 'S' DISPLAY LIKE 'E'. RETURN. ENDIF. PERFORM FRM_GET_DATA. "动态创建内表 <dyn_table>,获取lt_alv_cat PERFORM FRM_GET_DATA2." END-OF-SELECTION. PERFORM F_DISPLAY_DATA.View Code
ZPPR0102_HEAD
*&---------------------------------------------------------------------* *& INCLUDE ZPPR0102_HEAD *&---------------------------------------------------------------------* TABLES:SSCRFIELDS,AFKO,AFPO. TYPE-POOLS: SLIS,OLE2. TYPES: BEGIN OF SY_OUTPUT. INCLUDE STRUCTURE ZPPT002. TYPES: ZCOLOR(4), ZCOLOR2 TYPE CHAR200, "存储导出EXCEL的行数,列数,颜色。 行.列.颜色;行.列.颜色; * cellcolor TYPE lvc_t_scol, WERKS TYPE AFPO-DWERK, "超出40位则判为'X' * MATNR TYPE AFPO-MATNR, "超出80位则判为'X' SLBOX, END OF SY_OUTPUT. DATA: GT_OUTPUT TYPE TABLE OF SY_OUTPUT, GS_OUTPUT TYPE SY_OUTPUT. FIELD-SYMBOLS: <FS_OUT> TYPE SY_OUTPUT. DATA: D_REF TYPE REF TO DATA, D_REF2 TYPE REF TO DATA, LT_ALV_CAT TYPE TABLE OF LVC_S_FCAT, LS_ALV_CAT LIKE LINE OF LT_ALV_CAT. DATA: LT_TABLE LIKE TABLE OF DNTAB. DATA: LS_TABLE TYPE DNTAB. DATA: DYN_TABLE TYPE REF TO DATA. DATA: DYN_WA TYPE REF TO DATA. FIELD-SYMBOLS :<DYN_TABLE> TYPE TABLE, <DYN_WA> TYPE ANY, <DYN_FIELD> TYPE ANY, <FS_STR> TYPE ANY. DATA: DYN_TABLE2 TYPE REF TO DATA. DATA: DYN_WA2 TYPE REF TO DATA. FIELD-SYMBOLS :<DYN_TABLE2> TYPE TABLE, <DYN_WA2> TYPE ANY, <DYN_FIELD2> TYPE ANY, <FS_STR2> TYPE ANY. DATA: GO_EXCEL TYPE OLE2_OBJECT, GO_WORKBOOK TYPE OLE2_OBJECT, GO_SHEET TYPE OLE2_OBJECT. DATA: GS_LAYOUT TYPE LVC_S_LAYO, GT_FIELDCAT TYPE LVC_T_FCAT. DATA GV_COL TYPE P DECIMALS 0. DATA: LV_CHECKF TYPE CHAR1, LV_MSG TYPE BAPI_MSG.View Code
ZPPR0102_SCREEN
*&---------------------------------------------------------------------* *& INCLUDE ZPPR0102_SCREEN *&---------------------------------------------------------------------* SELECTION-SCREEN: BEGIN OF BLOCK B01 WITH FRAME TITLE TEXT-001. *PARAMETERS: p_name TYPE dd03l-tabname OBLIGATORY. SELECT-OPTIONS: S_WERKS FOR AFPO-DWERK DEFAULT '6100' OBLIGATORY, S_AUFNR FOR AFPO-AUFNR. * S_ERDAT FOR afko-ERDAT, * S_MATNR FOR AFPO-MATNR. SELECTION-SCREEN: END OF BLOCK B01.View Code
ZPPR0102_FORM
*&---------------------------------------------------------------------* *& INCLUDE ZPPR0102_FORM *&---------------------------------------------------------------------* FORM FRM_GET_DATA . *取出表结构的字段目录 CALL FUNCTION 'NAMETAB_GET' EXPORTING LANGU = SY-LANGU TABNAME = 'ZPPT002' TABLES NAMETAB = LT_TABLE EXCEPTIONS NO_TEXTS_FOUND = 1. *根据取出的字段目录生成参考字段目录 CLEAR LT_ALV_CAT. LOOP AT LT_TABLE INTO LS_TABLE. * IF ls_table-fieldname NE 'MANDT'. LS_ALV_CAT-FIELDNAME = LS_TABLE-FIELDNAME. LS_ALV_CAT-REF_TABLE = 'ZPPT002'. *CONVEXIT LS_ALV_CAT-REF_FIELD = LS_TABLE-FIELDNAME. IF LS_TABLE-FIELDNAME EQ 'MEINS'. LS_ALV_CAT-NO_CONVEXT = 'X'. CLEAR: LS_ALV_CAT-REF_FIELD,LS_ALV_CAT-REF_TABLE. ENDIF. LS_ALV_CAT-SELTEXT = LS_TABLE-FIELDTEXT. LS_ALV_CAT-SCRTEXT_L = LS_TABLE-FIELDTEXT. LS_ALV_CAT-SCRTEXT_M = LS_TABLE-FIELDTEXT. LS_ALV_CAT-SCRTEXT_S = LS_TABLE-FIELDTEXT. APPEND LS_ALV_CAT TO LT_ALV_CAT. * ENDIF. CLEAR LS_ALV_CAT. ENDLOOP. *内表创建 CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE EXPORTING IT_FIELDCATALOG = LT_ALV_CAT IMPORTING EP_TABLE = D_REF. *指定生成的内表到字段符号 ASSIGN D_REF->* TO <DYN_TABLE>. *创建动态工作区结构 CREATE DATA DYN_WA LIKE LINE OF <DYN_TABLE>. *创建动态工作区 ASSIGN DYN_WA->* TO <DYN_WA>. DELETE LT_ALV_CAT[] WHERE FIELDNAME EQ 'MANDT'. * DELETE lt_alv_cat[] WHERE fieldname EQ 'ZTABIX' OR fieldname EQ 'MANDT'. * ls_alv_cat-fieldname = 'ZCOLOR'. ** ls_alv_cat-ref_table = 'ZMMT090'. * ls_alv_cat-ref_field = 'ZCOLOR'. * ls_alv_cat-seltext = '状态'. * ls_alv_cat-scrtext_l = '状态'. * ls_alv_cat-scrtext_m = '状态'. * ls_alv_cat-scrtext_s = '状态'. * APPEND ls_alv_cat TO lt_alv_cat. * CLEAR: GT_FIELDCAT[]. DEFINE SET_FIELDCAT. CLEAR: LS_ALV_CAT. LS_ALV_CAT-FIELDNAME = &1. LS_ALV_CAT-REF_FIELD = &2. LS_ALV_CAT-REF_TABLE = &3. LS_ALV_CAT-CONVEXIT = &4. LS_ALV_CAT-DO_SUM = &5. LS_ALV_CAT-SELTEXT = &6. LS_ALV_CAT-SCRTEXT_L = &6. LS_ALV_CAT-SCRTEXT_M = &6. LS_ALV_CAT-SCRTEXT_S = &6. LS_ALV_CAT-COLTEXT = &6. APPEND LS_ALV_CAT TO LT_ALV_CAT[]. END-OF-DEFINITION. *显示内容 SET_FIELDCAT 'WERKS' 'DWERK' 'AFPO' '' '' '工厂'. * SET_FIELDCAT 'MATNR' 'MATNR' 'AFPO' '' '' '物料'. ENDFORM. "frm_get_data *&---------------------------------------------------------------------* *& Form FRM_GET_DATA2 *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM FRM_GET_DATA2 . DATA: LS_AFPO LIKE AFPO, LT_AFPO LIKE TABLE OF AFPO. SELECT * INTO TABLE GT_OUTPUT FROM ZPPT002 WHERE AUFNR IN S_AUFNR. IF GT_OUTPUT[] IS NOT INITIAL. SELECT AUFNR POSNR DWERK INTO CORRESPONDING FIELDS OF TABLE LT_AFPO FROM AFPO WHERE AUFNR IN S_AUFNR AND DWERK IN S_WERKS. ENDIF. LOOP AT GT_OUTPUT ASSIGNING <FS_OUT>. READ TABLE LT_AFPO INTO LS_AFPO WITH KEY AUFNR = <FS_OUT>-AUFNR BINARY SEARCH. IF SY-SUBRC EQ 0. <FS_OUT>-WERKS = LS_AFPO-DWERK. * <fs_out>-matnr = ls_afpo-matnr. ENDIF. ENDLOOP. DELETE GT_OUTPUT[] WHERE WERKS NOT IN S_WERKS. ENDFORM. "frm_get_data2 *&---------------------------------------------------------------------* *& Form F_DISPLAY_DATA *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> p1 text *& <-- p2 text *&---------------------------------------------------------------------* FORM F_DISPLAY_DATA . PERFORM F_SET_LAYOUT. * PERFORM F_SET_FIELDCAT. PERFORM F_ALV_DISPLAY TABLES GT_OUTPUT. ENDFORM. "f_display_data *&---------------------------------------------------------------------* *& Form f_set_layout *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM F_SET_LAYOUT. CLEAR: GS_LAYOUT. GS_LAYOUT-BOX_FNAME = 'SLBOX'. GS_LAYOUT-ZEBRA = 'X'. GS_LAYOUT-CWIDTH_OPT = 'X'. * gs_layout-ctab_fname = 'CELLCOLOR'. ENDFORM. "f_set_layout *&---------------------------------------------------------------------* *& Form f_alv_display *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->IT_TABLE text *----------------------------------------------------------------------* FORM F_ALV_DISPLAY TABLES IT_TABLE. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING I_CALLBACK_PROGRAM = SY-REPID * i_callback_pf_status_set = 'F_SET_PF_STATUS' I_CALLBACK_USER_COMMAND = 'F_USER_COMMAND' IS_LAYOUT_LVC = GS_LAYOUT IT_FIELDCAT_LVC = LT_ALV_CAT I_SAVE = 'U' TABLES T_OUTTAB = IT_TABLE EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2. IF SY-SUBRC <> 0. ENDIF. ENDFORM. "f_alv_display *-----------------------------------------------------------------------* * *-----------------------------------------------------------------------* FORM F_SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB. * SET PF-STATUS 'STD'. ENDFORM. "f_set_pf_status *&---------------------------------------------------------------------* *& Form f_user_command *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->R_UCOMM text * -->RS_SELFIELD text *----------------------------------------------------------------------* FORM F_USER_COMMAND USING R_UCOMM LIKE SY-UCOMM RS_SELFIELD TYPE SLIS_SELFIELD. PERFORM F_CHECK_CHANGE_DATA. CASE R_UCOMM. WHEN 'ZSAVE'. * IF r1 EQ 'X'. * PERFORM frm_create_belnr. * ENDIF. WHEN '&IC1'."双击事件 * READ TABLE gt_output INTO DATA(gs_output) INDEX rs_selfield-tabindex. * IF sy-subrc EQ 0. * SET PARAMETER ID 'MAT' FIELD gs_output-matnr. * CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN. * ENDIF. ENDCASE. PERFORM F_REFRESH_ALV. ENDFORM. "f_user_command *-----------------------------------------------------------------------* * *-----------------------------------------------------------------------* FORM F_CHECK_CHANGE_DATA. DATA: LO_GRID TYPE REF TO CL_GUI_ALV_GRID. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING E_GRID = LO_GRID. LO_GRID->CHECK_CHANGED_DATA( ). ENDFORM. "f_check_change_data *-----------------------------------------------------------------------* * *-----------------------------------------------------------------------* FORM F_REFRESH_ALV. DATA: LO_GRID TYPE REF TO CL_GUI_ALV_GRID, LS_IS_STABLE TYPE LVC_S_STBL. CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' IMPORTING E_GRID = LO_GRID. LS_IS_STABLE-COL = 'X'. LS_IS_STABLE-ROW = 'X'. LO_GRID->REFRESH_TABLE_DISPLAY( EXPORTING IS_STABLE = LS_IS_STABLE EXCEPTIONS FINISHED = 1 OTHERS = 2 ). IF SY-SUBRC <> 0. ENDIF. ENDFORM. "f_refresh_alv *&---------------------------------------------------------------------* *& Form FRM_CHECK_AUTH *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM FRM_CHECK_AUTH . DATA: LT_T001W LIKE TABLE OF T001W, LS_T001W LIKE T001W. SELECT WERKS INTO TABLE LT_T001W FROM T001W WHERE WERKS IN S_WERKS. LOOP AT LT_T001W INTO LS_T001W. AUTHORITY-CHECK OBJECT 'M_MSEG_WWA' ID 'WERKS' FIELD LS_T001W-WERKS ID 'ACTVT' FIELD '03'. IF SY-SUBRC <> 0. LV_CHECKF = 'X'. CONCATENATE LV_MSG '沒有' LS_T001W-WERKS '工廠權限;' INTO LV_MSG. EXIT. ENDIF. ENDLOOP. IF S_AUFNR[] IS INITIAL. LV_CHECKF = 'X'. CONCATENATE LV_MSG '请填写要查询的工单' INTO LV_MSG. ENDIF. ENDFORM. " FRM_CHECK_AUTHView Code
标签:DATA,CAT,LS,简单,ALV,TABLE,TYPE,模板 From: https://www.cnblogs.com/chaguoguo/p/17551218.html