动态ALV程序

*&---------------------------------------------------------------------*
*& Report  ZDYNRPO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZDYNRPO.
*-------------ALV 相关--------------------------
DATA: WA_LAYO TYPE LVC_S_LAYO.
DATA: WA_FCAT TYPE LVC_S_FCAT,
      IT_FCAT TYPE LVC_T_FCAT.

DATA: IT_SCOL TYPE LVC_T_SCOL.

DATA: G_GRID TYPE REF TO CL_GUI_ALV_GRID.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>,
               <DYN_FIELD>.

DATA: DY_TABLE TYPE REF TO DATA,
      DY_LINE  TYPE REF TO DATA.

DATA: GV_COLNAME    TYPE CHAR10,
      GV_DESC       TYPE CHAR10,
      GV_INDEX      TYPE CHAR02.

*屏幕绘制
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE T1.
* 可描述的列数
PARAMETERS: COL TYPE I.

SELECTION-SCREEN END   OF BLOCK B1.

* 初始化 添加固定列 姓名 年龄
INITIALIZATION.

*  CLEAR: IT_FCAT.

  PERFORM FRM_ADD_FCAT USING:
           'NAME' 'C' '姓名' '10' ''.

  PERFORM FRM_ADD_FCAT USING:
           'AGE' 'C' '年龄' '6' ''.

START-OF-SELECTION.

* 根据条件动态生成列
  DO COL TIMES.
    GV_INDEX = GV_INDEX + 1.
    CONCATENATE 'COL' GV_INDEX INTO GV_COLNAME.
    CONCATENATE '维护信息' GV_INDEX INTO GV_DESC.
    PERFORM FRM_ADD_FCAT
      USING GV_COLNAME 'C' GV_DESC '15' 'X'.
  ENDDO.

* 根据it_fact生成动态表
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = IT_FCAT
    IMPORTING
      EP_TABLE        = DY_TABLE.

  ASSIGN DY_TABLE->* TO <DYN_TABLE>.

  CREATE DATA DY_LINE LIKE LINE OF <DYN_TABLE>.

  ASSIGN DY_LINE->* TO <DYN_WA>.

* 给表字段赋值
  CLEAR: GV_INDEX.
  DO COL TIMES.
    GV_INDEX = GV_INDEX + 1.
    CONCATENATE 'COL' GV_INDEX INTO GV_COLNAME.
    ASSIGN COMPONENT GV_COLNAME OF STRUCTURE <DYN_WA>  TO <DYN_FIELD>.
    <DYN_FIELD> = '添加信息'.
  ENDDO.

  GV_COLNAME = 'NAME'.
  ASSIGN COMPONENT GV_COLNAME OF STRUCTURE <DYN_WA>  TO <DYN_FIELD>.
  <DYN_FIELD> = 'MSC_笑明'.

  GV_COLNAME = 'AGE'.
  ASSIGN COMPONENT GV_COLNAME OF STRUCTURE <DYN_WA>  TO <DYN_FIELD>.
  <DYN_FIELD> = '18'.

  APPEND <DYN_WA> TO <DYN_TABLE>.

* 相当于给内表放入两条数据
  CLEAR: GV_INDEX.
  DO COL TIMES.
    GV_INDEX = GV_INDEX + 1.
    CONCATENATE 'COL' GV_INDEX INTO GV_COLNAME.
    ASSIGN COMPONENT GV_COLNAME OF STRUCTURE <DYN_WA> TO <DYN_FIELD>.
    <DYN_FIELD> = '添加信息'.
  ENDDO.

  GV_COLNAME = 'NAME'.
  ASSIGN COMPONENT GV_COLNAME OF STRUCTURE <DYN_WA>  TO <DYN_FIELD>.
  <DYN_FIELD> = 'MSC_小迈'.

  GV_COLNAME = 'AGE'.
  ASSIGN COMPONENT GV_COLNAME OF STRUCTURE <DYN_WA>  TO <DYN_FIELD>.
  <DYN_FIELD> = '3'.

  APPEND <DYN_WA> TO <DYN_TABLE>.

END-OF-SELECTION.

  CLEAR: WA_LAYO.
  WA_LAYO-ZEBRA = 'X'.
*  WA_LAYO-CWIDTH_OPT = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID
      IS_LAYOUT_LVC      = WA_LAYO
      IT_FIELDCAT_LVC    = IT_FCAT
    TABLES
      T_OUTTAB           = <DYN_TABLE>
    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.

*&---------------------------------------------------------------------*
*&      Form  frm_add_fcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->VALUE1     text
*      -->VALUE2     text
*      -->VALUE3     text
*      -->VALUE4     text
*----------------------------------------------------------------------*
FORM FRM_ADD_FCAT USING VALUE1 VALUE2 VALUE3 VALUE4 VALUE5.
  WA_FCAT-FIELDNAME   = VALUE1.
  WA_FCAT-INTTYPE     = VALUE2.
  WA_FCAT-REPTEXT     = VALUE3.
  WA_FCAT-OUTPUTLEN   = VALUE4.
  WA_FCAT-EDIT        = VALUE5.
  APPEND WA_FCAT TO IT_FCAT.
  CLEAR: WA_FCAT.
ENDFORM.                    "frm_add_fcat

 

posted @ 2019-09-10 10:32  真的是很难  阅读(657)  评论(0编辑  收藏  举报